【问题标题】:Excel VBA - loop over files in folder, copy range, paste in this workbookExcel VBA - 遍历文件夹中的文件,复制范围,粘贴到此工作簿中
【发布时间】:2017-06-08 11:00:11
【问题描述】:

我有 500 个带有数据的 excel 文件。我会将所有这些数据合并到一个文件中。

实现这一目标的任务列表:

  1. 我想遍历文件夹中的所有文件
  2. 打开文件,
  3. 复制此范围“B3:I102”
  4. 将其粘贴到活动工作簿的第一张表中
  5. 重复但在下面粘贴新数据

我已经完成了任务 1-4,但我需要任务 5 的帮助,最后一点 - 将数据粘贴到现有数据下并使其成为动态数据。我在代码中用 '#### 突出显示了这一点。

这是我根据其他人的问题整理的代码:)

关于如何做到这一点的任何建议?

Sub LoopThroughFiles()
Dim MyObj As Object, 
MySource As Object, 
file As Variant
Dim wbThis                  As Workbook     'workbook where the data is to be pasted, aka Master file
Dim wbTarget                As Workbook     'workbook from where the data is to be copied from, aka Overnights file
Dim LastRow As Long
Dim sht1 As Worksheet
Dim sht2 As Worksheet

'set to the current active workbook (the source book, the Master!)
Set wbThis = ActiveWorkbook
Set sht1 = wbThis.Sheets("Sheet1")

Folder = "\\dne\ldc\research-dept\3 CEEMEA\15. EMB\Turkey\TLC Overnight & Weekly Reports\weekly (majeed)\"
Fname = Dir(Folder)

While (Fname <> "")

  Set wbTarget = Workbooks.Open(Filename:=Folder & Fname)
  wbTarget.Activate
  Range("b3:i102").Copy

  wbThis.Activate

  '################################
  'NEED HELP HERE. I GET A ERROR HERE. NEEDS TO BE MORE DYNAMIC.
  sht1.Range("b1:i100").PasteSpecial

 Fname = Dir

 'close the overnight's file
  wbTarget.Close
 Wend

End Sub

【问题讨论】:

    标签: vba excel loops copy-paste


    【解决方案1】:

    我认为使用变体比复制方法有用。

    Sub LoopThroughFiles()
    
    Dim MyObj As Object, MySource As Object
    
    file As Variant
    Dim wbThis                  As Workbook     'workbook where the data is to be pasted, aka Master file
    Dim wbTarget                As Workbook     'workbook from where the data is to be copied from, aka Overnights file
    Dim LastRow As Long
    Dim sht1 As Worksheet
    Dim sht2 As Worksheet
    
    Dim vDB As Variant
    
    'set to the current active workbook (the source book, the Master!)
    Set wbThis = ActiveWorkbook
    Set sht1 = wbThis.Sheets("Sheet1")
    
    Folder = "\\dne\ldc\research-dept\3 CEEMEA\15. EMB\Turkey\TLC Overnight & Weekly Reports\weekly (majeed)\"
    Fname = Dir(Folder)
    
    While (Fname <> "")
    
      Set wbTarget = Workbooks.Open(Filename:=Folder & Fname)
    
      vDB = wbTarget.Sheets(1).Range("b3:i102")
    
      '################################
      'NEED HELP HERE. I GET A ERROR HERE. NEEDS TO BE MORE DYNAMIC.
    
            sht1.Range("b" & Rows.Count).End(xlUp)(2).Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB
    
     Fname = Dir
    
     'close the overnight's file
      wbTarget.Close
     Wend
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      我看到您已经为此添加了一个长变量,因此请在粘贴之前查找最后一行。此外,如果数据量不同,请粘贴到单个单元格中。

      我修改了你的脚本如下。

      Sub LoopThroughFiles()
      Dim MyObj As Object, 
      MySource As Object, 
      file As Variant
      Dim wbThis                  As Workbook     'workbook where the data is to be pasted, aka Master file
      Dim wbTarget                As Workbook     'workbook from where the data is to be copied from, aka Overnights file
      Dim LastRow As Long
      Dim sht1 As Worksheet
      Dim sht2 As Worksheet
      
      'set to the current active workbook (the source book, the Master!)
      Set wbThis = ActiveWorkbook
      Set sht1 = wbThis.Sheets("Sheet1")
      
      Folder = "\\dne\ldc\research-dept\3 CEEMEA\15. EMB\Turkey\TLC Overnight & Weekly Reports\weekly (majeed)\"
      Fname = Dir(Folder)
      
      While (Fname <> "")
      
        Set wbTarget = Workbooks.Open(Filename:=Folder & Fname)
        wbTarget.Activate
        Range("b3:i102").Copy
      
        wbThis.Activate
      
       'Just add this line:
        lastrow = sht1.Range("b1").End(xlDown).Row + 1
       'And alter this one as follows:
        sht1.Range("B" & lastrow).PasteSpecial
      
       Fname = Dir
      
       'close the overnight's file
        wbTarget.Close
       Wend
      
      End Sub
      

      【讨论】:

        【解决方案3】:

        您如何将sht1.Range("b1:i102") 定义为变量而不是常量?

        类似:

        Dim x As Long
        Dim y As Long
        x = 1
        y = 1
        Dim rng As Range
        Set rng = Range("b"&x ,"i"&y)
        

        然后使用:

        sht1.rng
        

        请记住在您的 while 语句末尾添加 x = x+100 and y = y +100(这样它会在每次粘贴之间更新新值。)

        【讨论】:

          【解决方案4】:

          你为什么不放置一个柜台?像这样:

          Dim counter As Long
          counter = 1
          

          然后:

          While (Fname <> "")
          
                Set wbTarget = Workbooks.Open(Filename:=Folder & Fname)
                wbTarget.Activate
                Range("b3:i102").Copy
          
                wbThis.Activate
          
          
                'Solution:
          
                sht1.Range("b" & counter & ":i" & counter + 99).PasteSpecial
                counter = counter + 100
          
                Fname = Dir
          
               'close the overnight's file
               wbTarget.Close
          Wend
          

          【讨论】:

            【解决方案5】:

            您可以在第 5 步中添加以下部分。我使用了在循环中递增变量的偏移量

            Dim i as Long
            Range("B1").Select     // 'select the column where you want to paste value
            ActiveCell.Offset(i, 0).Select     //'place the offset counter with variable 
            sht1.Range("b1:i100").PasteSpecial
            i=i+100     //'increment the offset with the number of data rows 
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-11-05
              • 1970-01-01
              • 1970-01-01
              • 2017-07-26
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多