Home > Software engineering >  Excel VBA Error while trying to add date and time via DateAdd
Excel VBA Error while trying to add date and time via DateAdd

Time:11-26

I am a newbie with VBA but I am trying to create some tools to make data input a bit easier for my colleagues.

I have a userform with two TextBoxes, one textbox (called TextBox1) is input for days, and the other TextBox(called TextBox2) is an input for time . I would like to add the days and times to the the starting point of "01/01/2022 00:00:00" but somehow I cannot get this working.

for example: TextBox1 = "5" TextBox2 = "10:00:00"

now I would like to add one day (value of TextBox1) to the starting day which is fixed (01/01/2022 00:00:00)

I use for this the following formula:

Range("A1").Value = DateAdd("d", Me.TextBox1.Value, "01/01/2022 00:00:00"   Me.TextBox2.Value)

the output I was expecting = 06/01/2022 10:00:00 but instead it gives me a Type mismatch error, I have no clue what I have to adjust.

anyone has any idea?

CodePudding user response:

Use DateTime, not text:

Range("A1").Value = DateAdd("d", Val(Me.TextBox1.Value), DateSerial(2022, 1, 1)   TimeValue(Me.TextBox2.Value))
  • Related