【问题标题】:Open all Excel Workbooks in folder and copy & paste打开文件夹中的所有 Excel 工作簿并复制和粘贴
【发布时间】:2020-10-06 04:47:59
【问题描述】:

我想逐一打开文件夹中的所有 Excel 工作簿并将单元格 B1 复制到活动工作簿中。
引用对吗?

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

【问题讨论】:

    标签: excel vba loops copy-paste


    【解决方案1】:

    一些修复:

    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
    

    【讨论】:

    • 非常感谢您的回答!不幸的是,它仍然无法正常工作。调试器向我显示,跳过了 Do While 循环。因此,我将 StrTyp 更改为 StrTyp="*.csv" 仍然无法正常工作,尽管我在正确的文件夹中有一个文档。你能帮帮我吗?
    • 为我工作 - 找到任何 .xls 文件直接在我的桌面上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多