首页 > 解决方案 > 仅从第一个工作簿复制粘贴标题

问题描述

我想知道是否有人可以帮助我提供代码。我正在尝试仅复制和粘贴第一个工作簿的标题(它有多个工作表,所有工作簿中的所有标题/格式都相同,但数据不同)。工作簿处于循环中。我不知道如何仅从循环中的第一个工作簿中复制标题。如果有人可以帮助我,那就太好了!非常感激!

我在这里有以下代码:

Sub simpleXLSMerger()
'Just change directory path and run code. Make sure workbook is not in the same direct as files to be merge
'Remember to add the same number of worksheets same as merge files
'If code does not work, or paste blanks into worksheet, try inserting code into a new module
Dim bookList As Workbook
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Dim i As Integer
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")

DirectoryPath = InputBox("Directory Path")

For i = 1 To Sheets.Count
    'change folder path of excel files here
    Set dirObj = mergeObj.Getfolder(DirectoryPath)
    Set filesObj = dirObj.Files
    For Each everyObj In filesObj
    Set bookList = Workbooks.Open(everyObj)
    'Change number to the source sheet number you want to import (starts with 1)
    Worksheets(i).Activate

    Range("A2", Range("A1").End(xlDown).End(xlToRight)).Copy
    'Change number to the destination sheet number you want to import to (starts with 1)
    ThisWorkbook.Worksheets(i).Activate
    Application.ScreenUpdating = True

    'Do not change the following column. It's not the same column as above
    Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    bookList.Close SaveChanges:=False
    Next
Next i

MsgBox "DONE! =)"

End Sub

标签: excelheadercopypaste

解决方案


推荐阅读