2016-07-26 70 views
0
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 

我的任務列表,我希望能夠將工作號碼添加到列表和VBA來新作業添加到我目前的工作表列表。我該怎麼做呢?我不清楚什麼是最好的方法。與WS更新列表

回答

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