【发布时间】:2020-12-02 06:54:50
【问题描述】:
代码旨在遍历指定范围内的所有单元格。下面是部分代码。在 for 循环中使用 Rng 似乎有错误。当Rng 被替换为指定范围时,例如Range("A1:B20"),它可以工作,但是代码在
Cells(cell2.Row, LastCol1 + 1) = Application.VLookup(Range("A2:A1000"), strFileToOpen.Range("A10:C10000"), 3)
Dim Rng, cell, cell1, cell2 As Range
Dim strFileToOpen As Variant
strFileToOpen = Application.GetOpenFilename _
(Title:="Please choose the File", _
FileFilter:="Excel Files *.xls* (*.xls*),")
If strFileToOpen = False Then
MsgBox "No file selected.", vbExclamation, "Sorry!"
Exit Sub
Else
Workbooks.Open Filename:=strFileToOpen
End If
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Rng = Range(Cells(5, 2), Cells(LastRow, LastCol))
For Each cell2 In Rng
If Trim(cell2.Value) <> "" Then
Cells(cell2.Row, LastCol1 + 1) = Application.VLookup(Range("A2:A1000"), strFileToOpen.Range("A10:C10000"), 3)
End If
Next
【问题讨论】: