删除工作表错误“下标超出范围”

时间:2019-02-08 22:19:39

标签: excel vba

我有2张纸分别名为Tab 1和Tab 2。 我正在使用以下选项卡2中的条目创建工作表:

Sub copyeachrowtosheets()

Dim i As Integer

Dim j As Integer

Dim z As String

Dim y As Integer

Dim x As String

Dim h As Worksheet

Dim k As Integer

Dim l As String

Dim m As String

i = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row

y = 2

For j = 1 To (i - 1)

z = "A" & y

x = "M" & y

k = ActiveWorkbook.Sheets.Count

m = "B" & y

l = Sheets(2).Range(m).Value

Set h = ThisWorkbook.Sheets.Add(after:=Sheets(k))

h.Name = l

Sheets(2).Range("A1:M1").Copy Destination:=ActiveSheet.Range("A1")

y = y + 1

Next j

End Sub

now if i want to delete all the created sheets using vba, i made the below:

Sub deletesheetsspecific()

Dim i As Integer

Dim j As Integer

'Dim x As Integer

Application.DisplayAlerts = False

i = ActiveWorkbook.Sheets.Count

    For j = 1 To i

    Dim k As String

    k = VBA.left(Sheets(j).Name, 3)

        If k <> "tab" Then

        ActiveWorkbook.Sheets(j).delete

        End If

    Next j

Application.DisplayAlerts = True

End Sub

它只是删除随机的几张纸,然后在显示“ k = VBA.left(Sheets(j).Name,3)”的行上显示错误“运行时错误:下标超出范围”。如果我单击以再次运行,它将删除几张纸,然后出现相同的错误。

有人可以告诉我我在这里做错什么吗?

0 个答案:

没有答案