If statement based on InputBox

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
723
Office Version
  1. 2016
Platform
  1. Windows
I can get this to work using Select Case, but was trying to shorten it up a bit.

No matter the InputBox response, row 4 is delete. I only want row 4 to be deleted if 82, 83, or 84 is entered.

VBA Code:
Sub Another_Test()
    Dim strArea As String
    strArea = InputBox("Area? (80,82,83,84,87)")
    If CInt(strArea) = 82 Or 83 Or 84 Then
            Rows("4").Delete
    End If
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
How about
VBA Code:
Sub Another_Test()
    Dim strArea As String
    strArea = InputBox("Area? (80,82,83,84,87)")
    Select Case CInt(strArea)
      Case 82 To 84: Rows("4").Delete
    End Select
    End If
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub Another_Test()
    Dim strArea As String
    strArea = InputBox("Area? (80,82,83,84,87)")
    Select Case CInt(strArea)
      Case 82 To 84: Rows("4").Delete
    End Select
    End If
End Sub
Thank you, I've been using that but was trying to shorten it without the use of Select Case. I had thought I saw someone else do something similar, but can't find it.
 
Upvote 0
How about
VBA Code:
Sub Another_Test()
    Dim strArea As String
    strArea = InputBox("Area? (80,82,83,84,87)")
    Select Case CInt(strArea)
      Case 82 To 84: Rows("4").Delete
    End Select
    End If
End Sub
Looking at your example again, I don't think I need that last "End If". If that's true, your example is the same number of lines as mine...sooooo
VBA Code:
Sub Another_Test()
    Dim strArea As String
    strArea = InputBox("Area? (80,82,83,84,87)")
    Select Case CInt(strArea)
      Case 82 To 84: Rows("4").Delete
    End Select
End Sub

Thank you for the help
 
Upvote 0
Another possibility

VBA Code:
Sub Another_Test()
    Dim strArea As String
    Do
    strArea = InputBox("Area? (80,82,83,84,87)")
    'cancel pressed
    If StrPtr(strArea) = 0 Then Exit Sub
    Loop Until Not IsError(Application.Match(Val(strArea), Array(80, 82, 83, 84, 87), 0))
    
    Rows("4").Delete

End Sub

Dave
 
Upvote 0
Or even
VBA Code:
Sub Another_Test()
    Dim strArea As String
    strArea = InputBox("Area? (80,82,83,84,87)")
    If CInt(strArea) >= 82 And CInt(strArea) <= 84 Then
      Rows("4").Delete
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,530
Messages
6,125,350
Members
449,220
Latest member
Edwin_SVRZ

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top