VBA to allow directory from cell

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
Sorry if title is confusing, i have this vba script which works fine

It loads up a sheet from another workbook.

Code:
Sub LoadSheet()

Dim FileName As String
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Dim ActiveListWB As Workbook


ChDrive "C:"
ChDir "C:\users\jay\desktop"


    Set WS2 = ActiveWorkbook.Sheets("Data")
    FileName = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*),*.xls*", _
                                               Title:="Select Timesheet to Import", _
                                               MultiSelect:=False)
    
    If FileName = "False" Then
            Exit Sub
        Else
            Set ActiveListWB = Workbooks.Open(FileName)
    End If
    
    Set WS1 = ActiveListWB.Sheets("NewData")
    
    WS1.UsedRange.Copy WS2.Range("A1")
    
    ActiveWorkbook.Close False


End Sub

i want to change this part

ChDrive "C:"
ChDir "C:\users\jay\desktop"


so i can change drive and directory from a cell

ive tried using:
drive1 = Range("B3")
directory1 = Range("B4")

and

ChDrive " & drive1 & "
ChDir " & directory1 & "

but its giving error
db933d0748.png


any help appreciated
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Re: Help modifying this vba to allow directory from cell

No need for " &.
Code:
ChDrive drive1
ChDir directory1
 
Upvote 0
Re: Help modifying this vba to allow directory from cell

Try
Code:
ChDrive Range("A20").Value
ChDir Range("A21").Value
Ensuring that the drive letter end with a :
& the path ends with a \
 
Upvote 0
Re: Help modifying this vba to allow directory from cell

both work fine

thankyou :)
 
Upvote 0
Re: Help modifying this vba to allow directory from cell

Glad WE could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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