【问题标题】:Loop through range of cells循环遍历单元格范围
【发布时间】: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

【问题讨论】:

    标签: excel vba for-loop


    【解决方案1】:

    请注意,Dim Rng, cell, cell1, cell2 As Range 仅将 cell2 As Range 声明为 Rng, cell, cell1 在 VBA 中您需要为 每个 变量指定类型,否则默认为 Variant: p>

    Dim Rng As Range, cell As Range, cell1 As Range, cell2 As Range
    

    此外,RngObject,因此必须使用 Set

    Set Rng = Range(Cells(5, 2), Cells(LastRow, LastCol))
    

    最后确保您使用Option Explicit,因为您的变量LastCol1LastCol 未定义,因此被视为0,但不存在列0,计数以1 开头。

    确保使用有意义的变量名。像cellcell1cell2 这样的名字对于每个程序员来说都是一场噩梦。每当你摔倒时,你需要给变量名编号,你可以确定你做错了。

    最后你的变量strFileToOpen 只是一个文件的路径,因为Application.GetOpenFilename 只返回一个String。而String 没有Range,所以strFileToOpen.Range("A10:C10000") 不能工作。

    因此你需要做类似的事情

    Dim OpenedWorkbook As Workbook
    Set OpenedWorkbook = Workbooks.Open(Filename:=strFileToOpen)
    

    并使用

    OpenedWorkbook.Worksheets("SpecifySheetName").Range("A10:C10000")
    

    所以像下面这样

    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
        Dim OpenedWorkbook As Workbook
        Set OpenedWorkbook = Workbooks.Open(Filename:=strFileToOpen)
    End If
    
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("SheetName") 'or ThisWorkbook.ActiveSheet if the sheet name is not defined
    
    Dim LastRow As Long
    LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    
    Dim WorkingRange As Range
    Set WorkingRange = ws.Range(ws.Cells(5, 2), ws.Cells(LastRow, LastCol)) 'LastCol is not defined!
            
    Dim Cell As Range        
    For Each Cell In WorkingRange 
        If Trim(Cell.Value) <> vbNullString Then
            'LastCol is not defined!
            ws.Cells(Cell.Row, LastCol1 + 1) = Application.VLookup(ws.Range("A2:A1000"), OpenedWorkbook.Worksheet("DefineWorksheetName").Range("A10:C10000"), 3)
        End If
    Next Cell
    

    【讨论】:

    • 实际上定义了最后一列变量,但仅包含代码的相关部分以保持代码简短。在为上面的每个变量指定类型之后,仍然存在上面问题中提到的错误。它在 vlookup 行中断。
    • @appleorange 请看看我编辑的答案。
    【解决方案2】:

    我找到了问题的解决方案。请让我知道下面是否有更有效的书写方式

    Cells(cellrng.Row, LastCol1 + 1).Formula = Application.VLookup(Cells(cellrng.Row, 1).Value, Workbooks("FileName.xlsx").Sheets("Sheet1").Range("A1:C10000"), 3, False)
    

    【讨论】:

      猜你喜欢
      • 2018-05-11
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 2014-09-25
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多