我尝试通过浏览和选择多个工作簿来合并工作簿并获取当前工作簿中的所有数据。我需要1张所选工作簿的所有数据。但是我的代码提供了当前工作簿的不同表格。 sheets.Copy after:= mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)根据此行,语法允许我选择之前或之后但不提供当前。请帮助我
element(by.cssContainingText('.customer-name-link', 'Auto Opportunity')).click();
答案 0 :(得分:1)
正如您所发现的, Sheets.Copy 将复制或移动整个工作表。它不会将数据合并到另一个工作表中。您必须复制要复制的工作表的单元格
dim dest as Range
For i = 1 To dailogbox.SelectedItems.Count
Workbooks.Open dailogbox.SelectedItems(i)
Set sourceWorkbook = Workbooks.Open(dailogbox.SelectedItems(i))
For Each aSheet In sourceWorkbook.Worksheets '
set dest = mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
aSheet.Cells.Copy dest.Cells
Next sheets ' NOT "tempWorkSheet"
sourceWorkbook.Close
Next i
另外:"表格"是一个保留字。您不能将其用作变量。我将其更改为" aSheet"。
编辑:要在复制文本后复制格式,请在aSheet.Cells.Copy dest.Cells
:
dest.PasteSpecial Paste:=xlPasteFormats
答案 1 :(得分:0)
这将打开一个文件对话框,允许您选择多个文件,然后循环浏览工作簿中的每个工作表,将数据从A2复制到数据的右下角,并将其粘贴到托管的工作簿中这段代码。
您需要修改或修改的内容:
1)托管此代码的图书的图纸名称
2)Col跨度(现在为A-Z)
3)如果您的导入图书有多张图纸,则需要为要导入的图纸设置标准,因为这样可以抓取每个选定工作簿中的每张图纸。
4)这假设Col A没有任何空白(确定最后一行(要复制的范围),你需要选择一个最不可能有空白的列,这样你就不会错过数据。
Option Explicit
Sub Consolidation()
Dim CurrentBook As Workbook
Dim WS As Worksheet
Set WS = ThisWorkbook.Sheets("SHEETNAME?")
Dim IndvFiles As FileDialog
Dim FileIdx As Long
Dim i As Integer, x As Integer
Set IndvFiles = Application.FileDialog(msoFileDialogOpen)
With IndvFiles
.AllowMultiSelect = True
.Title = "Multi-select target data files:"
.ButtonName = ""
.Filters.Clear
.Filters.Add ".xlsx files", "*.xlsx"
.Show
End With
Application.DisplayAlerts = False
Application.ScreenUpdating = False
For FileIdx = 1 To IndvFiles.SelectedItems.Count
Set CurrentBook = Workbooks.Open(IndvFiles.SelectedItems(FileIdx))
For Each Sheet In CurrentBook.Sheets
Dim LRow1 As Long
LRow1 = WS.Range("A" & WS.Rows.Count).End(xlUp).Row
Dim LRow2 As Long
LRow2 = CurrentBook.ActiveSheet.Range("A" & CurrentBook.ActiveSheet.Rows.Count).End(xlUp).Row
Dim ImportRange As Range
Set ImportRange = CurrentBook.ActiveSheet.Range("A2:Z" & LRow2)
ImportRange.Copy
WS.Range("A" & LRow1 + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Next
CurrentBook.Close False
Next FileIdx
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
答案 2 :(得分:0)
也许你在此之后(评论中的解释):
Option Explicit
Sub CopySheets()
Dim files As Variant, i As Long
Dim dailogbox As FileDialog
Dim mySheet As Worksheet, targetSheet As Worksheet
Set targetSheet = ActiveSheet ' set the sheet you want to collect selecte workbooks worksheets data into
Set dailogbox = Application.FileDialog(msoFileDialogFilePicker)
dailogbox.AllowMultiSelect = True
files = dailogbox.Show
For i = 1 To dailogbox.SelectedItems.Count
With Workbooks.Open(dailogbox.SelectedItems(i)) ' open and reference current workbook
For Each mySheet In .Worksheets ' loop through current workbook worksheets
mySheet.UsedRange.Copy targetSheet.Cells(targetSheet.Rows.Count, 1).End(xlUp).Offset(1) ' copy current worksheet "used" range and paste them into target sheet from its column A first empty cell after last not empty one
Next
.Close False ' cloe current workbook, discarding changes
End With
Next
End Sub