Assigning a variable a permant value till it is reassigned

Public wkbname

Function GetDirPath_ThisWorkbook(wkbname) As String
'Returns the path from a filespec
Dim x As Variant
x = Split(ThisWorkbook.FullName, Application.PathSeparator)
ReDim Preserve x(0 To UBound(x) - 1)
GetDir_ThisWorkbook = Join(x, Application.PathSeparator) & Application.PathSeparator
wkbname = GetDir_ThisWorkbook
End Function

Workbooks.Open Filename:=wkbname & "Days Cover.xls"

then on workbook sheet

Private Sub Workbook_Open()
GetDirPath_ThisWorkbook (wkbname)
End Sub
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
OK, gotcha.

you are trying to pass wkbname through the function, which is not necessary.

change GetDirPath_ThisWorkbook from a Function to a Sub, and take the variable out so it looks like this:

Sub GetDirPath_ThisWorkbook()
'Returns the path from a filespec
Dim x As Variant
x = Split(ThisWorkbook.FullName, Application.PathSeparator)
ReDim Preserve x(0 To UBound(x) - 1)

wkbname = Join(x, Application.PathSeparator) & Application.PathSeparator
End Sub

then on workbook sheet

Private Sub Workbook_Open()
GetDirPath_ThisWorkbook
End Sub

...and this should work. You don't have to pass the variable if it is public.

...Also, you know you could do away with the procedures and public variable completely by just using...

Workbooks.Open Filename:=ThisWorkbook.Path & Application.PathSeparator & "Days Cover.xls"


..or if you are going to need the variable for the file path elsewhere and really want it in a public (if this example is not the real problem) then just declare in module and use...


Private Sub Workbook_Open()
wkbname=ThisWorkbook.Path & Application.PathSeparator
End Sub
 
Upvote 0
Your exactually right thanks for that ,, that what happens when you copy code and try to manipulate it to do what you want.

I was actually using that variable quite a lot in the macros for opening other spreadsheets and that was the only way I could think of having a workbook that could be in any directory and shared accross a network without having to change the code thanks.
 
Upvote 0

Forum statistics

Threads
1,215,095
Messages
6,123,073
Members
449,093
Latest member
ripvw

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