Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 功能问题_Excel_Vba - Fatal编程技术网

Excel 功能问题

Excel 功能问题,excel,vba,Excel,Vba,我正在尝试让工作表的警报已经存在,但我迷路了。 功能定义是否不正确 Public Sub CopySheets() Do shName = InputBox("Please enter name of new project", "New Project") If shName <> "" Then shExists = SheetExists(shName) 'Check for existing sheet nam

我正在尝试让工作表的警报已经存在,但我迷路了。 功能定义是否不正确

 Public Sub CopySheets()

    Do

        shName = InputBox("Please enter name of new project", "New Project")

        If shName <> "" Then

        shExists = SheetExists(shName) 'Check for existing sheet name
        If Not shExists Then
        Worksheets(Array(1, 2)).Copy After:=Sheets(Sheets.Count)


        Else

        MsgBox "Project Name:" & Space(1) & shName & " already exists", vbOKOnly + vbCritical, "Deter"
        End If
        End If
    Loop Until Not shExists Or shName = ""
End Sub

Private Function SheetExists(ByVal sheetName As String, _
        Optional ByVal wb As Workbook)

        If wb Is Nothing Then Set wb = ActiveWorkbook
        On Error Resume Next
        SheetExists = Not wb.Worksheets(sheetName) Is Nothing

End Function
公共子副本()
做
shName=InputBox(“请输入新项目名称”、“新项目”)
如果你的名字是“”,那么
shExists=SHEETEISTS(shName)'检查现有图纸名称
如果不是她,那么她就存在
工作表(数组(1,2)).Copy After:=工作表(Sheets.Count)
其他的
MsgBox“项目名称:”&Space(1)&shName&“已存在”,vbOKOnly+vbCritical,“确定”
如果结束
如果结束
循环直到不存在shExists或shName=“”
端接头
私有函数SheetExists(ByVal sheetName作为字符串_
可选ByVal wb As工作簿)
如果wb为空,则设置wb=ActiveWorkbook
出错时继续下一步
SheetExists=非wb。工作表(sheetName)为空
端函数

您忘记添加函数的类型。它必须是
boolean

    Private Function 

    SheetExists(ByVal sheetName As String, _
        Optional ByVal wb As Workbook) As Boolean

        If wb Is Nothing Then Set wb = ActiveWorkbook
        On Error Resume Next
        SheetExists = Not wb.Worksheets(sheetName) Is Nothing

    End Function
代码的问题是,如果工作表不存在,则行
SheetExists=not wb。工作表(sheetName)为Nothing
将引发错误,
SheetExists
将保留其默认值,该值为空,作为函数的默认数据类型,而不定义数据类型为
variant
。如果将数据类型定义为
boolean
,则默认值为
False

我想这段代码虽然更长,但更清晰

Private Function SheetExists(ByVal sheetName As String, _
    Optional ByVal wb As Workbook) As Boolean

    Dim ws As Worksheet

    If wb Is Nothing Then Set wb = ActiveWorkbook

    On Error GoTo EH
    Set ws = wb.Worksheets(sheetName)
    SheetExists = True

    Exit Function

EH:
    SheetExists = False

End Function

再一次彻底解释!非常感谢斯道拉克斯。祝你周末愉快。