对于每个循环都没有按预期运行
For each loop does not operate as expected
我在 excel 中有两张纸,一张是一块板,里面有几个单元格,里面有数字,另一张是参考资料(上面有数字),我需要在同一行写单元格所在的引用。
image of the first board where are the references
image of the excel sheet that i have to write the location of each reference
my vba code
示例:
arm8.png 是电路板,local.png 是我写单元格去本地化的地方
Option Explicit
Sub ciclo()
Dim FindString As String
Dim Rng As Range
Dim matrixVal As Range
Set matrixVal = Sheets("Localizações").Range("B1")
FindString = matrixVal
For Each Rng In matrixVal
If Trim(FindString) <> "" Then
With Sheets("Arm8").Range("A1:J10")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
'Application.Goto Rng, False
'MsgBox Rng.Column & " - " & Rng.Row
Else
MsgBox "Nothing found"
End If
End With
With Sheets("Localizações")
.Range("C1:C9").Value = Rng.Column
.Range("D1:D9").Value = Rng.Row
End With
End If
Next Rng
End Sub
我希望 local.png 中的输出是 C 列和 D
2 - 9
2 - 7
2 - 8
2 - 4
5 - 4
7 - 4
5 - 9
9 - 7
9 - 0
首先,正如我在评论中所说的那样:
Set matrixVal = Sheets("Localizações").Range("B1")
将 matrixVal
设置为一个单元格(准确地说是 B1),因此您的 For-Each
循环除了这个单元格之外没有任何要循环的单元格,因此它只会 运行一次。
其次,FindString
需要在循环内更新,否则您将一遍又一遍地搜索相同的值。
最后,您不应该更新循环内的 Rng
变量,因为您已经在使用它来循环遍历一个范围。您需要 Range
.
类型的第二个变量
您的代码应如下所示:
Sub ciclo()
Dim FindString As String
Dim Rng As Range
Dim cell As Range
Dim matrixVal As Range
Set matrixVal = ThisWorkbook.Worksheets("Localizacoes").Range("B1:B9")
For Each cell In matrixVal
FindString = cell.Value
If Trim(FindString) <> "" Then
With ThisWorkbook.Worksheets("Arm8").Range("A1:J10")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
With ThisWorkbook.Worksheets("Localizacoes")
.Cells(cell.Row, "C").Value = Rng.Column
.Cells(cell.Row, "D").Value = Rng.Row
End With
Else
MsgBox "Nothing found"
End If
End With
End If
Next cell
End Sub
我在 excel 中有两张纸,一张是一块板,里面有几个单元格,里面有数字,另一张是参考资料(上面有数字),我需要在同一行写单元格所在的引用。
image of the first board where are the references
image of the excel sheet that i have to write the location of each reference
my vba code
示例:
arm8.png 是电路板,local.png 是我写单元格去本地化的地方
Option Explicit
Sub ciclo()
Dim FindString As String
Dim Rng As Range
Dim matrixVal As Range
Set matrixVal = Sheets("Localizações").Range("B1")
FindString = matrixVal
For Each Rng In matrixVal
If Trim(FindString) <> "" Then
With Sheets("Arm8").Range("A1:J10")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
'Application.Goto Rng, False
'MsgBox Rng.Column & " - " & Rng.Row
Else
MsgBox "Nothing found"
End If
End With
With Sheets("Localizações")
.Range("C1:C9").Value = Rng.Column
.Range("D1:D9").Value = Rng.Row
End With
End If
Next Rng
End Sub
我希望 local.png 中的输出是 C 列和 D
2 - 9
2 - 7
2 - 8
2 - 4
5 - 4
7 - 4
5 - 9
9 - 7
9 - 0
首先,正如我在评论中所说的那样:
Set matrixVal = Sheets("Localizações").Range("B1")
将 matrixVal
设置为一个单元格(准确地说是 B1),因此您的 For-Each
循环除了这个单元格之外没有任何要循环的单元格,因此它只会 运行一次。
其次,FindString
需要在循环内更新,否则您将一遍又一遍地搜索相同的值。
最后,您不应该更新循环内的 Rng
变量,因为您已经在使用它来循环遍历一个范围。您需要 Range
.
您的代码应如下所示:
Sub ciclo()
Dim FindString As String
Dim Rng As Range
Dim cell As Range
Dim matrixVal As Range
Set matrixVal = ThisWorkbook.Worksheets("Localizacoes").Range("B1:B9")
For Each cell In matrixVal
FindString = cell.Value
If Trim(FindString) <> "" Then
With ThisWorkbook.Worksheets("Arm8").Range("A1:J10")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
With ThisWorkbook.Worksheets("Localizacoes")
.Cells(cell.Row, "C").Value = Rng.Column
.Cells(cell.Row, "D").Value = Rng.Row
End With
Else
MsgBox "Nothing found"
End If
End With
End If
Next cell
End Sub