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

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.
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,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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