从选定工作簿的多个工作表中的特定区域获取数据
Get data from specific areas in multiple sheets in selected workbooks
我必须制作一个允许我从特定区域收集数据的宏,例如,一个 sheet 中的 A1-Ax 和 G1-Gx 以及另一个 sheet 中的 B1-Bx依此类推,在许多不同的工作簿中合并为一个主要 excel sheet。我喜欢 5-6 Excel 个文件,我必须从中收集数据,它们都包含大约 4-5 个工作sheet。
使用下面的代码,我可以在选定的 Workbooks
.
中收集每个 Worksheet
中的所有数据
但我必须收集的数据来自特定的 Range
,每个 Worksheet
and/or Workbook
.
到目前为止我的代码如下所示:
Function LastUsedCell(wks As Excel.Worksheet) As Excel.Range
With wks
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
Set LastUsedCell = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
End If
End With
End Function
Function GetNextRowStart(wks As Excel.Worksheet) As Excel.Range
Dim lastCell As Excel.Range
Dim nextRow As Integer
nextRow = 1
Set lastCell = LastUsedCell(wks)
If Not lastCell Is Nothing Then nextRow = lastCell.Row + 1
Set GetNextRowStart = wks.Cells(nextRow, 1)
End Function
Sub Multi()
Dim outputWorkbook As Excel.Workbook
Dim outputWorksheet As Excel.Worksheet
Dim filepath As Variant
Set outputWorkbook = Workbooks.Open("C:\Users\z003k50s\Desktop\Test\Output.xlsx")
Set outputWorksheet = outputWorkbook.Sheets("Sheet1")
For Each filepath In Application.GetOpenFilename(FileFilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
Dim wkbk As Excel.Workbook
Dim wks As Excel.Worksheet
Set wkbk = Workbooks.Open(filepath, , True)
For Each wks In wkbk.Sheets
Dim sourceRange As Excel.Range
Dim outputRange As Excel.Range
With wks
Set sourceRange = .Range(.Cells(1, 1), LastUsedCell(wks))
End With
Set outputRange = GetNextRowStart(outputWorksheet)
sourceRange.Copy outputRange
Next
Next
outputWorksheet.Columns.AutoFit
End Sub
我知道这听起来工作量很大,但只需通过复制和粘贴对其进行硬编码即可。这绝对不是最好的方法,但它会完成工作。
我必须制作一个允许我从特定区域收集数据的宏,例如,一个 sheet 中的 A1-Ax 和 G1-Gx 以及另一个 sheet 中的 B1-Bx依此类推,在许多不同的工作簿中合并为一个主要 excel sheet。我喜欢 5-6 Excel 个文件,我必须从中收集数据,它们都包含大约 4-5 个工作sheet。
使用下面的代码,我可以在选定的 Workbooks
.
Worksheet
中的所有数据
但我必须收集的数据来自特定的 Range
,每个 Worksheet
and/or Workbook
.
到目前为止我的代码如下所示:
Function LastUsedCell(wks As Excel.Worksheet) As Excel.Range
With wks
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
Set LastUsedCell = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
End If
End With
End Function
Function GetNextRowStart(wks As Excel.Worksheet) As Excel.Range
Dim lastCell As Excel.Range
Dim nextRow As Integer
nextRow = 1
Set lastCell = LastUsedCell(wks)
If Not lastCell Is Nothing Then nextRow = lastCell.Row + 1
Set GetNextRowStart = wks.Cells(nextRow, 1)
End Function
Sub Multi()
Dim outputWorkbook As Excel.Workbook
Dim outputWorksheet As Excel.Worksheet
Dim filepath As Variant
Set outputWorkbook = Workbooks.Open("C:\Users\z003k50s\Desktop\Test\Output.xlsx")
Set outputWorksheet = outputWorkbook.Sheets("Sheet1")
For Each filepath In Application.GetOpenFilename(FileFilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
Dim wkbk As Excel.Workbook
Dim wks As Excel.Worksheet
Set wkbk = Workbooks.Open(filepath, , True)
For Each wks In wkbk.Sheets
Dim sourceRange As Excel.Range
Dim outputRange As Excel.Range
With wks
Set sourceRange = .Range(.Cells(1, 1), LastUsedCell(wks))
End With
Set outputRange = GetNextRowStart(outputWorksheet)
sourceRange.Copy outputRange
Next
Next
outputWorksheet.Columns.AutoFit
End Sub
我知道这听起来工作量很大,但只需通过复制和粘贴对其进行硬编码即可。这绝对不是最好的方法,但它会完成工作。