编译错误:ByRef参数类型不匹配创建新工作表

时间:2017-03-11 05:45:45

标签: excel vba excel-vba

此代码定义了一个函数,用于检查Sheet是否存在并返回布尔值。 Sub正在尝试激活工作簿。然后它检查工作表(DataEntry)并遍历行并检查团队名称是否已经是工作表,如果它不是创建该工作表的新工作表并将数据粘贴到该工作表中。如果工作表确实存在,则将其复制到现有工作表中。然后它将匹配号加1,这样当它再次粘贴时,它确定它粘贴数据的行。但是当我尝试通过工作表上的按钮运行它时,它给了我byref参数类型不匹配错误。

对可能出现的问题有什么看法?

Function shtexists(shtname As Worksheet) As Boolean
    Dim sht As Worksheet
    Set sht = Sheets(shtname)
    On Error GoTo 0
    SheetExists = Not sht Is Nothing
End Function

Sub AddData()
    Workbooks("ScoutingDatabseMaster.xlsm").Activate
    Dim teamname As String
    Dim countery As Integer
    Dim matchnumber As Integer
    matchnumber = 1
    countery = 4
    teamname = Range("B4", "B" & countery)
    For countery = 4 To 9
        If shtexists(teamname) Then
            Worksheets("DataEntry").Range("C" & countery, "M" & countery).Copy
            Worksheets(teamname).Range("A" & matchnumber).Paste
        If teamname = "" Then
            MsgBox ("You forgot to Enter a Team Number!")
        Else
            Sheets.Add.Name = teamname
        End If
        Next countery
    matchnnumber = matchnumber + 1
    countery = 4
End Sub

1 个答案:

答案 0 :(得分:3)

If shtexists(teamname) Thenteamname时,您正在呼叫StringFunction shtexists(shtname As Worksheet) As Booleanshtname As Worksheet,这会引发运行时错误。 shtname也应为String

将您的Function声明更改为:

Function shtexists(shtname As String) As Boolean
    Dim sht As Worksheet
    Set sht = Sheets(shtname)
    On Error GoTo 0
    shtexists = Not sht Is Nothing
End Function