我正在尝试复制和重命名工作表,但出现下标超出范围错误

时间:2018-12-20 06:37:44

标签: excel vba excel-vba

我正在尝试将Sheet2复制到Sheet3并将其重命名为Result,但是我总是出现下标超出范围的错误(这是因为该工作表不存在,这可能意味着没有进行复制)。

我尝试使用“复制”,如下面的代码所示,但是下标超出范围错误不断出现。任何帮助解决此问题的方法都非常感谢。谢谢!

    Set shtSheet1 = Worksheets("Sheet1")
    Set shtSheet2 = Worksheets("Sheet2")

    'trying to copy Sheet2 and create a Sheet3 tab
    shtSheet2.Copy After:=Sheets("Sheet3")
    Set shtSheet3 = Sheets(Sheets("Sheet3").Index + 1)
    shtSheet3.Name = "Result"

1 个答案:

答案 0 :(得分:2)

复印表

在Sheet2之后

Sub NewSheetAfterSheet2()

  Dim shtSheet2 As Worksheet
  Dim shtSheet3 As Worksheet
  Set shtSheet2 = Sheets("Sheet2")
  shtSheet2.Copy After:=shtSheet2
  Set shtSheet3 = Sheets(shtSheet2.Index + 1)
  shtSheet3.Name = "Result"
End Sub

最后位置

Sub NewSheetLastPosition()

  Dim shtSheet2 As Worksheet
  Dim shtSheet3 As Worksheet
  Set shtSheet2 = Sheets("Sheet2")
  shtSheet2.Copy After:=Sheets(Sheets.Count)
  Set shtSheet3 = Sheets(Sheets.Count)
  shtSheet3.Name = "Result"
End Sub