Macro does not change to directory selected in Macro

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have a macro below to open a workbook and select directory C:\extract and to copy the data


when running the directory "C:\extract" directory is not selected

Kindly check & amend my code

Code:
 Sub Open_Workbook()
       ChDir "C:\extract\"

    
    Dim nb As Workbook, ts As Worksheet, A As Variant
    Dim rngDestination As Range
   
    Sheets("Imported Data").Select
    With Range("a1:AD2000")
    .ClearContents
    End With
    
    Set ts = ActiveSheet
    
    On Error Resume Next
    Set rngDestination = ts.[A1]
    On Error GoTo 0
    If rngDestination Is Nothing Then Exit Sub
    MsgBox ("select Sales Data Report Inquiry.xlsx File")
     ChDir "C:\extract\"
    With Application.FileDialog(msoFileDialogFilePicker)
.InitialFileName = "C\:Extract\sales*.xlsx"
If .Show = 0 Then Exit Sub
A = .SelectedItems(1)
End With

      If A = False Or IsEmpty(A) Then Exit Sub
    
    Application.ScreenUpdating = False
    
    Set nb = Workbooks.Open(A)
    ThisWorkbook.Activate
    
   nb.Sheets(1).Range("A1:AD2000").Copy
    rngDestination.PasteSpecial Paste:=xlPasteValues
    rngDestination.PasteSpecial Paste:=xlPasteFormats
    Application.CutCopyMode = False
         
        
    nb.Close savechanges:=False

  End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You have a typo on this line
VBA Code:
.InitialFileName = "C\:Extract\sales*.xlsx"
the colon is in the wrong place, but it will no show files that start with "sales"
 
Upvote 0
Solution
Thanks for picking this up Fluff. I did not issue at all and could not understand why I could not get the correct directory to be selected
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,167
Members
448,554
Latest member
Gleisner2

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