我想根据C列的长度分析B列中的单元格,并将我的值返回到A列。
以下代码对工作表1很好用,但是在后续工作表中,范围不会更改以反映C列的长度。换句话说,对于所有后续工作表,尽管返回的值正确,但它们只会填充到sheet1中C列的长度。
请问有人知道为什么我的循环不能正常反映C列的动态范围吗?
Sub SetValuesAllSheets()
Dim wSht As Worksheet
Dim myRng As Range
Dim allwShts As Sheets
Dim cel As Range
Dim LR As Long
LR = Range("C" & Rows.Count).End(xlUp).Row
Set allwShts = Worksheets
For Each wSht In allwShts
Set myRng = wSht.Range("B1:B" & LR)
For Each cel In myRng
If cel.Interior.Color = RGB(255, 0, 0) Then
cel.Offset(0, -1) = "colour"
ElseIf IsNumeric(cel) = True Then
cel.Offset(0, -1) = "number"
End If
Next cel
Next wSht
End Sub
答案 0 :(得分:2)
实际上,为活动工作表确定了LR。您需要将其插入循环中才能使其正常工作:
Sub SetValuesAllSheets()
Dim wSht As Worksheet
Dim myRng As Range
Dim allwShts As Sheets
Dim cel As Range
Dim LR As Long
Set allwShts = Worksheets
For Each wSht In allwShts
LR = wSht.Range("C" & Rows.Count).End(xlUp).Row
Set myRng = wSht.Range("B1:B" & LR)
For Each cel In myRng
If cel.Interior.Color = RGB(255, 0, 0) Then
cel.Offset(0, -1) = "colour"
ElseIf IsNumeric(cel) = True Then
cel.Offset(0, -1) = "number"
End If
Next cel
Next wSht
End Sub
致谢