Excel 将多张工作表中的所有数据填入一张工作表
目标是用表2、3、4等中的所有数据完全填充表1 源工作表可能有不同数量的数据行,但是,行中没有间隙(即,其中包含数据的最后一行是该工作表中数据的最后一行)。工作簿中有固定数量的工作表Excel 将多张工作表中的所有数据填入一张工作表,excel,vba,Excel,Vba,目标是用表2、3、4等中的所有数据完全填充表1 源工作表可能有不同数量的数据行,但是,行中没有间隙(即,其中包含数据的最后一行是该工作表中数据的最后一行)。工作簿中有固定数量的工作表 从第1行第2页第1行开始,将数据填入第1页 从表3第1行的下一个打开行开始,将数据填入表1 重复此操作,直到所有图纸都复制到图纸1中 基本上,所有其他工作表都将用作工作区,然后创建最终的连续工作表,并将其导入到另一个程序中 通过在宏中执行以下代码并使用工作表(“sheetname”),我可能已经完成了所需的操作。请
Sub CreateImport()
Sheets("Import").Select
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.ClearContents
Range("A2").Select
Sheets("IOAccess").Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Import").Select
ActiveSheet.Paste
Selection.End(xlDown).Select
Sheets("MemoryDisc").Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Import").Select
ActiveSheet.Paste
etc...
试一试第1单元A1
=query({Sheet2!A:Z; Sheet3!A:Z; Sheet4!A:Z}, "where Col1 <>'' ")
=query({Sheet2!A:Z;Sheet3!A:Z;Sheet4!A:Z},“where Col1'')
如果col A保存数值数据,则将最后一部分更改为“其中Col1不为空”
看看这是否有效?将单个工作簿中多张工作表中的数据合并到一张工作表中,以便与其他程序一起使用,这是VBA非常适合的任务 下面的代码被大量注释,以解释每个步骤中发生的情况,尽管
LastOccupiedColNum
和LastOccupiedColNum
函数没有注释。识别最后占用的行/列是VBA编程的基础,并在此处进行了详细说明:
简而言之,两个最重要的收获是属性和方法
选项显式
公共子组合数据来自AllSheets()
将WKSRC标注为工作表,WKSST标注为工作表
变暗rngSrc As范围,rngDst As范围
暗淡的lngLastCol为长,LNGSRCSTASTROW为长,LNGDSTASTROW为长
注释:“Src”是“源”的缩写,“Dst”是“目的”的缩写
“在前面设置引用
设置wksDst=ThisWorkbook.Worksheets(“导入”)
LNGDSTSTASTROW=LastOccupieDivertum(wksDst)'SO不是代码编写服务。试试fivver.com。或者,如果您有自己编写的代码,但在运行时遇到困难,请将其粘贴到此处,有人可以帮助您。我使用的是Excel2010。无法将“查询”识别为有效函数。抱歉。。我没有注意到excel标签。
Option Explicit
Public Sub CombineDataFromAllSheets()
Dim wksSrc As Worksheet, wksDst As Worksheet
Dim rngSrc As Range, rngDst As Range
Dim lngLastCol As Long, lngSrcLastRow As Long, lngDstLastRow As Long
'Notes: "Src" is short for "Source", "Dst" is short for "Destination"
'Set references up-front
Set wksDst = ThisWorkbook.Worksheets("Import")
lngDstLastRow = LastOccupiedRowNum(wksDst) '<~ defined below
lngLastCol = LastOccupiedColNum(wksDst) '<~ defined below
'Set the initial destination range
Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)
'Loop through all sheets
For Each wksSrc In ThisWorkbook.Worksheets
'Make sure we skip the "Import" destination sheet!
If wksSrc.Name <> "Import" Then
'Identify the last occupied row on this sheet
lngSrcLastRow = LastOccupiedRowNum(wksSrc)
'Store the source data then copy it to the destination range
With wksSrc
Set rngSrc = .Range(.Cells(2, 1), .Cells(lngSrcLastRow, lngLastCol))
rngSrc.Copy Destination:=rngDst
End With
'Redefine the destination range now that new data has been added
lngDstLastRow = LastOccupiedRowNum(wksDst)
Set rngDst = wksDst.Cells(lngDstLastRow + 1, 1)
End If
Next wksSrc
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INPUT : Sheet, the worksheet we'll search to find the last row
'OUTPUT : Long, the last occupied row
'SPECIAL CASE: if Sheet is empty, return 1
Public Function LastOccupiedRowNum(Sheet As Worksheet) As Long
Dim lng As Long
If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
With Sheet
lng = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
End With
Else
lng = 1
End If
LastOccupiedRowNum = lng
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INPUT : Sheet, the worksheet we'll search to find the last column
'OUTPUT : Long, the last occupied column
'SPECIAL CASE: if Sheet is empty, return 1
Public Function LastOccupiedColNum(Sheet As Worksheet) As Long
Dim lng As Long
If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
With Sheet
lng = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
End With
Else
lng = 1
End If
LastOccupiedColNum = lng
End Function