简体   繁体   English

打开文件夹中的所有 Excel 工作簿并复制和粘贴

[英]Open all Excel Workbooks in folder and copy & paste

I want to open all Excel workbooks in a folder, one-by-one and copy the cell B1 into the active workbook.我想一一打开文件夹中的所有 Excel 工作簿,然后将单元格 B1 复制到活动工作簿中。
Are the references right?参考文献正确吗?

Sub CopyPaste
    Const strVerzeichnis As String = "C:\Users\amke\Desktop"
    Dim StrDatei As String
    Const StrTyp As String = "*.xls"
    Dim Dateiname As String
    
    ThisWorkbook.Activate
    Dateiname = Dir(strVerzeichnis & StrTyp)
    Application.ScreenUpdating = False

    Do While Dateiname <> ""
        Workbooks.Open Filename:=strVerzeichnis & Dateiname

        Workbooks(Filename).Worksheets("sheet1").Cells("B1").Copy _
          Workbooks(ThisWorkbook).Worksheets("sheet1").Range("B1")
    Loop

    Application.ScreenUpdating = True
End Sub

A few fixes:一些修复:

Sub CopyPaste
    Const strVerzeichnis As String = "C:\Users\amke\Desktop\" 'Add trailing \
    Dim StrDatei As String
    Const StrTyp As String = "*.xls"
    Dim Dateiname As String, rngPaste As Range
    
    Set rngPaste = ThisWorkbook.Worksheets("sheet1").Range("B1")
    
    Application.ScreenUpdating = False
    
    Dateiname = Dir(strVerzeichnis & StrTyp)
    Do While Dateiname <> ""
        With Workbooks.Open(Filename:=strVerzeichnis & Dateiname)
            .Worksheets("sheet1").Cells("B1").Copy rngPaste
            Set rngPaste = rngPaste.offset(1, 0) 'next paste location
            .Close False                         'no save
        End with
        Dateiname = Dir() 'next file, if any 
    Loop
    Application.ScreenUpdating = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM