Macro to open up Specific File

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have a macro to open up a specfic file

I need the code amended to that if the first file specified (BR1EMK Branch Sales.csv) is not found then to select another specified file BR1NE branch sales.csv

I need this part of the code amended to accomodate my request


Your assistance is most appreciated

Code:
 TheFile = Dir(ThePath & "BR1EMK*Sales" & "*.csv")

Code:
 Sub Open_Workbook()
ChDir ("C:\extract")
Sheets(2).Delete

Dim nb As Workbook, tw As Workbook, ts As Worksheet, TheFile As String, ThePath As String
ThePath = "C:\extract\"
TheFile = Dir(ThePath & "BR1EMK*Sales" & "*.csv")



'If TheFile = False Or IsEmpty(TheFile) Then Exit Sub
With Application
    .ScreenUpdating = False
    End With
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set nb = Workbooks.Open(TheFile)


nb.Sheets(1).Copy After:=Workbooks("BR1 Sales.xlsm").Sheets(1)
 Sheets(2).Name = "Imported Data"
 ChDir ("C:\my Documents")
 
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Maybe this:
VBA Code:
 Sub Open_Workbook()
ChDir ("C:\extract")
Sheets(2).Delete

Dim nb As Workbook, tw As Workbook, ts As Worksheet, TheFile As String, ThePath As String
ThePath = "C:\extract\"
TheFile = Dir(ThePath & "BR1EMK*Sales" & "*.csv")
If TheFile = "" Or IsEmpty(TheFile) Then
    TheFile = Dir(ThePath & "BR1NE*Sales" & "*.csv")
End If


'If TheFile = False Or IsEmpty(TheFile) Then Exit Sub
With Application
    .ScreenUpdating = False
    End With
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set nb = Workbooks.Open(TheFile)


nb.Sheets(1).Copy After:=Workbooks("BR1 Sales.xlsm").Sheets(1)
 Sheets(2).Name = "Imported Data"
 ChDir ("C:\my Documents")
 
End Sub
 
Upvote 0
Thanks for the help, code works perfectly
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
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