用ws更新列表

时间:2016-07-26 16:57:19

标签: vba excel-vba excel

Sub addJobs()
Dim rngJobs As Range
Dim c As Variant

Set rngJobs = Range(Cells(2, 1), Cells(2, 1).End(xlDown))
MsgBox (rngJobs.Rows.Count)

For i = 1 To rngJobs.Rows.Count
    Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
    Sheets(Sheets.Count).Name = Cells(i + 1, 1).Value ' renames the new worksheet

Next i


End Sub

我有一个作业列表,我希望能够将该作业编号添加到该列表中,并希望将该新作业添加到我当前的工作表列表中。我该怎么做呢?我不清楚什么是最好的方法。

1 个答案:

答案 0 :(得分:0)

Sub addJobs()
Dim rngJobs As Range, j
Dim c As Variant, sht As Worksheet

Set rngJobs = Range(Cells(2, 1), Cells(2, 1).End(xlDown))
MsgBox (rngJobs.Rows.Count)

For i = 1 To rngJobs.Rows.Count

    j = Cells(i + 1, 1).Value

    Set sht = Nothing
    On Error Resume Next 'ignore error if sheet not found
    Set sht = Sheets(j)
    On Error Goto 0      'stop ignoring any errors

    If sht Is Nothing Then 'sheet not already present: create it
        Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
        Sheets(Sheets.Count).Name = j          'renames the new worksheet
    End If

Next i


End Sub