Open & copy specific files

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have several files in Directory C:\Sales for eg increases BR.Aug 2017.xls, increases CABR.Aug 2017xls


I have tried to write code to open a specific name after the text increases contained in the name of the workbook for eg "BR" that is after increases


when running the code below nothing happens


Kindly amend my code so that when "BR" appears after increases that file is selected and then copied


I think that it is only this section of code that needs to be amended


Your assistance is most appreciated

Code:
 If objFSO.GetExtensionName(objMyFile) = "csv" And StrConv(Left(objFSO.GetBaseName(objMyFile), 4), vbUpperCase) = "in BR" Then

Code:
 Sub Open_Spec_Files()
    
    Dim objFSO       As Object
    Dim objMyFolder  As Object
    Dim objMyFile    As Object
    Dim wbMyWorkBook As Workbook
    Dim lngLastRow   As Long
    
    Application.ScreenUpdating = False
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objMyFolder = objFSO.GetFolder("C:\Sales") 
   
    For Each objMyFile In objMyFolder.Files
      
        If objFSO.GetExtensionName(objMyFile) = "csv" And StrConv(Left(objFSO.GetBaseName(objMyFile), 4), vbUpperCase) = "in BR" Then
            '...set the wbMyWorkBook variable by opening the workbook and copy the data from range A4:O[lngLastRow].
            Set wbMyWorkBook = Workbooks.Open(objMyFolder & "\" & objMyFile.Name)
            'Finds the last row across columns A to O (inclusive) of the first sheet (note a csv file can only have one sheet) in the *.csv file
            On Error Resume Next
                lngLastRow = wbMyWorkBook.Sheets(1).Range("A:O").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            On Error GoTo 0
            If lngLastRow >= 4 Then
                With wbMyWorkBook
                    .Sheets(1).Range("A4:O" & lngLastRow).Copy Destination:=ThisWorkbook.ActiveSheet.Range("A2")
                    .Close SaveChanges:=False 'Close the *.csv without saving and changes
                End With
            End If
        End If
    Next objMyFile
   Set objFSO = Nothing
    Set objMyFolder = Nothing
    
    Application.ScreenUpdating = True

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
This will never be true for 2 reasons, firstly you are extracting 4 characters from the filename and 'in BR' is 5 characters and secondly you are converting those 4 characters to upper case and 'in BR' is mixed case, i.e. both upper and lower.
Code:
StrConv(Left(objFSO.GetBaseName(objMyFile), 4), vbUpperCase) = "in BR"
Also, with a filename like 'increases BR.Aug 2017.xls' the first 4 characters are 'incr'.
 
Upvote 0
Thanks for the reply Norie

I will change 4 to 5. How do I amend "vbUpperCase" for mixed characters ?
 
Upvote 0
You still won't get a match as the file name doesn't start with 'In BR'.
 
Upvote 0
I was just using that as an example if file name was "In BR.Aug 2017.xls"

Please advise how to amend "vbUpperCase" to accept any case
 
Upvote 0
I wouldn't use StrConv, I'd use UCase on both the filename and the term you are trying to match.

I'd also be more likely to use Like with a wildcard to find a file based on what the name starts with, in fact you could do the whole thing, including checking the extension, in one go using Like.
Code:
If UCase(objMyFile.Name) Like  UCase("In BR*.CSV") Then
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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