Compile Error: Expected Function or Variable

Sharid

Well-known Member
Joined
Apr 22, 2007
Messages
1,064
Office Version
  1. 2016
Platform
  1. Windows
I have moved some code into a Module called "Location" and I am trying to Run it from there when needed.

The Code is Run by using the following code
VBA Code:
Private Sub LocationTabStrip_Change()
'On Tab Change Call This Module
Application.Run "Location.LocationTabStrip"
End Sub

This code has been moved to a Module CALLED "Location"
VBA Code:
Private Sub LocationTabStrip()
'###  CHANGE FRAME CAPTIONS WHEN TAB IS SELECTED -
Select Case LocationTabStrip.Value
    Case Is = 0
       LocationFrame.Caption = "London"
    Case Is = 1
        LocationFrame.Caption = "Birmingham"
    Case Is = 2
         LocationFrame.Caption = "Nottingham"
    Case Is = 3
         LocationFrame.Caption = "Derby"
    Case Is = 4
         LocationFrame.Caption = "Manchester"
End Select
End Sub

I keep getting and error message here as soon as I change a Tab on this line of code below and I can't work it out.
VBA Code:
Select Case LocationTabStrip.Value
1609325126182.png
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I was able to sort this problem out by changing the name of the code, this error happens when a Sub is called the same as variable
From
VBA Code:
Private Sub LocationTabStrip()
'###  CHANGE FRAME CAPTIONS WHEN TAB IS SELECTED -
Select Case LocationTabStrip.Value
To
VBA Code:
Private Sub LocationTab()
'###  CHANGE FRAME CAPTIONS WHEN TAB IS SELECTED -
Select Case LocationTabStrip.Value

And to run the code
VBA Code:
Private Sub LocationTabStrip_Change()
'On Tab Change Call This Module
Application.Run "Location.LocationTab" 'changed this, removed the word TabStrip
End Sub

This has got rid of the first error message however I now get the following error message on the same line of code
VBA Code:
Select Case LocationTabStrip.Value

1609327882839.png


PS, All the code was working fine when it was part of the tabstrip and in the userform, I removed it to make it run from a Module as it would load the userform faster and tidy up all the code in the form. However it now throws up problems
 
Upvote 0
Not something that I'm entirely familiar with, but I expect that you need to refer to the parent name as well. For example if LocationTabStrip is an object on a userform then you need to use the form name as well, something like Userform1.LocationTabStrip.Value

More likely you will need to declare the value and caption as public variables or procedure variables for them to be passed back and forth.

Better advice would be to move the code back to the original module, having it elsewhere is going to create more problems than you think it might solve.
 
Last edited:
Upvote 0
Solution
Jasonb75

Thanks your answer was super, I had to reference the userform as well.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

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