以下VBA代码已经存在二定义名称,帮我重新改进一下 Private Sub Worksheet_Change(ByVal Target As Range) Dim i As Integer Dim j As Integer Dim k As Integer Dim ws As Worksheet Set ws = ActiveSheet If Target.Address = "$B$2" Then For i = 9 To 40 For j = 2 To 7 If ws.Cells(i, j).Value = ws.Cells(8, 5).Value Then For k = 3 To 4 ws.Cells(i, j + k - 2).Value = ws.Cells(Application.Match(ws.Cells(i, 1).Value, ws.Range("AK9:AK40"), 0) + 8, k).Value Next k End If Next j Next i End If End Sub
时间: 2023-06-18 10:05:59 浏览: 539
vbA_自学文本
以下是更新后的代码,避免了二次定义名称的问题:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim ws As Worksheet
Set ws = ActiveSheet
If Target.Address = "$B$2" Then
For i = 9 To 40
For j = 2 To 7
If ws.Cells(i, j).Value = ws.Cells(8, 5).Value Then
For k = 3 To 4
ws.Cells(i, j + k - 2).Value = ws.Cells(Application.Match(ws.Cells(i, 1).Value, ws.Range("AK9:AK40"), 0) + 8, k).Value
Next k
End If
Next j
Next i
End If
End Sub
阅读全文