sparky2205
Well-known Member
- Joined
- Feb 6, 2013
- Messages
- 507
- Office Version
- 365
- 2016
- Platform
- Windows
Hi folks,
I have a spreadsheet that requests a user enter a value for a vendor number.
The user has the option to first select the number of vendors.
I have some error proofing, e.g. if the user cancels or doesn't enter a vendor number.
What I'm trying to achieve is that when the user makes an error they are returned to the input box.
My issue is that my counter doesn't increment correctly when this happens.
A user can enter 3 as the number of vendors but once they make a mistake this increases the number of vendors they can enter.
There are a couple of commented out lines where I was trying to work it out.
In summary; if a user enters 3 as the number of vendors, they should only be able to enter 3 vendors regardless of whether or not they make a mistaken entry, i.e. blank, cancel or incorrect vendor name.
This is the code:
I have a spreadsheet that requests a user enter a value for a vendor number.
The user has the option to first select the number of vendors.
I have some error proofing, e.g. if the user cancels or doesn't enter a vendor number.
What I'm trying to achieve is that when the user makes an error they are returned to the input box.
My issue is that my counter doesn't increment correctly when this happens.
A user can enter 3 as the number of vendors but once they make a mistake this increases the number of vendors they can enter.
There are a couple of commented out lines where I was trying to work it out.
In summary; if a user enters 3 as the number of vendors, they should only be able to enter 3 vendors regardless of whether or not they make a mistaken entry, i.e. blank, cancel or incorrect vendor name.
This is the code:
VBA Code:
Sub Filter_Vendor()
Dim vendor As Variant
Dim vendorrow As Long
Dim lastrow As Long
Dim number As Variant
Dim i As Long
' Find the last populated row
lastrow = Cells(Rows.Count, 3).End(xlUp).Row
' Hide all vendors
Rows("9:" & lastrow).EntireRow.Hidden = True
' Get the number of Vendors to be filtered from the user
number = Application.InputBox("How many Vendors do you wish to filter?", , , , , , , 1)
If number = False Then
Exit Sub
' If the user clicks on OK but doesn't enter a Vendor Number
ElseIf number = "" Then
MsgBox ("You have not entered a Vendor Number")
Exit Sub
ElseIf Not IsNumeric(number) Then
MsgBox "A number >0 is required here"
Exit Sub
End If
vvendor: For i = 1 To number
' Get the vendor number from the user
vendor = Application.InputBox("Enter the Vendor Number", , , , , , , 2)
' If user clicks Cancel
If vendor = False Then
' Hide the "Filter by Vendor" screen button
ActiveSheet.Shapes("Rectangle 3").Visible = False
' Show the "Unfilter Vendors" screen button
ActiveSheet.Shapes("Rectangle 5").Visible = True
' Show the "Update Filter" screen button
ActiveSheet.Shapes("Rectangle 4").Visible = False
' Hide the "Unfilter Specific Vendor" screen button
ActiveSheet.Shapes("Rectangle 6").Visible = False
'i = i - 1
'GoTo vvendor
Exit Sub
' If the user clicks on OK but doesn't enter a Vendor Number
ElseIf vendor = "" Then
MsgBox ("You have not entered a Vendor Number")
' Hide the "Filter by Vendor" screen button
ActiveSheet.Shapes("Rectangle 3").Visible = False
' Show the "Unfilter Vendors" screen button
ActiveSheet.Shapes("Rectangle 5").Visible = True
' Show the "Update Filter" screen button
ActiveSheet.Shapes("Rectangle 4").Visible = False
' Hide the "Unfilter Specific Vendor" screen button
ActiveSheet.Shapes("Rectangle 6").Visible = False
'i = i - 1
'GoTo vvendor
'Exit Sub
ElseIf IsError(Application.Match(vendor, Sheets("Revised").Range("C:C"), 0)) Then
MsgBox "Selected vendor" & " '" & vendor & "' " & "does not exist"
' Hide the "Filter by Vendor" screen button
ActiveSheet.Shapes("Rectangle 3").Visible = False
' Show the "Unfilter Vendors" screen button
ActiveSheet.Shapes("Rectangle 5").Visible = True
' Show the "Update Filter" screen button
ActiveSheet.Shapes("Rectangle 4").Visible = False
' Hide the "Unfilter Specific Vendor" screen button
ActiveSheet.Shapes("Rectangle 6").Visible = False
'Exit Sub
GoTo vvendor
Else
' Get the row number of the selected vendor
vendorrow = Application.Match(vendor, Sheets("Revised").Range("C:C"), 0)
'End If
End If
' Unhide the selected vendor row plus the next 3 rows
Rows(vendorrow & ":" & (vendorrow + 3)).Hidden = False
' i = i + 1
Next i
' Hide the "Filter by Vendor" screen button
ActiveSheet.Shapes("Rectangle 3").Visible = False
' Show the "Unfilter Vendors" screen button
ActiveSheet.Shapes("Rectangle 5").Visible = True
' Show the "Update Filter" screen button
ActiveSheet.Shapes("Rectangle 4").Visible = True
' Hide the "Unfilter Specific Vendor" screen button
ActiveSheet.Shapes("Rectangle 6").Visible = True
End Sub