More efficient way to write a sequential If - Else statement

bioplz

New Member
Joined
Dec 1, 2021
Messages
15
Office Version
  1. 2019
Platform
  1. Windows
I am working on a program that contains a selection of a train number, and once that train number is selected there are two cars associated with each train number. These numbers will increase sequentially and the car numbers will also increase sequentially.

What I want to be able to do is write a smaller, more efficient, code that can continue without manually adding more code for hundreds of trains, and their associated cars.
Instead of writing an If-elseif-else statement for every train, could I write something that will manually track that?

The code provided is a snippet of what the final code will entail.
VBA Code:
If txtTrain = "7" Then
        If Me.cbxCar = "A" Then
            carNumber = "7013"
        ElseIf Me.cbxCar = "B" Then
            carNumber = "7014"
        Else
            carNumber = "7013 - 7014"
        End If
    ElseIf txtTrain = "8" Then
        If Me.cbxCar = "A" Then
            carNumber = "7015"
        ElseIf Me.cbxCar = "B" Then
            carNumber = "7016"
        Else
            carNumber = "7015 - 7016"
        End If
    ElseIf txtTrain = "9" Then
        If Me.cbxCar = "A" Then
            carNumber = "7017"
        ElseIf Me.cbxCar = "B" Then
            carNumber = "7018"
        Else
            carNumber = "7017 - 7018"
        End If
    ElseIf txtTrain = "10" Then
        If Me.cbxCar = "A" Then
            carNumber = "7019"
        ElseIf Me.cbxCar = "B" Then
            carNumber = "7020"
        Else
            carNumber = "7019 - 7020"
        End If
    ElseIf txtTrain = "11" Then
        If Me.cbxCar = "A" Then
            carNumber = "7021"
        ElseIf Me.cbxCar = "B" Then
            carNumber = "7022"
        Else
            carNumber = "7021 - 7022"
        End If
    ElseIf txtTrain = "12" Then
        If Me.cbxCar = "A" Then
            carNumber = "7023"
        ElseIf Me.cbxCar = "B" Then
            carNumber = "7024"
        Else
            carNumber = "7023 - 7024"
        End If
    End If
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
write all the combinations in a table of 3 columns somewhere in your workbook and use vlookup, index, ...
otherwise you can also use "select case"
 
Upvote 0
Try and adapt this:

VBA Code:
txtTrain = 10
cbxCar = "B"
myNum = (txtTrain - 7) * 2 + 7013


Select Case cbxCar
    Case "A"
        carNumber = myNum
    Case "B"
        carNumber = myNum + 1
    Case Else
        carNumber = myNum & "-" & myNum + 1
End Select

MsgBox carNumber
 
Upvote 0
Solution
Try and adapt this:

VBA Code:
txtTrain = 10
cbxCar = "B"
myNum = (txtTrain - 7) * 2 + 7013


Select Case cbxCar
    Case "A"
        carNumber = myNum
    Case "B"
        carNumber = myNum + 1
    Case Else
        carNumber = myNum & "-" & myNum + 1
End Select

MsgBox carNumber
I used this code for my program and it seemed to be working, until I received feedback the results were always 70** - 70**. The case never selects only A or B. I have tried this code on a separate userform to test and it works properly. Is there any reason that it wouldn't work in my regular project?

VBA Code:
    myNum = (txtTrain - 7) * 2 + 7013

    Select Case cbxCar
        Case "A"
            carNumber = myNum
        Case "B"
            carNumber = myNum + 1
        Case Else
            carNumber = myNum & "-" & myNum + 1
    End Select
        
    With Sheets("Data")
        .Cells(iCnt, 1) = iCnt - 2
        .Cells(iCnt, 2) = frmAddNewReport.txtStartDate
        .Cells(iCnt, 3) = CStr(txtTrain)
        .Cells(iCnt, 4) = CStr(carNumber)
        .Cells(iCnt, 5) = frmAddNewReport.cbxSupplier
        .Cells(iCnt, 6) = frmAddNewReport.txtIssues
        .Cells(iCnt, 7) = sel
        .Cells(iCnt, 8) = frmAddNewReport.txtAction
        .Cells(iCnt, 9) = frmAddNewReport.reqDate
        .Cells(iCnt, 10) = closeDate
        .Cells(iCnt, 11) = frmAddNewReport.closeEvid
        .Cells(iCnt, 12) = frmAddNewReport.closedBy
        .Cells(iCnt, 13) = CStr(frmAddNewReport.txtRTA)
        .Cells(iCnt, 14) = frmAddNewReport.cbxImpact
        .Cells(iCnt, 15) = frmAddNewReport.txtQAAction
        .Cells(iCnt, 16) = frmAddNewReport.txtSNBefore
        .Cells(iCnt, 17) = frmAddNewReport.txtSNAfter
        .Cells(iCnt, 18) = frmAddNewReport.txtNotes
    End With

This is the portion of my project that has the issue. Please let me know if you notice anything.
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,030
Members
449,205
Latest member
Eggy66

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