Calculating dates

WayneAli

New Member
Joined
Jun 9, 2015
Messages
11
Hi all,
Finally getting somewhere with my data sheet but have come unstuck with calculating dates in VBA.

I have a UserForm set up that the user can enter a "New User" name and then go through a series of 55 text boxes and enter the days remaining before they need to complete individual online eLearning modules.

I'll pop the code that I have then give a bit of an explanation.

VBA Code:
Dim ws As Worksheet
Dim crew_Name As Variant, r As Variant, dr As Variant
Dim i As Integer

    Set ws = ThisWorkbook.Worksheets("DataSheet1")
'------Set crew_Name as Surname,  Firstname
    crew_Name = Me.TextBox101 & ",  " & Me.TextBox100
        If Len(crew_Name) = 0 Then Exit Sub
'------Find the current row for this staff member, previous code has entered them to the end of column 2 on DataSheet1
    r = Application.Match(crew_Name, ws.Range("A3:BB22").Columns(2), 0)
        If IsError(r) Then Err.Raise 744, , crew_Name & " Not Found"
'------Subtracts the number of days in TextBox & i from todays date
    dr = DateAdd("d", - Val("Me.TextBox & i"), Date)

'------Row 2 in the spread sheet has a time interval identifying how often the eLearn needs completing, 6, 12 or 24 monthly
'         the idea is that a search is made of row 2 to find either 6, 12 or 24 then adds either 6, 12 or 24 months to "dr"
'         the resulting date is entered into the corresponding staff name row in the respective column i
    If ws.Range("A1:BA22").Cells(2, i).Value = 6 Then
        ws.Range("A3:BB22").Cells(CLng(r), i).Value = DateAdd("m", 6, dr)
    ElseIf ws.Range("A1:BA22").Cells(2, i).Value = 12 Then
        ws.Range("A3:BB22").Cells(CLng(r), i).Value = DateAdd("m", 12, dr)
    ElseIf ws.Range("A1:BA22").Cells(2, i).Value = 24 Then
        ws.Range("A3:BB22").Cells(CLng(r), i).Value = DateAdd("m", 24, dr)
'------If an entry has not been made the cell in the speadsheet column i is left blank
    ElseIf "me.textbox" & i = "" Then
        ws.Range("A3:BB22").Cells(CLng(r), i).Value = ""
End If
Next i

I feel I am close but it is entering the "New crew_Name" and referencing the row of that name, however the date calculation is going awry. Instead of adding 6, 12 or 24 months to "dr", in essence this should enter a date in the speadsheet when the crew_Name last complete the respective course, what it is doing is just adding 6, 12 or 24 months onto todays date (depending on row 2) into the corresponding crew name row.

The other thing I have noticed is that it is making an entry in every column/cell of the crew_Name irrespective of whether the "TextBox & i" is filled or blank, the code is identifying the 6, 12 or 24 in row 2 of the sheet and entering a date into the crew_Name row.

I've spent the day on this and this is probably the closest I've come to success, but I'm knackered so thought I'd ask here.

Hopefully this all makes sense, and if anyone can see how I need to tweek my code so that the result entered into the respective row is "Today - TextBox & i + '6, 12 or 24 months' or '182, 365 or 730' days" that would be fantastic.

Thanks all
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I guess this line is wrong:
dr = DateAdd("d", - Val("Me.TextBox & i"), Date)

Maybe:
VBA Code:
dr = DateAdd("d", - Val(Me.TextBox & i), Date)
Beware that a For I=xx to yy is missing somewhere in the code
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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