目标是用sheet 2,3,4等所有数据完全填充sheet1。
源表可能具有填充数据的可变行数,但是,行中没有间隙(即,其中包含数据的最后一行是该表中的最后一行数据)有固定数量的行工作簿中的工作表。
基本上,所有其他工作表将用作工作空间,然后创建最终连续工作表,将其导入另一个程序。
我可能已经通过在宏中使用Sheets(“sheetname”)执行以下代码来完成我所需要的操作。为每个工作表选择。我宁愿在For / Next循环中执行此操作以消除所有冗余行。
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...
答案 0 :(得分:1)
将单个工作簿中的多个表单中的数据合并到单个工作表以与其他程序一起使用是VBA非常适合的任务。
下面的代码是严重注释,以解释每个步骤中发生的情况,尽管LastOccupiedRowNum
和LastOccupiedColNum
函数不是。识别最后一个占用的行/列是VBA编程的基础,并在此处巧妙地解释:Error in finding last used cell in VBA
简而言之,两个最重要的要点是Workbook.Worksheets
属性和Range.Copy
方法。
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
像上面看到的那样使用宏录制器是开始学习VBA的一种很好的方法,但是当涉及到循环时,你很快就会碰壁。
您可以在此4分钟的演练中看到此代码的实际效果,并详细了解所包含的概念:https://www.youtube.com/watch?v=vbC2lGLFXS0&feature=youtu.be
答案 1 :(得分:0)
尝试Sheet1单元格A1
=query({Sheet2!A:Z; Sheet3!A:Z; Sheet4!A:Z}, "where Col1 <>'' ")
如果col A保存数字数据,则将最后一部分更改为&#34;其中Col1不为空&#34;。
看看这是否有效?