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
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