Excel VBA - 在 Do-While 循环中完成另一个循环操作

Excel VBA - Completing another Loop Operation within a Do-While Loop

我目前正在通过反复试验、宏记录器和在线 reading/researching 自学 VBA。在大多数情况下,我能够找到解决方法来填补空白。但在这种情况下,我有点挣扎。

我的问题是我在 do-while 循环中打开(变量命名工作簿),然后我需要 select 一个变量行,但静态列数据集,然后将其粘贴到具有指定 sheet 的模型。

我想通过以下步骤运行打开每个工作簿:

  1. 找到 active/opened 工作簿的最后一个空行
  2. 复制该数据
  3. 查找指定模型的最后一个空行 sheet。

问题出在上面的第 3 步。虽然我要求代码找到最后一行,但它似乎是 "ignoring" 并将数据粘贴到模型中的 A2 处。

我试过的

  1. 我想避免使用 used.range 选项,因为我的每个源数据在所有工作簿上都有 headers,在之前的测试中,我无法获得与 used.range 相同的范围我可以使用 lastRow、lastColumn 选项。
  2. 使用 byref,以便我可以调用复制的数据 - 但即使阅读了它,我仍在努力理解如何使用它。
  3. 在循环内使用具有指定范围的 wbSource 和 wbDest,我想避免这种情况,除非它可以被解释或提供关于如何在循环中使用它的示例,就像我一样'我一直无法让它工作。

    Sub GetAllFileNames()
    
    Dim Folder As String
    Dim FullFileName As String
    Dim FileStart As String
    Dim wbThis, wbSource As Workbook
    Dim lastRow As Long
    Dim wshtDest As Worksheet
    
    Set wbThis = ThisWorkbook
    Set wshtDest = wbThis.Sheets("Report")
    lastRow = wshtDest.Cells(wshtDest.Rows.Count, 1).End(xlUp).Offset(lastRow).row + 1
    'rlastRow = ReportSht.Cells(ReportSht.Rows.Count, 1).End(xlUp).Offset(lastRow).row
    
    Folder = Sheet1.Range("C7") & "\"
    
    FileStart = "D1*"
    FullFileName = Dir(Folder & FileStart)
    
    On Error Resume Next
    Do While FullFileName <> ""
    
        Workbooks.Open Folder & FullFileName
        Call source_rng
        For Each copied_rng In Selection
            With wshtDest
            .Activate
            .Cells(lastRow, 1).Select
            .PasteSpecial
            End With
        Next copied_rng
    '    wshtDest.Range("A" & rlastRow).Offset(1).Select
        Debug.Print lastRow
        Debug.Print FullFileName
        FullFileName = Dir() ' moves to next file
        DoEvents     'Ensure Workbook has opened before moving on to next line of code
        Application.CutCopyMode = False
    Loop
    
    End Sub
    
    
    Sub source_rng()
    
    Dim sht As Worksheet
    Dim lastRow, lastColumn As Long
    Dim StartCell, copied_rng As Range
    
    Set sht = ActiveSheet
    Set StartCell = Range("A2")
    
    'Find Last Row and Column
      lastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).row
      lastColumn = sht.Cells(StartCell.row, sht.Columns.Count).End(xlToLeft).Column
    
    'Select Range
    sht.Range(StartCell, sht.Cells(lastRow, lastColumn)).Copy
    
    End Sub
    

目前,上述代码不会产生任何错误,但是当删除循环并打开 1 个 spreadsheet 并将其复制并粘贴到模型中时,模型中的 lastRow 查找工作正常。

但是,当循环就位时,每个打开的工作簿的数据都被复制到模型中的 A2 处,它应该找到下一个空行并在该点粘贴源数据。

我认为问题在于您在循环外定义了 lastrow。这意味着它被填充了 1 次(可能是第 2 行),并且从未更新过。您将不得不为 for 循环的每次迭代重新计算它。

所以像这样:

    For Each copied_rng In Selection
        lastRow = wshtDest.Cells(wshtDest.Rows.Count, 1).End(xlUp).row + 1
        With wshtDest
        .Activate
        .Cells(lastRow, 1).Select
        .PasteSpecial
        End With
    Next copied_rng

既然你说你还在学习;有一种方法可以检查您的变量是否填充了您期望的值。您可以使用 F8 单步执行代码并在 Watch window 中跟踪它们:https://bettersolutions.com/vba/debugging/watch-window.htm

希望对您有所帮助。