matthew_dubbels
New Member
- Joined
- Jan 9, 2019
- Messages
- 3
I am trying to write a macro that will compare 2 cells, then will take the dates corresponding to those cells and tell me the amount of week days between the 2 dates.
The part I'm having trouble with is the IF i wrote
Sub DateCalculation()
'
' DateCalculation Macro
'
' Keyboard Shortcut: Ctrl+Shift+A
'
'Inserting Columns I
Columns(9).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'In cell I1 put the text "Number of Work Days"
Range("I1").FormulaR1C1 = "Number of Work Days"
'Creating variable and setting as a range
Dim MyRange As Range
Dim MyCell As Range
'Creates Date variables firstDate and secondDate, weekDays is created as an integer
Dim firstDate As Date, secondDate As Date, weekDays As Integer
'Creates Lastrow Function to be the equivalent of a 'end' + 'down arrow' operation
Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
'MyRange is equal to A2-A(lastrow)
Set MyRange = Range(Cells(2, 1), Cells(Lastrow, 1))
'loop to continue down the column A one at a time until the lastrow
For Each MyCell In MyRange
'Broken Part
If MyCell.Value = Cells(MyCell - 1, 1).Value Then
firstDate = Cells(MyCell, 8)
secondDate = Cells(MyCell - 1, 8)
weekDays = DateDiff("w", firstDate, secondDate)
Cells(MyCell, 9) = weekDays
Else
Cells(MyCell, 9) = "0"
End If
'Goes to next cell down
Next MyCell
End Sub
The part I'm having trouble with is the IF i wrote
Sub DateCalculation()
'
' DateCalculation Macro
'
' Keyboard Shortcut: Ctrl+Shift+A
'
'Inserting Columns I
Columns(9).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'In cell I1 put the text "Number of Work Days"
Range("I1").FormulaR1C1 = "Number of Work Days"
'Creating variable and setting as a range
Dim MyRange As Range
Dim MyCell As Range
'Creates Date variables firstDate and secondDate, weekDays is created as an integer
Dim firstDate As Date, secondDate As Date, weekDays As Integer
'Creates Lastrow Function to be the equivalent of a 'end' + 'down arrow' operation
Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
'MyRange is equal to A2-A(lastrow)
Set MyRange = Range(Cells(2, 1), Cells(Lastrow, 1))
'loop to continue down the column A one at a time until the lastrow
For Each MyCell In MyRange
'Broken Part
If MyCell.Value = Cells(MyCell - 1, 1).Value Then
firstDate = Cells(MyCell, 8)
secondDate = Cells(MyCell - 1, 8)
weekDays = DateDiff("w", firstDate, secondDate)
Cells(MyCell, 9) = weekDays
Else
Cells(MyCell, 9) = "0"
End If
'Goes to next cell down
Next MyCell
End Sub