Excel表创build和更新

我正在寻找一种基于单元格列表的Excel中创build工作表的方法是,我希望脚本检查列表是否已更新,并添加附加工作表,而不是重新创build全部或删除旧副本

1)有可能从excel(非VBA)

2)如果不是我创build工作表的代码是:但它会创build新的entrys如果我重新运行(我正在寻找更新)

Sub AddSheets() 'Updateby Extendoffice 20161215 Dim xRg As Excel.Range Dim wSh As Excel.Worksheet Dim wBk As Excel.Workbook Set wSh = ActiveSheet Set wBk = ActiveWorkbook Application.ScreenUpdating = False For Each xRg In wSh.Range("A1:A7") With wBk .Sheets.Add after:=.Sheets(.Sheets.Count) On Error Resume Next ActiveSheet.Name = xRg.Value If Err.Number = 1004 Then Debug.Print xRg.Value & " already used as a sheet name" End If On Error GoTo 0 End With Next xRg Application.ScreenUpdating = True End Sub 

使用这个函数检查工作表是否已经存在,然后让它跳过它。

 Function WorksheetExists(sName As String) As Boolean WorksheetExists = Evaluate("ISREF('" & sName & "'!A1)") End Function 

所以你的代码可以是:

 Sub AddSheets() 'Updateby Extendoffice 20161215 Dim xRg As Variant Dim wSh As Excel.Worksheet Dim wBk As Excel.Workbook Set wSh = ActiveSheet Set wBk = ActiveWorkbook Application.ScreenUpdating = False For Each xRg In wSh.Range("A1:A7") If Not IsError(xRg) Then If xRg <> "" Then If Not WorkSheetExists((xRg)) Then With wBk .Sheets.Add after:=.Sheets(.Sheets.Count) ActiveSheet.Name = xRg.Value End With End If End If End If Next xRg Application.ScreenUpdating = True End Sub Function WorksheetExists(sName As String) As Boolean WorksheetExists = Evaluate("ISREF('" & sName & "'!A1)") End Function 

这是另一种select。 我还添加了一个部分,它将命名表中的A列值。 (如果需要,可以删除)。

 Sub AddSheets() 'Updateby Extendoffice 20161215 Dim xRg As Excel.Range Dim wSh As Excel.Worksheet Dim wBk As Excel.Workbook Set wSh = ActiveSheet Set wBk = ActiveWorkbook Application.ScreenUpdating = False For Each xRg In wSh.Range("A1:A7") With wBk If Not sheetExists(xRg.Value) and xRg <> "" Then .Sheets.Add after:=.Sheets(.Sheets.Count) ActiveSheet.Name = xRg.Value End If End With Next xRg Application.ScreenUpdating = True End Sub Function sheetExists(sheetToFind As String) As Boolean 'http://stackoverflow.com/a/6040454/4650297 Dim sheet As Worksheet sheetExists = False For Each sheet In Worksheets If sheetToFind = sheet.Name Then sheetExists = True Exit Function End If Next sheet End Function