Hello Board,
On 11/9/14, I got a typically great response from the board when I asked about "Adding time to a date". The attached code that resulted works but I had to hard-code the formatting for the new value; Excel will default to displaying the full Date & Time string if I don't specify the format. I get a run-time error when I 'Dim cFormat' as a string or a variant; 'Dim .. Date' would defeat the goal of a generic code-block. What an I doing wrong at thline marked with'==>'?
On 11/9/14, I got a typically great response from the board when I asked about "Adding time to a date". The attached code that resulted works but I had to hard-code the formatting for the new value; Excel will default to displaying the full Date & Time string if I don't specify the format. I get a run-time error when I 'Dim cFormat' as a string or a variant; 'Dim .. Date' would defeat the goal of a generic code-block. What an I doing wrong at thline marked with'==>'?
Code:
'----------------------------------------------------------------------------
Private Sub cmdChange_Click()
'
'1sr effort with input screens, per link found thru Mr. XL
'Change cell-value per numbers entered, confim in calling preoram
'11/12/14
'
Dim bSubtract As Boolean
Dim cFormat As Variant 'String bombs
Dim iDays As Integer, iHours As Integer, iMins As Integer, iSecs As Integer
Dim dChange As Date, dNew As Date, dOrig As Date
'Imitialize values
bSubtract = Me.optSubtract.Value
==> 'cFormat = ActiveCell.Format
dOrig = ActiveCell.Value
'Move data from form to mVars
iDays = Me.TxtDays.Value
iHours = Me.TxtHrs.Value
iMins = Me.TxtMins.Value
'Convert D/H/M to TDS serial-value, update cell
iDays = iDays * 1440
iHours = iHours * 60
dChange = iDays + iHours + iMins
If dChange = 0 Then
Me.TxtDays.SetFocus
MsgBox "Please enter aleast one nunumber"
Exit Sub
ElseIf bSubtract Then
dChange = dChange * -1
End If
dChange = dChange / 1440
dNew = dOrig + dChange
ActiveCell.Value = dNew
ActiveCell.NumberFormat = "[$-409]h:mm AM/PM;@"
Unload Me
End Sub
'----------------------------------------------------------------------------
Private Sub cmdCancel_Click()
Unload Me
End Sub
'----------------------------------------------------------------------------
Private Sub UserForm_QueryClose(Cancel As Integer, _
CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Please use the button!"
End If
End Sub
'----------------------------------------------------------------------------
'----------------------------------------------------------------------------