Macro merger of filenames that start with a specific name

RAZE

New Member
Joined
Feb 27, 2020
Messages
6
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. Web
Hello,



I have a macro that consolidates all filenames that start with "BDRIDGE" and have a "DATAx" sheet.name from all containing subfolders. The path of the subolders being added in the 1st sheet of the macro file.

My problem is that the macro does not retrieve anything as it only runs and creates a blank new sheet without data.



Any help please ?

VBA Code:
Sub CopWKBooksInFolder()

Dim WS As Worksheet

Dim myfolder As String

Dim Str As String

Dim a As Single

Dim sht As Worksheet



Str = Application.InputBox(prompt:="Search only sheet names containing this string:", Title:="Search worksheet whose name contain this string:", Type:=2)



On Error Resume Next

Set Rng = Application.InputBox(prompt:="Select a cell range containing paths to folders" _

, Title:="Select a cell range", Default:=ActiveCell.Address, Type:=8)

On Error GoTo 0



Set WS = Sheets.Add



For Each cell In Rng



If Dir(cell.Value, vbDirectory) <> "" Then



chk = 0

Value = Dir(cell.Value)



Do Until Value = ""

If Value = "." Or Value = ".." Then

Else

If Left(Value, 6) = "BRIDGE" Then

On Error Resume Next

Workbooks.Open Filename:=cell.Value & Value

If Err.Number > 0 Then

Else

On Error GoTo 0

For Each sht In ActiveWorkbook.Worksheets



If InStr(sht.Name, Str) <> 0 Then



If sht.Range("A1") <> "" Then

Lrow = WS.Range("A" & Rows.Count).End(xlUp).Row + 1



If chk = 0 Then

sht.Range("A1").CurrentRegion.Copy Destination:=WS.Range("A" & Lrow)

chk = 1

Else

Set crng = sht.Range("A1").CurrentRegion

Set crng = crng.Offset(1, 0)

Set crng = crng.Resize(crng.Rows.Count - 1)

crng.Copy Destination:=WS.Range("A" & Lrow)

End If

End If

End If

Next sht

End If

Workbooks(Value).Close False

On Error GoTo 0

End If

End If

Value = Dir

Loop

End If

Next cell

Cells.EntireColumn.AutoFit

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Correction: filenames start with "BRIDGE"
 
Upvote 0

Forum statistics

Threads
1,214,424
Messages
6,119,407
Members
448,894
Latest member
spenstar

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