VBA: Specify drive letter for filepath to open?

Coyner

New Member
Joined
Mar 23, 2021
Messages
6
Office Version
  1. 2010
Hi,

Im trying to get the user to specify which drive they have a filepath mapped to in order to open and manipulate an excel spreadsheet but I can't get it right, any ideas where the below code is wrong?

sDrive = InputBox("What drive letter have you mapped to?")

sDrive = sDrive & ":"
'Open downloaded file
ChDir "sDrive\test folder\2021"
Workbooks.OpenText Filename:="sDrive\test folder\2021"
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Welcome to the Board!

It is important to note that in VBA, anything enclosed within double-quotes is treated as literal text.
Therefore, all variables must be outside of the double-quotes to be treated as variables and not literal text.

So your references to:
VBA Code:
"sDrive\test folder\2021"
should be:
VBA Code:
sDrive & "\test folder\2021"
 
Upvote 0
Along with what Joe has said, that won't work unless the drive is the active drive, you should use
VBA Code:
chDrive sDrive
ChDir sDrive & "\test folder\2021"
although you don't need to drive the drive/directory to open a file
 
Upvote 0
Solution
So is this looking right:

sDrive = InputBox("What drive letter have you mapped to?")

sDrive = sDrive & ":"
'Open downloaded file
ChDrive sDrive
ChDir sDrive & "\Test file\2021"
Workbooks.OpenText Filename:=sDrive & "\Test file\2021\namedworkbook"


Also, i then want to save the amended file in a folder, how should i structure that code? Below is giving an error? Really appreciate this advice.

ActiveWorkbook.SaveAs Filename:= _
([sDrive & \Test file\2021\"]) & Format(Date, "MMMM YYYY") & " Structure" & ".xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
 
Upvote 0
Try it like
VBA Code:
ActiveWorkbook.SaveAs sDrive & "\Test file\2021\" & Format(Date, "MMMM YYYY") & " Structure" & ".xlsx", 51
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,857
Messages
6,121,948
Members
449,056
Latest member
FreeCricketId

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