且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Excel VBA 试图创建一个新的工作表

更新时间:2023-11-23 23:05:10

Tim Williams 关于添加新工作表的观点绝对正确.

Tim Williams is absolutely correct about adding a new worksheet.

但是您的逻辑似乎有缺陷.您的代码仅在没有名为Logix Friendly"的工作表且工作簿中只有 1 个工作表时才有效.

However your logic appears to be flawed. Your code only works if there isn't a worksheet named "Logix Friendly" and there is only 1 sheet in the workbook.

你应该做什么:

For Each ws In ThisWorkbook.Worksheets
    bWS_Found = (InStr(1, ws.Name, "Logix Friendly", vbTextCompare) = 1)
    ' Exit checking if a worksheet already named "Logix Friendly"
    If bWS_Found Then Exit For
Next
' Add worksheet if there isn't a "Logix Friendly" worksheet
If Not bWS_Found Then
    With ThisWorkbook.Worksheets.Add
        .Name = "Logix Friendly"
    End With
End With