subscript out of range when search for sheet to pull data (could rename sheet to avoid error?)

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
428
Office Version
  1. 2016
Platform
  1. Windows
Hi experts ,
here is code will pull all of data from all of files are existed in the same folder for sheet name is MATCH , so if there files don't contain MATCH sheet then will shows error subscript out of range in this line
VBA Code:
With .Sheets("MATCH")

so what I want show message box" the sheet is not existed, do you want rename all of sheets in all closed file to MATCH sheet" and contains two choices if press ok , then will rename to MATCH sheet and pull data , if I press no , then will just pull data for just files contains MATCH sheet without rename MATCH sheet for files don't contain MATCH sheet .
last thing when rename sheet to MATCH sheet for closed files should search for the first sheet to rename to MATCH sheet and ignore the others files contain MATCH sheet .
File-Copy-icon.png

VBA Code:
Sub CopyRangeFromSetFolder()

Dim desWS As Worksheet, wb As Workbook, lRow As Long
Dim wbNm As String, Fld As String

Application.ScreenUpdating = False

Set desWS = ThisWorkbook.Sheets("Sheet1")
desWS.Range("A2").CurrentRegion.Offset(1, 0).ClearContents
' define path to set folder, ending in \
Fld = ThisWorkbook.Path & "\"
'get first file with wildcard match
    wbNm = Dir(Fld & "*.xls*", vbNormal)
    ' loop while there's another matching file
    Do While wbNm <> ""
       ' Check it's not this workbook
        If wbNm <> ThisWorkbook.Name Then
            With GetObject(Fld & wbNm)
                With .Sheets("MATCH")
                    lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                    .Range("A2:D" & lRow).Copy
                    desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
                End With
                ' close the file without saving
                Application.DisplayAlerts = False
                .Close False
                Application.DisplayAlerts = True
            End With
            Else
            MsgBox "File Elmarghanie Brand .xlsm not found"
     Exit Sub
        End If

        ' get next matching file
        wbNm = Dir()
    Loop

Application.ScreenUpdating = True
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: add message box and rename the same sheet for all of closed files in the folder
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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