我正在尝试编写一个VBA代码,它将从表中过滤选定的月份。如果用户没有给出月份问题的答案,我想要一个循环,一直问问题,直到给出答案。最好只选择01,02,03,04,05,06,07,08,09,10,11,12,但我只是VBA的初学者。
到目前为止,我编写了以下代码:
Sub BE_NL_AddressBirthdaySplit()
'
' BE_NL_AddressBirthdaySplit Macro
'
' Keyboard Shortcut: Ctrl+w
'
Dim bdaymonth As String
bdaymonth = "0"
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=19, Criteria1:= _
"BE-PM Benelux BVBA"
bdaymonth = InputBox("What Month would you like to filter?", "Month of Birth")
Do While bdaymonth Is "0"
If bdaymonth = "" Then
result = MsgBox("You didn't specify the month", vbCritical, "No Month")
Else: ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=16, Criteria1 _
:=bdaymonth
End If
Loop
End Sub
但它继续在
中给我一个“类型不匹配”错误Do While bdaymonth Is "0"
我尝试了一些方法。如果我试试这个,我得到一个“对象必需的错误”:
Sub BE_NL_AddressBirthdaySplit()
'
' BE_NL_AddressBirthdaySplit Macro
'
' Keyboard Shortcut: Ctrl+w
'
Dim bdaymonth As Variant
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=19, Criteria1:= _
"BE-PM Benelux BVBA"
bdaymonth = InputBox("What Month would you like to filter?", "Month of Birth")
Do While bdaymonth Is Null
If bdaymonth = "" Then
result = MsgBox("You didn't specify the month", vbCritical, "No Month")
Else: ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=16, Criteria1 _
:=bdaymonth
End If
Loop
End Sub
我对VBA编程很陌生,你们有些人可以给我一些指向错误的地方吗?
答案 0 :(得分:1)
你错误地比较了字符串。你应该这样做:
Do While bdaymonth = "0"
即。使用=
而非Is
。
此外,当您想要将字符串与“null”进行比较时,您应该这样做:
Do While bdaymonth = vbNullString