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

时间:2014-02-03 01:00:54

标签: excel vba excel-vba 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
     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(allchekcs)>0 then
 If f.Offset(0, 5).Value = Left(allchecks, Len(allchecks) - 1) Then ctrl.Value = True
end if
编辑:所以我添加了if Len(allchecks)> 0然后命令,并且它没有给出错误5但它仍然没有勾选用户形式复选框 - 我该如何解决这个问题?现在,当信息被添加到第6列时,它被添加为“蒙特利尔渥太华多伦多温哥华”,也许这就是为什么它没有把它拿起来?因为一个单元格中有多个复选框名称?为了使ctrl.value = true工作,单元格值必须等于一个ctrl.name?有没有办法让我分开所以它拿起来使用ctrl?

1 个答案:

答案 0 :(得分:0)

要分隔单元格中的值,请使用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值的单元格的值。