1

我有要复制到新的临时工作簿的工作表,但不保存

Worksheet.Copy将工作表复制到一个新的、未命名的(Book1、Book2、Book3 等)工作簿。我希望将所有工作表复制到同一个工作簿。

对于第一个之后的所有工作表,我都尝试过使用Worksheet.Copy After:=xlWb.Sheets(1),但我不知道在设置 xlWb 工作簿对象时如何引用新创建的工作簿。我不断收到

运行时错误 9,“下标超出范围”。

Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook
Dim xlWbOld As String
Dim xlWs As Excel.Worksheet
Dim xlWbNew As Excel.Workbook
Dim xlWsOld As Excel.Worksheet
Dim xlRng As Excel.Range
Dim xlRngOld As Excel.Range

xlWbOld = ActiveWorkbook.Name

Set xlApp = New Excel.Application
xlApp.Visible = True                                '*** Quite Important to set Excel.Visible,_
                                                    'Otherwise user wouldn't see the application's running _
                                                    'even though it would run as background

xlApp.Application.ScreenUpdating = False
Set xlWb = xlApp.Workbooks.Add                      'Create a new Workbook
Set xlWs = xlWb.Worksheets.Add

这是应该复制工作表的地方:

Select Case strRptType
    Case "DAILY"
        xlWs.Name = "1-Daily Price"
        'Check the last column and the last row
        lLastRow = oBasic.GetLast(, "DailyRpt", False, "A")
        iLastCol = oBasic.GetLast(, "DailyRpt", True, 4)
        Set xlRngOld = wksDailyRpt.Range(wksDailyRpt.Cells(4, 1), wksDailyRpt.Cells(lLastRow, iLastCol))
        Application.ScreenUpdating = True
        xlRngOld.Copy

        Set xlRng = xlWs.Cells(1, 1)
        xlRng.PasteSpecial Paste:=xlPasteValues
        xlWs.Columns.AutoFit

        For Each xlWsOld In ActiveWorkbook.Worksheets
            If xlWsOld.Name = "ForwardPrices" Or xlWsOld.Name = "ForwardVolatilities" _
              Or xlWsOld.Name = "ForwardReturns" Or xlWsOld.Name = "ForwardCorrelations" Then

                Sheets(xlWsOld.Name).Copy After:=Workbooks(xlWb).Sheets(1)

            End If
        Next xlWsOld
End Select
4

3 回答 3

1

这个小宏遍历所有打开的工作簿的所有工作表,并将它们复制到当前工作表之后。

Sub GatherAllSheets()
  Dim Wb As Workbook, Sh As Worksheet
  For Each Wb In Workbooks
    If Not Wb Is ThisWorkbook Then
      For Each Sh In Wb.Worksheets
        Sh.Copy after:=ActiveSheet
      Next Sh
    End If
  Next Wb
End Sub

这是你需要的吗?
或者您需要将工作表的内容复制到同一张工作表上?

于 2013-02-14T19:04:12.660 回答
0

我用以下方法解决了它:

            For Each xlWsOld In ActiveWorkbook.Worksheets
                If xlWsOld.Name = "ForwardPrices" Or xlWsOld.Name = "ForwardVolatilities" _
                Or xlWsOld.Name = "ForwardReturns" Or xlWsOld.Name = "ForwardCorrelations" Then

                    Set xlRngOld = Nothing
                    Set xlWsForwards = xlWb.Worksheets.Add

                    lLastRow = oBasic.GetLast(, xlWsOld.Name, False, "A")
                    iLastCol = oBasic.GetLast(, xlWsOld.Name, True, 1)

                    Set xlRngOld = xlWsOld.Range(xlWsOld.Cells(1, 1), xlWsOld.Cells(lLastRow, iLastCol))

                    xlWsForwards.Name = xlWsOld.Name
                    xlRngOld.Copy
                    Set xlRngForwards = xlWsForwards.Cells(1, 1)
                    xlRngForwards.PasteSpecial Paste:=xlPasteValues
                    xlWsForwards.Columns.AutoFit

                    xlWsForwards.Cells(1, 1).Select

                    Set xlWsForwards = Nothing

                End If
            Next xlWsOld
于 2013-02-15T12:40:22.403 回答
0

跳过创建新工作簿,一次性复制所有工作表。

xlWbOld.Sheets(Array("ForwardPrices", "ForwardVolatilities", "ForwardReturns", "ForwardCorrelations")).Copy
Set xlWb = ActiveWorkbook

这种方式复制创建成为 ActiveWorkbook 的新工作簿。然后您可以将它分配给一个工作簿对象,并在以后通过该名称引用它

于 2016-09-29T11:27:54.327 回答