Create new sheet and copy data from another worksheet if match conditions

sbv1986

Board Regular
Joined
Nov 2, 2017
Messages
87
Hi all
I have workbook main with sheet(data) and folder with a lot of excel file, all excel files the same structure
sheet(data).column(A) have range look like
A
1123456
2124567
3125678
4126789
5...
.......
n... and more to n = last row

<tbody>
</tbody>

Now I want to have macro to do brown to folder choose all excel file, If excel file name have string like value in column(A) then:
1. Adding new sheet with sheetname = Range(Ai) value
2. Copy range(C10:AC54) from excel file have string to new sheet has just adding

Thanks./.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You can try this.
Assuming that Workbook "main" is in the same folder as the files to retrieved.
Code:
Sub t()
Dim fName As String, fPath As String, msh As Worksheet, wb As Workbook, nm As String
Set msh = ThisWorkbook.Sheets("Data")
fPath = ThisWorkbook.Path & "\"
fName = Dir(fPath & "*.xls*")
    Do While fName <> ""
        nm = Left(fName, InStr(fName, ".") - 1)
        If fName <> ThisWorkbook.Name Then
            If Application.CountIf(msh.Range("A:A"), nm) > 0 Then
                ThisWorkbook.Sheets.Add After:=Sheets(ThisWorkbook.Sheets.Count)
                ActiveSheet.Name = nm
                Set wb = Workbooks.Open(fPath & fName)
                wb.Sheets(1).Range("C10:AC54").Copy ThisWorkbook.Sheets(nm).Range("C10") 'Might need to adjust
                wb.Close False
            End If
        End If
        fName = Dir
    Loop
End Sub
 
Last edited:
Upvote 0
Thanks JLGWhiz for your respond, I do as your suggues but there's nothing happen.



Then I have not interpreted your objective correctily. Without accurate sheet, file, directory and data layout specifications, I cannot offer anything better. Sorry.
Regards, JLG
 
Last edited:
Upvote 0
Then I have not interpreted your objective correctily. Without accurate sheet, file, directory and data layout specifications, I cannot offer anything better. Sorry.
Regards, JLG
I've just found the way to run your code with a small change
Code:
Sub t()Dim fName As String, fPath As String, msh As Worksheet, wb As Workbook, nm As String
Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$
Set msh = ThisWorkbook.Sheets("Data")
Application.ScreenUpdating = False
fPath = ThisWorkbook.Path & "\"
InitialFoldr$ = fPath
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder to list Files from"
        .InitialFileName = InitialFoldr$
        .Show
        If .SelectedItems.Count <> 0 Then
        fName = Dir(fPath & "*.xl*")
        Do While fName <> ""
        nm = Mid(fName, 8, 6)
        If fName <> ThisWorkbook.Name Then
            If Application.CountIf(msh.Range("A:A"), nm) > 0 Then
                ThisWorkbook.Sheets.Add After:=Sheets(ThisWorkbook.Sheets.Count)
                On Error Resume Next
                ActiveSheet.Name = nm
                On Error GoTo 0
                Set wb = Workbooks.Open(fPath & fName)
                [COLOR=#333333]wb.Sheets(1).Range("C10:AC54").Copy ThisWorkbook.Sheets(nm).Range("C10") 'Might need to adjust[/COLOR]
                wb.Close False
            End If
        End If
        fName = Dir
    Loop
    End If
    End With
Application.ScreenUpdating = True
End Sub

Important I change
Code:
[COLOR=#333333]nm = Left(fName, InStr(fName, ".") - 1)[/COLOR]
to
Code:
nm = Mid(fName, 8, 6)
This work well with file have name like xxxxxxx123456yyyyyyyyy.xlsx
But when file name like xxx123456yyyyyyyyy.xlsx code don't work

Do you have any way to do with code I've just find above? JLGWhiz
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,760
Members
449,095
Latest member
m_smith_solihull

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