此代码定义了一个函数,用于检查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
答案 0 :(得分:3)
当If shtexists(teamname) Then
为teamname
时,您正在呼叫String
,Function shtexists(shtname As Worksheet) As Boolean
,shtname 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