I am trying to loop through all cells in a specified range and replace two different parts of the function in the cell.
However, the code seems to run, and when I put a msgbox LastCol, it returns me the last cell in the row, but the function does not change.
Code:
Sub Function1()
Dim LastCol As Integer
Dim i As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
With ActiveSheet
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
For i = 1 To LastCol
ActiveWorkbook.Sheets("Data").Range("D3" & i).Replace "4. Apr", "5. May", xlPart
ActiveWorkbook.Sheets("Data").Range("D3" & i).Replace "April", "May", xlPart
Next i
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
If i was 1 then Range("D3" & i) refers to D31. Try,
with ActiveWorkbook.Sheets("Data")
.Range(.cells(3, "D"), .cells(3, LastCol)).Replace What:="4. Apr", replacement:="5. May", LookAt:=xlPart, MatchCase:=false
.Range(.cells(3, "D"), .cells(3, LastCol)).Replace What:="April", replacement:="May", LookAt:=xlPart, MatchCase:=false
end with
tbh, it's not at all clear what you mean by 'specified range'.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments