我有以下工作正常的宏。我一直在尝试在代码中指定源单元格(行和列)和目标单元格(行和列),但到目前为止,我没有成功。
Sub mainx()
Dim r As Range, c As Range
With Cells(1).CurrentRegion
With .Offset(1).Resize(.Rows.Count - 1)
For Each r In .Cells
Set c = .Find(r.Value, r, , 1, , , 2)
If (c.Address <> r.Address) Then
If c.Row > r.Row Then
r.Offset(, 40) = c.Row - r.Row - 1
Else
Do
Set c = .FindNext(c)
Loop While c.Row = r.Row And c.Address <> r.Address
If c.Row > r.Row Then
r.Offset(, 40) = c.Row - r.Row - 1
Else
r.Offset(, 40) = "na"
End If
End If
Else
r.Offset(, 40) = "na"
End If
Next
End With
End With
End Sub
当前代码在第二行和第40列之后生成结果,并且代码将对所有值起作用直到第40列。
我需要使代码对A15:N100
(源单元格)之间的数据起作用,并在AO15:BB100
(目标单元格)中输出结果。这样,介于两者之间的值将不受代码的影响。
请问我该如何实现?我可以帮忙吗?非常感谢!
答案 0 :(得分:0)
这是答案。
Sub mainx()
Dim r As Range, c As Range
With Range("A15:N100")
For Each r In .Cells
Set c = .Find(r.Value, r, , 1, , , 2)
If (c.Address <> r.Address) Then
If c.Row > r.Row Then
r.Offset(, 40) = c.Row - r.Row - 1
Else
Do
Set c = .FindNext(c)
Loop While c.Row = r.Row And c.Address <> r.Address
If c.Row > r.Row Then
r.Offset(, 40) = c.Row - r.Row - 1
Else
r.Offset(, 40) = "na"
End If
End If
Else
r.Offset(, 40) = "na"
End If
Next
End With
End