VBA的新手。尝试从每个工作表中最后填充的列中获取填充的单元格,并将所有这些值粘贴到单个工作表中-在下一个空行上,这样就不会覆盖任何值。有以下内容,但在为LastCol变量分配范围时有问题。欢迎提供指导。
Sub ExtractLastColumn()
Dim ws As Worksheet
Dim sht As Worksheet
Dim wrk As Workbook
Dim LastCol As Range
Dim LastRow As Range
'Create new sheet and combine tabs
Set wrk = ActiveWorkbook 'Working in active workbook
'Add new worksheet as the last worksheet called INSERTS
With ThisWorkbook
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
ws.Name = "INSERTS"
End With
'loop to get values from last column on each worksheets and paste into new INSERTS sheet
For Each sht In wrk.Worksheets
If sht.Name <> "INSERTS" And sht.Name <> ws.Name Then
'get range of populated cells in last populated column
LastCol = Cells(1, Columns.Count).End(xlToLeft).Value
'get next empty row on INSERTS sheet
Worksheets("INSERTS").Activate
LastRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
'paste range from sheet into next emtpy row for INSERTS sheet
Worksheets(sht).Range(LastCol).Copy Worksheets("INSERTS").Range(LastRow)
End If
Next sht
End Sub
1条答案
按热度按时间z0qdvdin1#
提取最后一列