montypython
New Member
- Joined
- Apr 1, 2014
- Messages
- 12
Hi Some advice needed
Is there wa way to optimise these macro's I am still learning so please bare with me
Dim dateRange As Range
Set dateRange = Range("A2:H" & Cells(Rows.Count, "H").End(xlUp).Row)
If Application.CountIf(dateRange, "") = 0 And Application.CountIf(dateRange, "=ISNUMBER(value)") = dateRange.Cells.Count Then
Range("I2:I" & lastRow).FormulaR1C1 = "=INT(RC[-7]-RC[-2])"
End If
Sub FormatColumnI()
Dim lastRow As Long
' Determine the last row of data
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
' Set the number format for cells in Column I
Range("I2:I" & lastRow).NumberFormat = "0"
End Sub
Sub CalculateDaysDifference()
Dim lastRow As Long
Dim i As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row 'Get last row in column A
For i = 1 To lastRow 'Loop through each row in column A
If IsDate(Cells(i, "A").Value) And IsDate(Cells(i, "H").Value) Then 'Check if both values in columns A and H are dates
Cells(i, "I").Value = DateDiff("d", Cells(i, "A").Value, Cells(i, "H").Value) 'Calculate the number of days between the two dates and put the result in column I
End If
Next i
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("I:I")) Is Nothing Then
Call CalculateDaysDifference
End If
End Sub
on the last one I am getting a compile error, I need it to run the macro live when updating in my worksheet or perhaps a suggestion for another solution
Thanks Keith
Is there wa way to optimise these macro's I am still learning so please bare with me
Dim dateRange As Range
Set dateRange = Range("A2:H" & Cells(Rows.Count, "H").End(xlUp).Row)
If Application.CountIf(dateRange, "") = 0 And Application.CountIf(dateRange, "=ISNUMBER(value)") = dateRange.Cells.Count Then
Range("I2:I" & lastRow).FormulaR1C1 = "=INT(RC[-7]-RC[-2])"
End If
Sub FormatColumnI()
Dim lastRow As Long
' Determine the last row of data
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
' Set the number format for cells in Column I
Range("I2:I" & lastRow).NumberFormat = "0"
End Sub
Sub CalculateDaysDifference()
Dim lastRow As Long
Dim i As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row 'Get last row in column A
For i = 1 To lastRow 'Loop through each row in column A
If IsDate(Cells(i, "A").Value) And IsDate(Cells(i, "H").Value) Then 'Check if both values in columns A and H are dates
Cells(i, "I").Value = DateDiff("d", Cells(i, "A").Value, Cells(i, "H").Value) 'Calculate the number of days between the two dates and put the result in column I
End If
Next i
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("I:I")) Is Nothing Then
Call CalculateDaysDifference
End If
End Sub
on the last one I am getting a compile error, I need it to run the macro live when updating in my worksheet or perhaps a suggestion for another solution
Thanks Keith