How to make a macro go to a certain folder directly to complete the macro run

agohir

New Member
Joined
Aug 5, 2016
Messages
14
Hello,

I have the below code to extract data from a certain excel file (xlsx type) but I want to include two things in the macro.

1) The macro should directly open a certain folder directory for me to select the file from for example: "C\My Documents\"

2) And two if I press cancel instead of opening the document in the folder directory I want it to stop the macro and not show the debug window.

Macro:

Sub OpenPastCFFData()
'
' OpenPastCFFData Macro
'
Dim strSelectedLDCFile As String
Dim path As String


strSelectedLDCFile = Application.GetOpenFilename(", *.xlsx", , "Select Folder with desired Data Files")

Workbooks.Open Filename:=strSelectedLDCFile

Windows("Template Combination Tool.xlsm").Activate

Sheets("COMBINE DATA BUTTONS").Select

Range("M2").Select

'
End Sub


Any help highly appreciated.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
small modification to your code.


Code:
Sub OpenPastCFFData()
'
' OpenPastCFFData Macro
'
Dim strSelectedLDCFile As String
Dim path As String




strSelectedLDCFile = Application.GetOpenFilename(", *.xlsx", , "Select Folder with desired Data Files")


If strSelectedLDCFile = False Then
MsgBox "The operation was cancelled", vbInformation
Exit Sub
End If


Workbooks.Open Filename:=strSelectedLDCFile


Windows("Template Combination Tool.xlsm").Activate


Sheets("COMBINE DATA BUTTONS").Select


Range("M2").Select


'
End Sub
 
Upvote 0
And to address your first request, add this line just after the Dim statements...

Code:
ChDir "C:\MyDocuments"
Cheers,

tonyyy
 
Upvote 0
And to address your first request, add this line just after the Dim statements...

Code:
ChDir "C:\MyDocuments"
Cheers,

tonyyy


Thanks for the help, it worked.

I have another question. There is a different macro in vba that imports data from multiple files and than I put additional macro lines to do a data quality check on certain columns. The issue is that the macro runs and doesn't give me an error but the data quality check column don't fill up, they are empty. As in the data quality check macro didn't work.

Here's the code and any help is appreciated. Thanks.

'Subcategories=TotalCategories in Admin Costs


Range("A2").Activate

Selection.CurrentRegion.Select

TotRows = Selection.Rows.Count
LastCol = Selection.Columns.Count

Range("A2").Cells(1, LastCol + 1).Select

ActiveCell.Formula = "=IF(SUM(C2:F2)<>G2,""Flag"","""")"

Range(Selection, Selection.Offset(TotRows - 2, 0)).Select
Selection.FillDown

Range("A2").Select

'Measure_ID


Range("A2").Activate

Selection.CurrentRegion.Select

TotRows = Selection.Rows.Count
LastCol = Selection.Columns.Count

Range("A2").Cells(1, LastCol + 1).Select

ActiveCell.Formula = "=VLOOKUP(B2,'Program Map'!$A:$B,2,FALSE)"

Range(Selection, Selection.Offset(TotRows - 2, 0)).Select
Selection.FillDown

Range("A2").Select

'Measure_ID


Range("A2").Activate

Selection.CurrentRegion.Select

TotRows = Selection.Rows.Count
LastCol = Selection.Columns.Count

Range("A2").Cells(1, LastCol + 1).Select

ActiveCell.Formula = "=VLOOKUP(E2,'LDC Map'!$A:$B,2,FALSE)"

Range(Selection, Selection.Offset(TotRows - 2, 0)).Select
Selection.FillDown
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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