lundi 22 août 2016

Need to shorten VBA, uses multiple IF statements

I am relatively new to VBA. I have a large amount of "If" statements which have to be shortened due to the fact I have reached a compile error, procedure limit reached.

I believe I may be able to write a case, or maybe a loop, but I am not sure if it is possible. I have already shorted the code slightly, but quite noobish.

I am taking a barcode scanner and scanning in a condition to an ActiveX textbox, upon entry of the barcode value, it selects a condition based on what the value is and performs a calculation then resets the textbox to "".

Link to other question which is similar

Here is a subset of the code.

Private Sub TextBox1_Change()

Dim ws As Worksheet
Set ws = Worksheets("Sheet1")

If (TextBox1.Value) = "15 - FT - R" Then
    ws.Cells(5, 11) = ws.Cells(5, 11) + 1
    TextBox1.Activate
    TextBox1.Value = ""
Else

    If (TextBox1.Value) = "16 - FT - R" Then
        ws.Cells(6, 11) = ws.Cells(6, 11) + 1
        TextBox1.Activate
        TextBox1.Value = ""
    Else
        '...
    End If
End If

End Sub

The other type of "if" statements include the following.

Private Sub TextBox1_Change()

Dim ws As Worksheet

Set ws = Worksheets("Sheet1")

If (TextBox1.Value) = "6 in x 6 ft R -1" Then
    ws.Cells(1, 9) = "W6 in x 6 ft R -1"
    ws.Cells(17, 1) = ws.Cells(17, 1) + 13
    ws.Cells(31, 11) = ws.Cells(31, 11) - 1
    Application.EnableEvents = False
    TextBox1.Activate
    TextBox1.Value = ""
Else
    If (TextBox1.Value) = "6 in x 15 ft R -1" Then
        ws.Cells(1, 9) = "W6 in x 15 ft R -1"
        ws.Cells(18, 1) = ws.Cells(18, 1) + 13
        ws.Cells(5, 11) = ws.Cells(5, 11) - 1
        Application.EnableEvents = False
        TextBox1.Activate
        TextBox1.Value = ""
    Else
        '...
    End If
End If

End Sub

I have spent some time working on shortening this, and any help would be amazing. Any links to an article or youtube would be great. Keep in mind, I work best with literal examples like Sheet1 or [a1] ect...

I really appreciate the help.

Regards

Aucun commentaire:

Enregistrer un commentaire