[英]invalid procedure call or argument excel vba
我想要一個添加命令和一個搜索命令。 我以為如果我往復添加命令,那么我可以將其用作搜索命令,但是它給出了無效的過程調用或參數。
添加命令:
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接?
要分隔單元格中的值,請使用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
值的單元格的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.