Home » excel » excel – How to repeat macro for entire sheet

excel – How to repeat macro for entire sheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

First post on here so I’m sorry if this has been put in the wrong place or not enough information. Hopefully quite a simple one. I currently have a macro running on my worksheet but this only applies with this specific row. I would like to continue this macro throughout the worksheet. I know in the code I only reference specific cells, but I do not know what to put in to reference the specific cells for each row repeated for the whole sheet. Could anyone help? I know this is basis but the help is much appreciated.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("C383")) Is Nothing Then
        Range("F383").ClearContents
    End If
End Sub
How to&Answers:

You need to loop through the rows and do the same what you did for this specific row.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim AffectedRange As Range
    Set AffectedRange = Intersect(Target, Me.Range("C383:C500")) 'check rows 383 to 500 or use Me.Range("C:C") for entire column C

    If Not AffectedRange Is Nothing Then
        Dim Cell As Range
        For Each Cell In AffectedRange 
            Me.Range("F" & Cell.Row).ClearContents
        Next Cell
    End If
End Sub