此代码旨在查看一系列单元格并将它们的值与参考单元格进行比较,然后根据excel的哪一部分与参考值匹配来更新某些单元格。我收到“无效的过程调用或参数”错误。当我使用F8浏览代码时,似乎是在第二条If语句之后触发的。
Private Sub CommandButton1_Click()
Dim rng As Range, cell As Range
Set rng = Range("A171:AJ171")
For Each cell In rng
If cell.Value = ActiveSheet.Cells(4, 4).Value Then
If ActiveSheet.Cells((cell.Row + 1), cell.Column).Value = ActiveSheet.Cells("D3").Value Then
ActiveSheet.Cells("M45").Value = ActiveSheet.Cells((cell.Row + 3), cell.Column).Value
ActiveSheet.Cells("M46").Value = ActiveSheet.Cells((cell.Row + 4), cell.Column).Value
ActiveSheet.Cells("M47").Value = ActiveSheet.Cells((cell.Row + 5), cell.Column).Value
ActiveSheet.Cells("M48").Value = ActiveSheet.Cells((cell.Row + 6), cell.Column).Value
ActiveSheet.Cells("M49").Value = ActiveSheet.Cells((cell.Row + 7), cell.Column).Value
ActiveSheet.Cells("M26").Value = ActiveSheet.Cells((cell.Row + 8), cell.Column).Value
ActiveSheet.Cells("M27").Value = ActiveSheet.Cells((cell.Row + 9), cell.Column).Value
ActiveSheet.Cells("M28").Value = ActiveSheet.Cells((cell.Row + 10), cell.Column).Value
ActiveSheet.Cells("M57").Value = ActiveSheet.Cells((cell.Row + 11), cell.Column).Value
ActiveSheet.Cells("M59").Value = ActiveSheet.Cells((cell.Row + 12), cell.Column).Value
ActiveSheet.Cells("M60").Value = ActiveSheet.Cells((cell.Row + 13), cell.Column).Value
End If
End If
Next
End Sub
答案 0 :(得分:0)
尝试以下代码:
Private Sub CommandButton1_Click()
Dim rng As Range, cell As Range
Dim i As Long
Dim d4 As Variant, d3 As Variant
d3 = Range("D3").Value
d4 = Range("D4").Value
' rng starts at A171
Set rng = Range("A171")
' loop through 36 columns to AJ171
For i = 1 To 36
If rng.Cells(1, i).Value = d4 Then
If rng.Cells(2, i).Value = d3 Then
'Copy values from M45:M49 to Axxx3:Axxx7
Range("M45").Resize(5, 1).Value = rng.Cells(3, i).Resize(5, 1).Value
'Copy values from M26:M28 to Axxx8:Axxx10
Range("M26").Resize(3, 1).Value = rng.Cells(8, i).Resize(3, 1).Value
'Copy values from M57 to Axxx11
Range("M57").Value = rng.Cells(11, i).Value
'Copy values from M59:M60 to Axxx12:Axxx13
Range("M59").Resize(2, 1).Value = rng.Cells(12, i).Resize(2, 1).Value
End If
End If
Next i
End Sub
一些显着的变化:
.Cells(4,4)
中的值与.Range("D4")
相同,并且存储在d4
的循环外部。.Range("D3")
中的值类似地存储在d3
中的循环外部。For
的{{1}}循环,以便您可以使用i
引用A171
下的表中的单元格rng.Cells(row,i)
模式前后书写多个连续的单元格。