Can you lookup a directory name stored in a sheet and use it to ChDir ?

kevanr

New Member
Joined
Nov 7, 2019
Messages
20
Hi
I have saved a directory location in a cell in a worksheet. In my macro I want to retrieve the directory and change to that directory. I have used the following simple code:

Sub TestingChDir()

Dim sDirectory As String
sDirectory = Worksheets("Assumptions").Range("J17")

MsgBox "sDirectory " & sDirectory

ChDir sDirectory

End Sub

I get the following error 76:

1583384817498.png

Can someone please help me with this?

Many thanks
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You can put here the data of cell J17.
And an image of your explorer showing that the directory does exist.

Try this

VBA Code:
Sub TestingChDir()
  Dim sDirectory As String
  sDirectory = Worksheets("Assumptions").Range("J17")
  
  If Dir(sDirectory, vbDirectory) = "" Then
    MsgBox "Does not exists"
  Else
    ChDir sDirectory
  End If

End Sub
 
Upvote 0
You can put here the data of cell J17.
And an image of your explorer showing that the directory does exist.

Please provide that information.
 
Upvote 0
In cell J17 you must put the data without quotes:

C:\TestChDir Foler
 
Upvote 0
If you are going to open the file in cell J8, it is not necessary to change the directory, it can be like this:

VBA Code:
Sub TestingChDir()
  Dim sDirectory As String, sFile As String
  sDirectory = Worksheets("Assumptions").Range("J17").Value
  If Right(sDirectory, 1) <> "\" Then sDirectory = sDirectory & "\"
  sFile = Worksheets("Assumptions").Range("J18").Value
  If Dir(sDirectory & sFile) = "" Then
    MsgBox "Does not exists"
  Else
    Workbooks.Open (sDirectory & sFile)
  End If

End Sub
 
Upvote 0
If you are going to open the file in cell J8, it is not necessary to change the directory, it can be like this:

VBA Code:
Sub TestingChDir()
  Dim sDirectory As String, sFile As String
  sDirectory = Worksheets("Assumptions").Range("J17").Value
  If Right(sDirectory, 1) <> "\" Then sDirectory = sDirectory & "\"
  sFile = Worksheets("Assumptions").Range("J18").Value
  If Dir(sDirectory & sFile) = "" Then
    MsgBox "Does not exists"
  Else
    Workbooks.Open (sDirectory & sFile)
  End If

End Sub

Thank you DanteAmor, that works perfectly, your help has been greatly appreciated !!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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