VBA If Else or Select Case

Michaels

Active Member
Joined
Apr 2, 2009
Messages
404
Hi All,

My code below is working fine with If Else block but I am trying hard to use select case in this code instead of if with calculation/editing inside the select code block:

Code:
Sub ChangePOQty()
Application.ScreenUpdating = False
    Dim ws As Worksheet
    Dim FindString As String
    Dim Rng As Range


    Set ws = ThisWorkbook.Worksheets("Sheet1")
    FindString = UserForm1.TextBox1.Value
                
    If Not IsNumeric(UserForm1.TextBox3.Value) Or UserForm1.TextBox3.Value < 1 Or UserForm1.TextBox3.Value = "" Then
        MsgBox "Please double click the item to modify and enter positive quantity received.", vbInformation, "Quantity Received"
        With UserForm1.TextBox3
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Text)
        End With


    ElseIf Trim(FindString) <> "" Then
        Set Rng = ws.Cells.Find(What:=FindString, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Offset(0, 2)
        Rng.Value = UserForm1.TextBox3.Value
        With UserForm1
            .TextBox1.Visible = False
            .TextBox2.Visible = False
            .TextBox3.Visible = False
            .TextBox4.Visible = False
            .TextBox5.Visible = False
            .CommandButton1.Visible = False
        End With
    End If
Application.ScreenUpdating = True
End Sub

In other words, the below line of code has multiple conditions:
Code:
    If Not IsNumeric(UserForm1.TextBox3.Value) Or UserForm1.TextBox3.Value < 1 Or UserForm1.TextBox3.Value = "" Then

Then the ElseIf has this line:

Code:
    ElseIf Trim(FindString) <> "" Then

How can I use Select Case in above lines ?

Thank you for your help.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
My approach would be to use a function that returns a Boolean (True, False):

Code:
Function MeetsCriteria(val) As Boolean
    MeetsCriteria = True
    If Not IsNumeric(val) Then Exit Function
    If val < 1 Then Exit Function
    If val = "" Then Exit Function
    MeetsCriteria = False
End Function

Then call it in a Select Case:
Code:
Sub testit()
    Select Case MeetsCriteria(userform1.TextBox3.Value)
        Case True
            MsgBox "True"
        Case Else
            MsgBox "Not so much"
    End Select
End Sub
 
Upvote 0
Hi Tlowry,

Thank you for your code.

You see, when we use select case, apart from showing MsgBox in first if part, we would be performing actions too in ElseIf.

How can we use actions performing part and use that in Select Case ?
 
Upvote 0

Forum statistics

Threads
1,215,409
Messages
6,124,743
Members
449,186
Latest member
HBryant

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