Question

I'm trying to write a peace of VBA code that will filter the selected month from a table. If the user doesn't give an answer to the month question, I want a loop in there that keeps asking the question until an answer is given. Preferable even with as only option 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, but I'm just a beginner in VBA.

I have written the following code so far:

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

But it keep giving me a "type mismatch" error in

Do While bdaymonth Is "0"

I have tried some ways. If I try this I get an "Object required error":

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

I'm pretty new to VBA programming, could some of you give me some pointers to where I went wrong?

Was it helpful?

Solution

You compare the strings wrongly. You should do this:

Do While bdaymonth = "0"

i.e. use = and not Is.

Also, when you want to compare a string against "null", you should do this:

Do While bdaymonth = vbNullString
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top