【问题标题】:Looping through multiple Sheets in Multiple Workbooks循环浏览多个工作簿中的多个工作表
【发布时间】:2016-10-20 04:47:42
【问题描述】:

我的宏需要遍历工作表“AtualizaABS”中的这个范围,其中包含宏工作所需的数据:

  1. 宏必须检查范围中的 F 列,以识别当前工作簿中要将数据粘贴到的工作表名称(代码中的变量“Destino”)。

  2. 完成此操作后,宏继续打开一个新文件夹,它将在其中搜索名称与 E 列中的值匹配的工作簿(代码中的变量“ABSid”)。

  3. 识别工作簿后,宏必须复制工作表中名称与 G 列中的值匹配的所有单元格(代码中的变量“Dados”),然后将新打开的工作簿中的数据粘贴到原始的(恰好在由变量“Destino”和 F 列确定的工作表中)。

该代码适用于范围的第一行,但是当循环遍历工作表“AtualizaABS”中的其他条件和要打开的其他工作簿时,它会失败(即使我使用了“For each”命令)。

如何使宏循环遍历我范围内的行,然后遍历代码确定的文件夹中的工作簿?

Sub CopyThenPaste()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet
Dim PasteStart As Range
On Error GoTo Errorcatch

'States the number of the last row thtat contains relevant information to the Macro
ultima_linha = Range("e2", Range("e2").End(xlDown)).Rows.Count

'Selects the data to be used in the Macro
Worksheets("AtualizaABS").Activate
For i = 2 To ultima_linha + 1
Destino = ActiveSheet.Cells(i, 6).Value
Dados = ActiveSheet.Cells(i, 7).Value
ABSid = ActiveSheet.Cells(i, 5).Value

'Selects all of the cells of the worksheet that is going to be updated
    Set wb1 = ActiveWorkbook
    For Each Sheet In wb1.Worksheets
    Set PasteStart = Worksheets(Destino).[A1]
    Sheets(Destino).Select
    Cells.Select

'Asks the user what is the folder where VBA should look for the Workbook with the new information
    With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Por favor escolha uma pasta"
    .AllowMultiSelect = False
    If .Show = -1 Then Pasta = .SelectedItems(1)
    End With


'Opens the new workbook, copies and then pastes the data in the current Workbook
    For Each wb2 In Workbooks
    Set wb2 = Workbooks.Open(Filename:=Pasta & "\" & ABSid & ".xls")
    wb2.Sheets(Dados).Select
    Cells.Select
    Selection.Copy
    wb1.Worksheets(Destino).Paste Destination:=PasteStart

    Application.CutCopyMode = False
    wb2.Close


    Next

    Next


Next


Exit Sub
Errorcatch:
MsgBox Err.Description


End Sub

感谢您的关注。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您不需要遍历所有 Workbook 对象或所有 Worksheet 对象,因此您的代码可以简化为:

    Sub CopyThenPaste()
    
        Dim wb1 As Workbook
        Set wb1 = ActiveWorkbook
    
        Dim wsAtualizaABS As Worksheet
        Set wsAtualizaABS = wb1.Worksheets("AtualizaABS")
    
        Dim wb2 As Workbook
    
        Dim Destino As String
        Dim Dados As String
        Dim ABSid As String
        Dim Pasta As String
    
        On Error GoTo Errorcatch
    
        'States the number of the last row that contains relevant information to the Macro
        ultima_linha = wsAtualizaABS.Range("e2").End(xlDown).Row
    
        For i = 2 To ultima_linha
            Destino = wsAtualizaABS.Cells(i, 6).Value
            Dados = wsAtualizaABS.Cells(i, 7).Value
            ABSid = wsAtualizaABS.Cells(i, 5).Value
    
    '********************
    '**** This block of code can probably be executed outside the loop,
    '**** unless the path to each workbook is different
            'Asks the user what is the folder where VBA should look for the Workbook with the new information
            With Application.FileDialog(msoFileDialogFolderPicker)
                .Title = "Por favor escolha uma pasta"
                .AllowMultiSelect = False
                If .Show = -1 Then Pasta = .SelectedItems(1)
            End With
    '********************
    
            'Opens the new workbook, copies and then pastes the data in the current Workbook
            Set wb2 = Workbooks.Open(Filename:=Pasta & "\" & ABSid & ".xls")
            wb2.Sheets(Dados).Cells.Copy Destination:=wb1.Worksheets(Destino).Range("A1")
            wb2.Close
    
        Next
    
        Exit Sub
    
    Errorcatch:
        MsgBox Err.Description
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 2014-09-15
      • 2018-05-27
      • 2016-11-25
      相关资源
      最近更新 更多