Change Directories

Michael68

New Member
Joined
Nov 20, 2019
Messages
1
I am having a simple problem of getting the directory to change using VBA.
This_PC is a variable

This_PC = (Environ$("Username"))
New_Dir = "C:\Users\" & This_Pc & "\Downloads"
ChDir New_Dir

When I execute the following I open in my present directory instead of the directory I want to change to.

FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a file to open", _
FileFilter:="Excel Files *.xls* (*.xls*),*.xlsm (*xlsm")
If FileToOpen <> False Then
Workbooks.Open FileName:=FileToOpen
File_Name = ActiveWorkbook.Name
Else
MsgBox "No File Selected.", vbExclamation, "Sorry!"
GoTo End_Of_File
End If
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try this

VBA Code:
Sub test()
  Dim This_Pc As String, New_Dir As String, File_Name As String
  This_Pc = (Environ$("Username"))
  New_Dir = "C:\Users\" & This_Pc & "\Downloads"
  With Application.FileDialog(msoFileDialogFilePicker)
    .Title = "Please choose a file to open"
    .Filters.Clear
    .Filters.Add "Excel Files", "*.xls*"
    .AllowMultiSelect = False
    .InitialFileName = New_Dir & "\"
    If .Show Then
      Workbooks.Open .SelectedItems.Item(1)
      File_Name = ActiveWorkbook.Name
    Else
      MsgBox "No File Selected.", vbExclamation, "Sorry!"
    End If
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,960
Messages
6,122,479
Members
449,088
Latest member
Melvetica

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