简体   繁体   中英

How to repeat macro for entire sheet

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM