Very new to working with Visual Basic / Excel. I am trying to write a quick script that enters the current time in one column, and allows the user to enter how many days/hours/minutes will pass until a new time, and output that in another column.
I’m sure this isn’t the best way to do it, but what I have so far is the following. I have given up on fiddling with dates, and am just working with the time:
Sub TimeModule()
Dim DaysLeft, HoursLeft, MinutesLeft As Double
DaysLeft = Val(InputBox("Days left"))
HoursLeft = Val(InputBox("Hours left"))
MinutesLeft = Val(InputBox("Minutes left"))
Dim CurrentTime As Date
CurrentTime = TimeValue(Now())
ActiveCell.Value = CurrentTime
ActiveCell.Offset(0, 1) = CurrentTime + Time(HoursLeft, MinutesLeft, 0)
End Sub
I am getting an error, of course. If anyone could shed some light on a better way to do this, along with the functions I’m misusing, I would really appreciate it!
Edit: I would, of course ultimately like for the script to handle days as well.
I think this is possible just using cell functions in Excel, if I’ve understood you correctly.
For example, this is what you’d see…
…and this is what is in each cell (assuming top-left cell is A1)…
Describing each function:
NOW()returns the current date and time formatted as a date and time.DATE(year,month,day)returns the number that represents the date in MS Excel date-time code.TIME(hours,minutes,seconds)converts hours, minutes, and seconds given as numbers to an Excel serial number, formatted with a time format.Dissecting the equation in the last cell:
A2is the cell containing the current date/time (as of last worksheet calculation).B2is the user-inputted value for days.TIME(C2,D2,0)is the TIME() function, taking the user-inputted values for hours and minutes from cellsC2andD2respectively.Is this anything like your intended functionality…?