Simple question I think re: Dateserial from inputbox

braindiesel

Well-known Member
Joined
Mar 16, 2009
Messages
571
Office Version
  1. 365
  2. 2019
  3. 2010
Platform
  1. Windows
I am trying to test these 4 lines
Code:
confirmMonth = InputBox("What month (number) are they starting the lease? (1=January, 12 = December)", "Month Start", Month(Date) + 1)    
confirmYear = InputBox("What year are they starting the lease?", "Year Start", Year(Date))
    
confirmLength = InputBox("What is the length (in months) of the lease? (1=month to month, 12 = annual)", "Length of Lease")
    
MsgBox DateSerial(confirmYear, confirmMonth + confirmLength, 1)
but the year isn't working.

It seems to be adding the confirmmonth to the confirmyear

Your help is appreciated
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I am trying to test these 4 lines
Code:
confirmMonth = InputBox("What month (number) are they starting the lease? (1=January, 12 = December)", "Month Start", Month(Date) + 1)    
confirmYear = InputBox("What year are they starting the lease?", "Year Start", Year(Date))
    
confirmLength = InputBox("What is the length (in months) of the lease? (1=month to month, 12 = annual)", "Length of Lease")
    
MsgBox DateSerial(confirmYear, confirmMonth + confirmLength, 1)
but the year isn't working.

It seems to be adding the confirmmonth to the confirmyear

Your help is appreciated

This...
confirmMonth + confirmLength
...is concatenating the two strings (not adding the values). So it is creating two or three digit month values which can be multiple years.

Try this...
MsgBox DateSerial(CLng(confirmYear), CLng(confirmMonth) + CLng(confirmLength), 1)
 
Upvote 0
Add this to the top

Dim confirmMonth As Integer, confirmYear As Integer, confirmLength As Integer

Otherwise, the Month and Length are considered TEXT strings
And in VBA, the + operator is like concatenate..
So if the month was 4 and the length was 12, it became 412
 
Upvote 0
I think your problem is that the Inputbox function returns a string not a number.

Try this:
Code:
confirmMonth = Val(InputBox("What month (number) are they starting the lease? (1=January, 12 = December)", "Month Start", Month(Date) + 1))
If confirmMonth = 0 Then Exit Sub
confirmYear = Val(InputBox("What year are they starting the lease?", "Year Start", Year(Date)))
If confirmYear = 0 Then Exit Sub
confirmLength = Val(InputBox("What is the length (in months) of the lease? (1=month to month, 12 = annual)", "Length of Lease"))
If confirmLength = 0 Then Exit Sub
MsgBox DateSerial(confirmYear, confirmMonth + confirmLength, 1)
 
Upvote 0
Alternativly, use an Application.Inputbox of Type:=1 which returns numbers and not strings.

Code:
confirmMonth = [B]Application[/B].InputBox("What month (number) are they starting the lease? (1=January, 12 = December)", "Month Start", Month(Date) + 1, [B]Type:=1[/B])
confirmYear = [B]Application[/B].InputBox("What year are they starting the lease?", "Year Start", Year(Date), [B]Type:=1[/B])
    
confirmLength = [B]Application[/B].InputBox("What is the length (in months) of the lease? (1=month to month, 12 = annual)", "Length of Lease", [B]Type:=1[/B])
    
MsgBox DateSerial(confirmYear, confirmMonth + confirmLength, 1)
 
Upvote 0
Wow. thank you all so much.
The Type:=1 didn't work, but the CLng worked perfectly

I will try the other two out of curiosity. Thanks everyone
 
Upvote 0
Wow. thank you all so much.
The Type:=1 didn't work, but the CLng worked perfectly

I will try the other two out of curiosity. Thanks everyone

You're welcome.

With the Type:=1 solution, declare (Dim) the variables as Integer or Long.
 
Upvote 0

Forum statistics

Threads
1,215,518
Messages
6,125,292
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top