无效的过程调用或参数excel vba

无效的过程调用或参数excel vba,vba,excel,userform,Vba,Excel,Userform,我想要一个添加命令和一个搜索命令。我想如果我重复add命令,那么我可以将其用作搜索命令,但它会给出无效的过程调用或参数 添加命令: Sub TransferMasterValue() Dim allchecks As String Dim ws As Worksheet 'Iterate through the checkboxes concatenating a string of all names For Each ctrl In UserForm1.Controls I

我想要一个添加命令和一个搜索命令。我想如果我重复add命令,那么我可以将其用作搜索命令,但它会给出无效的过程调用或参数

添加命令:

Sub TransferMasterValue()
Dim allchecks As String
Dim ws As Worksheet
 'Iterate through the checkboxes concatenating a string of all names
   For Each ctrl In UserForm1.Controls
     If TypeName(ctrl) = "CheckBox" Then
       If ctrl Then
        allchecks = allchecks & ctrl.Name & " "
       End If
     End If
    Next

'If you have at least one transfer to the Master sheet
If Len(allchecks) > 0 Then
  Set ws1 = Sheets("Master")
    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

     With ws1
        .Cells(emptyRow, 1).Value = surname.Value
        .Cells(emptyRow, 2).Value = firstname.Value
        .Cells(emptyRow, 3).Value = tod.Value
        .Cells(emptyRow, 4).Value = program.Value
        .Cells(emptyRow, 5).Value = email.Value
        .Cells(emptyRow, 7).Value = officenumber.Value
        .Cells(emptyRow, 8).Value = cellnumber.Value
        .Cells(emptyRow, 6).Value = Left(allchecks, Len(allchecks) - 1)
     End With
  End If
End Sub
搜索命令:

Private Sub Search_Click()
Dim Name As String
Dim f As Range
Dim r As Long
Dim ws As Worksheet
Dim s As Integer
Dim FirstAddress As String
Dim ctrl As control
Dim allchecks As String

  For Each ctrl In UserForm1.Controls
    If TypeName(ctrl) = "CheckBox" Then
      If ctrl.value = true Then
        allchecks = allchecks & ctrl.Name & " "


       End If
     End If
  Next

 Name = surname.Value

 With ws
   Set f = Range("A:A").Find(what:=Name, LookIn:=xlValues)
    If Not f Is Nothing Then
      With Me
        firstname.Value = f.Offset(0, 1).Value
        tod.Value = f.Offset(0, 2).Value
        program.Value = f.Offset(0, 3).Value
        email.Value = f.Offset(0, 4).Text
        officenumber.Value = f.Offset(0, 6).Text
        cellnumber.Value = f.Offset(0, 7).Text
编辑:


编辑:所以我添加了if Len(allchecks)>0 then命令,它没有给出错误5,但它仍然没有选中userform复选框-我如何修复这个问题?现在,当信息被添加到第6列时,它被添加为“蒙特利尔-渥太华-多伦多-温哥华”,也许这就是为什么它不接受它的原因?因为一个单元格中有多个复选框名称?要使ctrl.value=true起作用,单元格值必须等于一个ctrl.name?是否有一种方法可以将其分离,以便使用ctrl键拾取

要分离单元格中的值,请使用
Split
命令:

For each ctrl in UserForm1.Controls
    For i = 0 to UBound(Split(CellValue," "))
        If Split(CellValue, " ")(i) = ctrl.Name Then ctrl.Value = True
    Next i
Next  

其中
CellValue
是包含
allchecks
值的单元格的值。

发生此错误时,allchecks的值是多少(我猜是
)?
Left()
的第二个参数必须大于或等于0。如果将“allchecks”作为零长度值,那么它将出错为“5”。@PatricK我已经编辑并添加了If Len(allchecks)>0函数,但它没有选择ctrl.value=true。我已经编辑了postSo
allchecks
”。问题在于
如果按ctrl键,则为
。如果ctrl.Value=True,那么可以尝试将其替换为
吗?我已经添加了If ctrl.Value=True,但是什么也没有发生。很可能变量
allchecks
是根本原因。如果选中,为什么要附加控件名,然后再比较整个字符串?不要获取f偏移量(0,5)
中应该包含的内容。是否尝试根据工作表更改复选框值?我想这就是您需要的:
ctrl.Value=(Instr(1,f.Offset(0,5).Value,allchecks,vbTextCompare)=1)
For each ctrl in UserForm1.Controls
    For i = 0 to UBound(Split(CellValue," "))
        If Split(CellValue, " ")(i) = ctrl.Name Then ctrl.Value = True
    Next i
Next