0

使用以下代码,我可以同时浏览多个 excel 文件并将它们粘贴到彼此下方的单张纸上,所以我的问题是它复制了所有内容,包括它们的标题,但问题是我只希望它复制第一个文件标题和其余部分它必须只复制数据而不是标题并将其粘贴在彼此下方,因为它们的所有标题都是相同的。

示例: eg1姓名,姓氏,年龄
Kgotso,Smith,20

eg2姓名、姓氏、年龄

布赖恩,棕色,32

结果: 姓名、姓氏、年龄

Kgotso,史密斯,20

布赖恩,棕色,32

Sub Button4_Click()
    Dim fileStr As Variant
    Dim wbk1 As Workbook, wbk2 As Workbook
    Dim ws1 As Worksheet
     fileStr = Application.GetOpenFilename(FileFilter:="microsoft excel files (*.xlsx), *.xlsx", Title:="Get File", MultiSelect:=True)
    Set wbk1 = ActiveWorkbook
    Set ws1 = wbk1.Sheets("Sheet3")

    For i = 1 To UBound(fileStr)
    MsgBox fileStr(i), , GetFileName(CStr(fileStr(i)))
    Set wbk2 = Workbooks.Open(fileStr(i))
    wbk2.Sheets(1).UsedRange.Copy ws1.Cells(ws1.Range("A" & Rows.Count).End(xlUp).Row + 2, 1)
    wbk2.Close
    Next i

End Sub
4

2 回答 2

1

这将是我对此的快速尝试:

Sub Button4_Click()
Dim fileStr As Variant
Dim wbk1 As Workbook, wbk2 As Workbook
Dim ws1 As Worksheet
 fileStr = Application.GetOpenFilename(FileFilter:="microsoft excel files (*.xlsx), *.xlsx", Title:="Get File", MultiSelect:=True)
Set wbk1 = ActiveWorkbook
Set ws1 = wbk1.Sheets("Sheet3")

'handling first file seperately
MsgBox fileStr(1), , GetFileName(CStr(fileStr(1)))
Set wbk2 = Workbooks.Open(fileStr(1))
wbk2.Sheets(1).UsedRange.Copy ws1.Cells(ws1.Range("A" & Rows.Count).End(xlUp).Row + 2, 1)
wbk2.Close

For i = 2 To UBound(fileStr)
MsgBox fileStr(i), , GetFileName(CStr(fileStr(i)))
Set wbk2 = Workbooks.Open(fileStr(i))
'using offset to skip the header - not the best solution,  but a quick one
wbk2.Sheets(1).UsedRange.Offset(1,0).Copy ws1.Cells(ws1.Range("A" & Rows.Count).End(xlUp).Row + 2, 1)
wbk2.Close
Next i

End Sub
于 2012-10-16T07:55:06.913 回答
1

试试这个

If i = 1 then
     ' Do your copy as is
Else
    ' Offset past firt row
    wbk2.Sheets(1).UsedRange.Offset(1, 0).Copy ...
    ' This will copy one blank line too
    ' Too avoid this extra line use instead
    Set rng2 = wbk2.Sheets(1).UsedRange.Offset(1, 0)
    Set rng2 = rng2.Resize(rng2.Rows.Count - 1)
    rng2.Copy ...
End If
于 2012-10-16T07:55:58.283 回答