Discussion Locked

Excel Date Macro

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a column with the weekend date in cell [B3] and the days (Monday - Friday) beneath it begining in cell [B5], between each day I have a gap that's dynamic depending on the number of records I have for a given day. I want to be able to create perhaps a loop that'll fill in the actual date between the days based on the weekend date. However I'm not sure how to approach this.

Any suggestions?

5 replies

by joey indolos

You don't need a macro for this one, a simple formula will do. Excel allows date arithmetic involving dates and numbers (representing number of days). For example, subtracting two dates will return the number of days between them; adding a date and a number x will return a date that is x days away from the first one. The formula "=B3+1" in B5 (and so on down the line) should do the trick.

The big question is the part that you mentioned where the gap between days is dynamic. How exactly does this gap change? If it's a simple matter of deleting and inserting rows between days, Excel will automatically adjust the formulas so that the cell for Monday, wherever it may move, will still refer to Sunday + 1, and so on. If it's more complicated than that, you'll have to give more details. Hope this helps.

by michaelh

I created a VBA macro to transform an unformatted lotus file into a templated layout. Each section is grouped by day with an X amount of records for each day. That's where the gaps come in. So if I can incorporate something within the macro I have to look at the Range and find these gaps and populate the dates would be ideal, as I get about 10-15 of these documents a day.

by abarnes70

Hi Michael

Try this one. You'll need to enter the bottom row of data; it would be more complicated (but certainly possible given more information than I have) to detect the end of the data automatically.

Sub mcrDateFill()
Dim intRow As Integer
Dim intMaxRow As Integer
Dim intCol As Integer
Dim datWeekendDate As Date
Dim strTemp As String
Dim intDateMod As Integer

intDateMod = 0
intCol = 2
intMaxRow = CInt(InputBox("Enter bottom row", "Enter parameter"))
datWeekendDate = Range("B3").Value
For intRow = 5 To intMaxRow Step 1
Range(Cells(intRow, intCol), Cells(intRow, intCol)).Select
strTemp = ActiveCell.Value
Select Case strTemp
Case "Monday"
intDateMod = 1
Case "Tuesday"
intDateMod = 2
Case "Wednesday"
intDateMod = 3
Case "Thursday"
intDateMod = 4
Case "Friday" intDateMod = 5
Case Else
ActiveCell.Value = datWeekendDate + intDateMod
End Select
Next intRow
End Sub

This discussion is closed to new replies.