Using Const Variable to Reference a Cell in XL03 VBA

fmcna

New Member
Joined
Jul 14, 2011
Messages
3
I am creating a macro that will need to open another workbook on a server. I originally set the file path of the "server workbook" to a public constant variable to be used by all modules/procedures.

Once I complete this project, it will be used at different sites with their own servers, so the file path will change. The other sites are not VBA proficient, so my answer to this was to have a cell on Sheet1 that contains the file path and the public constant variable will refer to that cell. However, when I debug the project, I get a compile error stating "Constant Expression Required" and the ".Range" part of the code is highlighted. (If the whole file path is coded in, I do not get this error)

Is there a way to get this to work? Or am I going to have to explain to others how to change the path in the macro?

Problem Code:
Code:
Public Const lotdatalocation As String = ThisWorkbook.Worksheets("Raw Materials").Range("C30") & "\Lotdata.xls"
Working Code:
Code:
Public Const lotdatalocation As String = "C:\Documents and Settings\cterry\Desktop\MCDS 4.0\Lotdata.xls"
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Thank you, but that didn't work either. I get another compile error, "Expected: end of statement". The whole line turns red and the equal sign is highlighted.
 
Upvote 0
You can't declare a variable and assign it on the same line like you do a Const.
But with a Const, by definition, one can't make it vary.

So try this:

Code:
'In your module 
Public lotdatalocation As String 
 
'Then in Workbook_Open
Private Sub Workbook_Open()
lotdatalocation = ThisWorkbook.Worksheets("Raw Materials").Range("C30") & "\Lotdata.xls"
'Alternate Method
'lotdatalocation = Application.GetOpenFilename("Excel Files (*.xl*), *.xl*")
End Sub
 
Upvote 0
tweedle,

That did it! I appreciate the help.

FYI, I already had 'Sub Auto_Open' procedure and I put the code in that.
 
Upvote 0

Forum statistics

Threads
1,224,543
Messages
6,179,429
Members
452,914
Latest member
echoix

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