你能帮我写一个VBA代码来检索一些数据:
我有一张包含一些工作表的Excel表格。其中一些表称为:
所有这些表都有相同的标题,所以我的想法是:
提前致谢。
答案 0 :(得分:0)
这应该有所帮助:
Sub CopySheets()
Dim i As Long, r As Long, c As Long
Dim rNew As Long
Dim ws As Worksheet, wbo As Workbook, wbNew As Workbook
Dim wsNew As Worksheet
Set wbo = ActiveWorkbook
Set wbNew = Workbooks.Add
Set wsNew = wbNew.Worksheets(1)
rNew = 1
For Each ws In wbo.Worksheets
With ws
If Left(ws.Name, 6) = "region" Then
r = .Cells(.Rows.Count, 1).End(xlUp).Row
c = .Cells(1, .Columns.Count).End(xlToLeft).Column
If rNew = 1 Then ' copy headers
wsNew.Range(wsNew.Cells(1, 1), wsNew.Cells(r, c)).Value = _
.Range(.Cells(1, 1), .Cells(r, c)).Value
rNew = r + 1
Else ' don't copy headers
wsNew.Range(wsNew.Cells(rNew, 1), wsNew.Cells(r + rNew - 2, c)).Value = _
.Range(.Cells(2, 1), .Cells(r, c)).Value
rNew = r + rNew - 1
End If
End If
End With
Next
Call wbNew.Save("N:\wb.xls")
End Sub
为我工作。使用此解决方案,您不必使用临时表。它将复制所有以“region”开头的工作表。