Macro to Browse to C:\extract

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,589
Office Version
  1. 2021
Platform
  1. Windows
I have the following code, but code does not select directory "C:\extract. It selects previous Directory that was selected


Kindly amend my code so that C:\extract is selected. If I manually select C:\extract when selecting Macro the rest of the code works. I only the section of code amended so that the directory C:\extract is selected and not the previous folder that was selected


Code:
 Sub Open_UV_Workbook()
    'Clear the previous folder selection
    ChDrive ("C:")
    ChDir ("\")

    'Browse to the "C:\extract" folder
    ChDir ("C:\extract")

    'Clear the contents of the "Used Vehicles" sheet
    With Sheets("Used Vehicles")
        .Range("A1:AQ1500").ClearContents
    End With

    'Declare variables
    Dim nb As Workbook, ts As Worksheet, A As Variant
    Dim rngDestination As Range
    Dim filename As Variant
    Dim File As String
    File = Dir("C:\extract")

    'Select the "Used Vehicles" sheet
    With Sheets("Used Vehicles")
        .Select
    End With

    On Error Resume Next
    Set rngDestination = Application.Range("'Used vehicles'!A1")
    On Error GoTo 0

    If rngDestination Is Nothing Then Exit Sub  'User canceled

    'Select the CSV file to open
    MsgBox ("Select Used Vehicle Report")

    With Application.FileDialog(msoFileDialogFilePicker)
        .ButtonName = "Select"
        With .Filters
            .Clear
            .Add "CSV Files", "*.csv"
        End With
        .FilterIndex = 1
        .InitialFileName = "*Used Vehicles*.csv"
        .Title = "Select CSV file"
        If .Show = 0 Then Exit Sub
        filename = .SelectedItems(1)
    End With

    Debug.Print filename
    Application.ScreenUpdating = False

    'Open the CSV file and copy its contents to the "Used Vehicles" sheet
    Dim srcWorkbook As Workbook
    Set srcWorkbook = Workbooks.Open(filename:=filename, local:=True)
    ThisWorkbook.Activate

    srcWorkbook.Sheets(1).Range("A:AQ").Copy
    rngDestination.PasteSpecial Paste:=xlPasteValues
    rngDestination.PasteSpecial Paste:=xlPasteFormats
    Application.CutCopyMode = False

    srcWorkbook.Close SaveChanges:=False
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Can't see why it does what you say unless you're storing the last path in a module level variable that you're not showing since
a) the dialog is supposed to provide only a starting point if you use that property at all, and
b) the InitialFileName property is supposed to be the complete starting path, not a file name. At least that's how I remember it.
So try .InitialFileName = "C:\extract\"
 
Upvote 0
Solution
thanks for the help. I changed this parts of the code from

Code:
 .InitialFileName = "*Used Vehicles*.csv"
to
Code:
 .InitialFileName = "C:\extract\*Used Vehicles*.csv"

Code now selects correct folder
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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