Help with VBA

danpan

New Member
Joined
Aug 27, 2020
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi all I have a workbook that comes out daily with stats about work related issues, with a number of different tabs , and I need to extract the data from one worksheet that contains 10000-30000 rows of data but I only want data based on a list of names that relate to my tream. the names can be found in that particular worksheet in a particular column. I have managed to source the vba below but it does not paste any data to the new workbook . I dont know if it is the actual filter part that is not working.
VBA Code:
Sub FilterAndCopyData()
    Dim fso As Object
    Dim fldr As Object
    Dim sFldr As String
    Dim wbk As Workbook
    Dim wks As Worksheet
    Dim rng As Range
    Dim names As Variant
    Dim name As Variant
    Dim newWbk As Workbook
    Dim newWks As Worksheet

    ' List of names to filter
    names = Array("Name1", "Name2", "Name3") ' Add more names as needed

    ' Create new workbook for filtered data
    Set newWbk = Workbooks.Add
    Set newWks = newWbk.Sheets(1)

    ' Path to the folder
    sFldr = "C:\Path\To\Folder"

Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldr = fso.GetFolder(sFldr)

    ' Call the recursive procedure
    ProcessFolder fldr, names, newWks

    ' Save the new workbook with filtered data
    newWbk.SaveAs "C:\Path\To\NewWorkbook.xlsx"
End Sub

Sub ProcessFolder(fldr As Object, names As Variant, newWks As Worksheet)
    Dim subFldr As Object
    Dim file As Object
    Dim wbk As Workbook
    Dim wks As Worksheet
    Dim rng As Range
    Dim name As Variant

    For Each subFldr In fldr.SubFolders
        ProcessFolder subFldr, names, newWks ' Recursive call for subfolders
    Next subFldr

    For Each file In fldr.Files
        If Right(file.Name, 4) = "xlsx" Or Right(file.Name, 3) = "xls" Then ' Check if file is an Excel workbook
            Set wbk = Workbooks.Open(file.Path)
            On Error Resume Next ' Ignore error if worksheet not found
            Set wks = wbk.Sheets("activity")
            On Error GoTo 0 ' Reset error handling

            If Not wks Is Nothing Then ' Check if worksheet was found
                For Each name In names
                    Set rng = wks.Range("G1:G" & wks.Cells(wks.Rows.Count, "G").End(xlUp).Row) ' Adjust range as needed

                    ' Filter data based on name and copy to new worksheet
                    rng.AutoFilter Field:=1, Criteria1:="=*" & name & "*", Operator:=xlAnd
rng.SpecialCells(xlCellTypeVisible).Copy Destination:=newWks.Cells(newWks.Cells(newWks.Rows.Count, "A").End(xlUp).Row + 1, "A")
                    rng.AutoFilter ' Remove filter

                    ' Clear objects for next iteration
                    Set rng = Nothing
                Next name

                Set wks = Nothing ' Clear objects for next iteration
            End If

            wbk.Close SaveChanges:=False ' Close workbook without saving changes

            Set wbk = Nothing ' Clear objects for next iteration
        End If
    Next file

End Sub
Can any one provide some help or if they know of a vba that does so. many thanks!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi all I have a workbook that comes out daily with stats about work related issues, with a number of different tabs , and I need to extract the data from one worksheet that contains 10000-30000 rows of data but I only want data based on a list of names that relate to my tream. the names can be found in that particular worksheet in a particular column. I have managed to source the vba below but it does not paste any data to the new workbook . I dont know if it is the actual filter part that is not working.
VBA Code:
Sub FilterAndCopyData()
    Dim fso As Object
    Dim fldr As Object
    Dim sFldr As String
    Dim wbk As Workbook
    Dim wks As Worksheet
    Dim rng As Range
    Dim names As Variant
    Dim name As Variant
    Dim newWbk As Workbook
    Dim newWks As Worksheet

    ' List of names to filter
    names = Array("Name1", "Name2", "Name3") ' Add more names as needed

    ' Create new workbook for filtered data
    Set newWbk = Workbooks.Add
    Set newWks = newWbk.Sheets(1)

    ' Path to the folder
    sFldr = "C:\Path\To\Folder"

Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldr = fso.GetFolder(sFldr)

    ' Call the recursive procedure
    ProcessFolder fldr, names, newWks

    ' Save the new workbook with filtered data
    newWbk.SaveAs "C:\Path\To\NewWorkbook.xlsx"
End Sub

Sub ProcessFolder(fldr As Object, names As Variant, newWks As Worksheet)
    Dim subFldr As Object
    Dim file As Object
    Dim wbk As Workbook
    Dim wks As Worksheet
    Dim rng As Range
    Dim name As Variant

    For Each subFldr In fldr.SubFolders
        ProcessFolder subFldr, names, newWks ' Recursive call for subfolders
    Next subFldr

    For Each file In fldr.Files
        If Right(file.Name, 4) = "xlsx" Or Right(file.Name, 3) = "xls" Then ' Check if file is an Excel workbook
            Set wbk = Workbooks.Open(file.Path)
            On Error Resume Next ' Ignore error if worksheet not found
            Set wks = wbk.Sheets("activity")
            On Error GoTo 0 ' Reset error handling

            If Not wks Is Nothing Then ' Check if worksheet was found
                For Each name In names
                    Set rng = wks.Range("G1:G" & wks.Cells(wks.Rows.Count, "G").End(xlUp).Row) ' Adjust range as needed

                    ' Filter data based on name and copy to new worksheet
                    rng.AutoFilter Field:=1, Criteria1:="=*" & name & "*", Operator:=xlAnd
rng.SpecialCells(xlCellTypeVisible).Copy Destination:=newWks.Cells(newWks.Cells(newWks.Rows.Count, "A").End(xlUp).Row + 1, "A")
                    rng.AutoFilter ' Remove filter

                    ' Clear objects for next iteration
                    Set rng = Nothing
                Next name

                Set wks = Nothing ' Clear objects for next iteration
            End If

            wbk.Close SaveChanges:=False ' Close workbook without saving changes

            Set wbk = Nothing ' Clear objects for next iteration
        End If
    Next file

End Sub
Can any one provide some help or if they know of a vba that does so. many thanks!
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
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