【发布时间】:2016-07-01 04:02:47
【问题描述】:
我正在使用文件对话框从各种 excel 文件中复制数据并将它们粘贴到单个工作表上。然而,第二个文件会覆盖第一个文件中的数据。第一个数据粘贴在目标工作簿的 A2:E2710 范围内。如果第二组数据范围是 A2:A118,它将覆盖目标工作簿上的 A2:A118。如何让它粘贴而不覆盖以前粘贴的数据?我试过 Selection.Insert Shift:=xlDown 但这并没有粘贴数据。请帮忙。
Sub FASB_Select()
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Set ExcelSheet = CreateObject("Excel.Sheet")
'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.
With fd
'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the action button.
If .Show = -1 Then
Sheets("Data").Select
'Step through each string in the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is a String that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example simply displays the path in a message box.
ThisWorkbook.FollowHyperlink (vrtSelectedItem)
'Clear CutCopyMode
Application.CutCopyMode = False
'Wait some time
Application.Wait Now + TimeValue("00:00:01") ' wait 3 seconds
DoEvents
'IN Excel :
'SELECT ALL
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'EXIT (Close & Exit)
Application.Wait Now + TimeValue("00:00:01") ' wait 3 seconds
ActiveWorkbook.Close SaveChanges:=False
'Wait some time
Application.Wait Now + TimeValue("00:00:01") ' wait 3 seconds
Range("A2").Select
'Paste
ActiveSheet.Paste
Next vrtSelectedItem 'Loop for each file selected in the file dialog box
'Exit if the user pressed Cancel
Else
Exit Sub
End If
End With
'Set the object variable to Nothing
Set fd = Nothing
End Sub
【问题讨论】:
-
每个部分应该粘贴到哪里?