我有一个名为“测试”的工作簿。我想将名为“estimate”的工作簿中的所有工作表复制到测试。我不知道如何创建可以复制到“测试”的“estimate”上的工作表列表。有没有办法在 VBA 中做到这一点?
答案1
考虑:
Sub dural()
Dim b1 As Workbook, b2 As Workbook
Dim sh As Worksheet
Workbooks.Open Filename:="C:\TestFolder\test.xls"
Set b1 = ActiveWorkbook
Workbooks.Open Filename:="C:\TestFolder\estimate.xls"
Set b2 = ActiveWorkbook
For Each sh In b2.Sheets
sh.Copy after:=b1.Sheets(b1.Sheets.Count)
Next sh
End Sub
答案2
这将列出它们
Sub list()
Dim wbEstimate As Workbook
Set wbEstimate = ThisWorkbook
Dim wsList As Worksheet
Dim numSheets As Integer
numSheets = wbEstimate.Sheets.Count
Worksheets.Add(After:=Worksheets(numSheets)).Name = "list"
Set wsList = Worksheets("list")
Dim i As Integer
i = 1
For Each Sheet In wbEstimate.Worksheets
wsList.Cells(i, 1) = Sheet.Name
i = i + 1
Next
End Sub
这应该复制它们如果它们都打开
Sub list()
Dim wbEstimate As Workbook
Set wbEstimate = ThisWorkbook
Dim wbTest As Workbook
Set wbTest = Workbooks("test.xlsx")
For Each Sheet In wbEstimate.Sheets
Sheet.Copy after:=wbTest.Sheets(wbTest.Sheets.Count)
Next
End Sub
如果您需要在复印表格之前打开测试,请这样做 -
Sub list()
Dim wbEstimate As Workbook
Set wbEstimate = ThisWorkbook
Dim wbTest As Workbook
Set wbTest = Workbooks.Open("C:\Users\path\to\test.xlsx")
For Each Sheet In wbEstimate.Sheets
Sheet.Copy after:=wbTest.Sheets(wbTest.Sheets.Count)
Next
End Sub