Look for multple extensions

Dutchmaste

New Member
Joined
Jun 14, 2016
Messages
23
Office Version
  1. 365
Platform
  1. Windows
I currently use the below macro. But as shown on line 7 & 15 this only looks for a filename with .xlsx
But unfortunatelly sometimes the extension of the files I receive change into either .xls, .xlsx or .xlsm
Due to this I currently have to edit the macro manually to look for the other extension. But I want it to do this automatically. But I can't seem to figure it out so it works for both actions on line 7 and 15.
Code:
Line1:    Dim curWorkbook As WorkbookLine2:    Set curWorkbook = ActiveWorkbook
Line3:    Application.AskToUpdateLinks = False
Line4:    Application.DisplayAlerts = False
Line5:    fName = curWorkbook.Sheets("HS Lijst").Range("K2")
Line6:    Sheets("DATA").Select
Line7:    Workbooks.Open FileName:="O:\Splitsen\" & fName & ".xlsx"
Line8:    Cells.Select
Line9:    Selection.Copy
Line10:    curWorkbook.Activate
Line11:    Cells.Select
Line12:    ActiveSheet.Paste
Line13:    Sheets("HS lijst").Select
Line14:    Range("C4").Select
Line15:    Windows(fName & ".xlsx").Activate
Line16:    Application.CutCopyMode = False
Line17:    ActiveWindow.Close
Line18:    Application.CutCopyMode = True
Line19:    curWorkbook.Activate
Line20:    Application.DisplayAlerts = True
Line21:    Application.AskToUpdateLinks = True
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi,

Something like this should work:
Code:
Sub Test()
    Dim fName        As String
    Dim curWorkbook  As Workbook
    
    Set curWorkbook = ActiveWorkbook
    Application.AskToUpdateLinks = False
    Application.DisplayAlerts = False
    fName = curWorkbook.Sheets("HS Lijst").Range("K2")
    fName = Dir("O:\Splitsen\" & fName & ".xls*")
    Sheets("DATA").Select
    Workbooks.Open Filename:="O:\Splitsen\" & fName
    Cells.Select
    Selection.Copy
    curWorkbook.Activate
    Cells.Select
    ActiveSheet.Paste
    Sheets("HS lijst").Select
    Range("C4").Select
    Windows(fName).Activate
    Application.CutCopyMode = False
    ActiveWindow.Close
    Application.CutCopyMode = True
    curWorkbook.Activate
    Application.DisplayAlerts = True
    Application.AskToUpdateLinks = True
End Sub
This line:
Code:
fName = Dir("O:\Splitsen\" & fName & ".xls*")
will search the folder using a wildcard and will return the first one found into the string fName.

Regards,
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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