Link to home
Start Free TrialLog in
Avatar of Euro5
Euro5Flag for United States of America

asked on

vba choose sub using cell value

I am trying to use a cell value to determine the sub called. The code follows - there are three, each attached to a button.
If the box on Enter Data is checked, Queries!G2 = "TRUE"
I have used this in other ways, but this time it is not working- can anyone help please?

Sub ChooseSeries()
If Sheets("Queries").Range("G2").Value = "TRUE" Then
RunSeriesRatedWeight
Else
Call RunSeries
End If
End Sub
Sub Chooseoldweightseries()
If Sheets("Queries").Range("G2").Value = "TRUE" Then
RunRerateOriginalWeightONLY
Else
Call RunRerateONLY
End If
End Sub
Sub ChooseCustomerDataseries()
If Sheets("Queries").Range("G2").Value = "TRUE" Then
Call RunCustomerDataRATEDWT
Else
Call RunCustomerDataDIM
End If
End Sub

Open in new window

TEST.xlsm
Avatar of DrTribos
DrTribos
Flag of Australia image

Use select case...

Select Case TRUE

CASE myCellValue = 1
' stuff to do

Case myCellValue = 10
' stuff to do

End Select
There is also a case else... for situations where nothing is true

You can also change the way you use select case...

Select Case myCellValue
  Case 1
  ' do stuff

  Case 2
  ' do stuff

End Select
ASKER CERTIFIED SOLUTION
Avatar of Professor J
Professor J

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Euro5

ASKER

DrTribos, I'm not sure how to use that CASE in this context.
I tried and receive errors.
Sub ChooseSeries()
Select Case True
Sheets("Queries").Range("G2").Value = 1
Call RunSeriesRatedWeight
Sheets("Queries").Range("G2").Value = 10
Call RunSeries
End Select
End Sub
Avatar of Professor J
Professor J

Euro5 please see my answer above
Avatar of Euro5

ASKER

ProfessorJimJam, yes, I am trying. Thanks!
Avatar of Euro5

ASKER

Thanks so much!
you are welcome. i am glad i was able to spot the problem. :-)
Sub ChooseSeries()
Select Case True
Case Sheets("Queries").Range("G2").Value = 1
Call RunSeriesRatedWeight
Case Sheets("Queries").Range("G2").Value = 10
Call RunSeries
End Select
End Sub