Improving a piece of code

Trueblue862

Board Regular
Joined
May 24, 2020
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm currently learning VBA, I've created this piece of code below. I'm looking for advice on ways to improve it. This is the first piece of code that I've written entirely without copying or modifying sections from someone else. This code works perfectly for my purposes, I'm just looking for advice on how to go about making it better for future reference. I appreciate any input on this.
VBA Code:
Private Sub LabEL_upDate()

If tbxSloppy.Value = "" And tbxJade.Value = "" Then
    LabelE.Visible = True
    LabelE.Caption = "YES!!!"
    LabelE.BackColor = vbGreen
  Else
    LabelE.Visible = False
End If
    labeL_DaTe

End Sub
Private Sub labeL_DaTe()

If LabelE.Visible = True Then Exit Sub
If tbxSloppy.Value = "PERHAPS" And tbxJade.Value = "" Then
    LabelE.Visible = True
    LabelE.Caption = "MAYBE"
    LabelE.BackColor = vbYellow
  Else
    LabelE.Visible = False
End If
    labeL_DaTe2
End Sub
Private Sub labeL_DaTe2()

If LabelE.Visible = True Then Exit Sub
If tbxSloppy.Value <> "" Or tbxJade.Value <> "" Then
    LabelE.Visible = True
    LabelE.Caption = "NO :(  "
    LabelE.BackColor = vbRed
  Else
    LabelE.Visible = False
End If

End Sub
 
for sure use vbnullstring for assignments - that is, somestring = vbnullstring
and the LEN test for time savings - quicker to check if the LEN is zero
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
This is all very interesting reading, all the different ways to solve the same problem. I really appreciate everyone's input on this.

CephasOz, what are the benefits to using "select case" instead of "if"?
 
Upvote 0
That's a good question. Some programmers will argue the speed of one versus the other, but I just prefer the flexibility and readability of the Select Case statement. Most people are familiar with the basic form:
VBA Code:
    Select Case strVar
        Case "a"
            MsgBox "a"
        Case "b"
            MsgBox "b"
        Case Else
            MsgBox "something else"
    End Select
and testing against other variables:
VBA Code:
    Select Case strVar1
        Case strVar2
            MsgBox "1=2"
        Case strVar3
            MsgBox "1=3"
        Case Else
            MsgBox "1 <> 2 or 3"
    End Select
and the "Is" tests:
VBA Code:
    Select Case strVar
        Case Is < "i"
            MsgBox "a-h"
        Case Is < "q"
            MsgBox "i-p"
        Case Else
            MsgBox "something else"
    End Select
but not many realise that you can invert the logic of "Select Case variable, Case value" to make it "Select Case value, Case variable" to test multiple variables like so:
VBA Code:
    Select Case True
        Case bolVar1
            MsgBox "1 is True"
        Case bolVar2
            MsgBox "2 is True"
        Case bolVar3
            MsgBox "3 is True"
        Case Else
            MsgBox "none is True"
    End Select
and you can even use a selection of tests by extending the inverted logic, like so:
VBA Code:
    Select Case True
        Case (strVar1 = strVar2)
            MsgBox "1=2"
        Case (strVar2 = strVar3)
            MsgBox "2=3"
        Case (strVar1 = strVar3)
            MsgBox "1=3"
        Case Else
            MsgBox "nothing equal"
    End Select
 
Upvote 0
And that's all without mentioning Select's ability to deal with both lists of values and ranges of values, such as (lists):
VBA Code:
    Select Case strVar
        Case "a", "e", "i", "o", "u"
            MsgBox "A vowel"
        Case Else
            MsgBox "Not a vowel"
    End Select
and (ranges):
VBA Code:
    Select Case strVar
        Case "b" To "d", "f" To "h", "j" To "n", "p" To "t", "v" To "z"
            MsgBox "A consonant"
        Case Else
            MsgBox "Not a consonant"
    End Select
as well as mixing equalities, lists, ranges, and Is tests on the same line. So Select is sort of a swiss army knife of value testing.
 
Upvote 0
Thanks for the explanation, I’ll try and make use of it in my future attempts at writing code.
 
Upvote 0

Forum statistics

Threads
1,223,953
Messages
6,175,598
Members
452,658
Latest member
GStorm

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