【问题标题】:invalid procedure call or argument excel vba无效的过程调用或参数excel vba
【发布时间】:2014-02-03 01:00:54
【问题描述】:

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

添加命令:

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 then 命令,它没有给出错误 5,但它仍然没有选中用户表单复选框 - 我该如何解决这个问题?现在,当信息被添加到第 6 列时,它被添加为“蒙特利尔渥太华多伦多温哥华”,也许这就是它没有选择它的原因?因为一个单元格中有多个复选框名称?为了让 ctrl.value = true 起作用,单元格值必须等于一个 ctrl.name?有没有办法我可以分开,所以它使用 ctrl 拾取?

【问题讨论】:

  • 出现这个错误时allchecks的值是多少(我猜是"")? Left() 的第二个参数必须大于或等于 0。如果您将“allchecks”作为零长度值,那么它将错误输出 '5'。
  • @PatricK 我已经编辑并添加了 If Len(allchecks)>0 函数,但它没有获取 ctrl.value = true。我已经编辑了帖子
  • 所以allchecks""。问题在If ctrl Then。你可以尝试用If ctrl.Value = True Then替换它吗?
  • 我添加了 if ctrl.value = true then 但没有任何反应..
  • 变量allchecks 很可能是根本原因。如果选中,为什么要附加控件名称,然后再比较整个字符串?不要得到应该在f.Offset(0, 5) 中的内容。您是否尝试根据工作表更改复选框值?我想这就是你需要的:ctrl.Value = (Instr(1,f.Offset(0, 5).Value,allchecks,vbTextCompare) = 1)

标签: vba excel userform


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多