我正在使用文件对话框从各种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
答案 0 :(得分:0)
在名为“Data”的工作表中,在包含宏的wb中。我希望后续复制的数据自动粘贴到最后一个活动单元格下面的单元格中。