仅从第一个工作簿复制粘贴标题

时间:2018-09-24 14:15:24

标签: excel header copy paste

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

我在这里有以下代码:

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

0 个答案:

没有答案