VAB - Replace \Username\ in filepath with cell range value does not work

ONIWE

New Member
Joined
Jun 29, 2010
Messages
27
Hello Good People,

When I replace the username in a file path as below, the code does not run unless i use the actual username.
Someone please tell me what I am doing wrong.

"C:\Users\Jackdniels\OneDrive - This is the actual path and it works fine

"C:\Users\" & fromPath & "\OneDrive - This is the replacement that does not work.
fromPath = Sheets("Summary Template").Range("b6")
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
What does "does not work" mean? What happens that is different than what you expect? What is the value of fromPath (B6) at the point this executes? How is this path used in your code? Please show all the code, not just the one line where you think there is a problem.
 
Upvote 0
it means I'm getting constant expression required.
1695351976615.png


Sub ExtractData()
Application.ScreenUpdating = False
Dim wkbDest As Workbook
Dim wkbSource As Workbook
Dim fPath As String

fPath = Sheets("Summary").range("b6").Value

Set wkbDest = ThisWorkbo

With Application
.ScreenUpdating = False
.AskToUpdateLinks = False
.DisplayAlerts = False
End With


'Const strPath As String = "C:\Users\jackdaniels\OneDrive - headoffice\Desktop\MACRO 2\"
Const strPath As String = "C:\Users\" & fPath & "\OneDrive - headoffice\Desktop\MACRO 2\"
ChDir strPath

strExtension = Dir("*Evaluation_*")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPath & strExtension)


Something to do with a dynamic range from what I have read so far.
 
Upvote 0
You need to dim it as a string, not a const. you can’t use variables in a const
 
Upvote 0
VBA Code:
Const strPath As String = "C:\Users\" & fPath & "\OneDrive - headoffice\Desktop\MACRO 2\"
The value of a Const must be a constant that is determined at compile time. You are using the "&" operator which is evaluated at runtime and so cannot be used in an assignment of a Const value.
Do this instead
VBA Code:
Dim strPath As String
strPath = "C:\Users\" & fPath & "\OneDrive - headoffice\Desktop\MACRO 2\"
 
Upvote 0

Forum statistics

Threads
1,215,329
Messages
6,124,301
Members
449,149
Latest member
mwdbActuary

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