当前,我需要为工作表中的每一行重复这一行代码,如何使代码更短而不必引用每一行?
Private Sub Work:sheet_SelectionChange(ByVal Target As Range)
Me.Range("G114:AH114").Interior.Color = Me.Range("B114").Interior.Color
Me.Range("G115:AH115").Interior.Color = Me.Range("B115").Interior.Color
Me.Range("G116:AH116").Interior.Color = Me.Range("B116").Interior.Color
Me.Range("G117:AH117").Interior.Color = Me.Range("B117").Interior.Color
Me.Range("G118:AH118").Interior.Color = Me.Range("B118").Interior.Color
Me.Range("G119:AH119").Interior.Color = Me.Range("B119").Interior.Color
Me.Range("G120:AH120").Interior.Color = Me.Range("B120").Interior.Color
End Sub
谢谢
答案 0 :(得分:2)
您可以修改工作表名称和行变量,然后尝试:
Option Explicit
Sub test()
Dim Row As Long
Row = 114
Do Until Row = 121
With ThisWorkbook.Worksheets("Sheet1")
.Cells(Row, 2).Interior.Color = .Cells(Row, 7).Interior.Color
End With
Row = Row + 1
Loop
End Sub
答案 1 :(得分:1)
感谢您的帮助;我使用了以下代码:
Option Explicit
Sub test()
Dim Row As Long
Row = 88
Do Until Row = 121
With ThisWorkbook.Worksheets("Two Weekly Plan 2019")
.Range(Cells(Row, 7), Cells(Row, 34)).Interior.Color = .Cells(Row, 2).Interior.Color
End With
Row = Row + 1
Loop
End Sub