检查VBA范围内的现有图纸名称

时间:2019-05-22 10:49:47

标签: excel vba

我正在编写一个宏,用于检查选项卡名称列表中是否存在当前存在的选项卡或工作表,例如在A列中。在名为Import RPT的选项卡中找到A列

如果名称已经存在,我希望宏结束并创建一个MsgBox,说A列的第i行中已经存在一个电子表格,如果不存在,我希望它继续。

到目前为止,我已经将堆栈溢出中的片段合并在一起,看起来像这样

Dim sht As Worksheet, r As Range

With Sheets("Import RPT")
    For Each sht In Worksheets
        Set r = sht.Cells.Range("C2:C" & Count)
        If Application.WorksheetFunction.CountIf(Range("rngSheetInclusions"), sht.Name) > 0 Then
            strResult = strResult & "Duplicate Name: " & r & vbNewLine & "Rows: " & _
            Left(dict.Item(r), Len(dict.Item(r)) - 1) & vbNewLine & vbNewLine
            MsgBox strResul

    Next

我们非常感谢您的帮助;完成Newb到VBA

Dim sht As Worksheet, r As Range  

With Sheets("Import RPT")
    For Each sht In Worksheets
        Set r = sht.Cells.Range("C2:C" & Count)
        If Application.WorksheetFunction.CountIf(Range("rngSheetInclusions"), sht.Name) > 0 Then
            strResult = strResult & "Duplicate Name: " & r & vbNewLine & "Rows: " & _
            Left(dict.Item(r), Len(dict.Item(r)) - 1) & vbNewLine & vbNewLine
            MsgBox strResul

    Next

1 个答案:

答案 0 :(得分:1)

 Public Function SheetExists(s as string) As Boolean
     'Returns true if s is the name of a sheet in the current workbook
     On Error Goto NOPE
     Dim ws As Worksheet
     Set ws = ThisWorkbook.Sheets(s)
     SheetExists = True
     Exit function

 NOPE:
     SheetExists = False
 End function

一旦有了此功能,就可以在单元格B1中输入=SheetExists(A1),它将显示true或false。然后向下拖动选项卡名称旁边的b的其余部分