Solved

Need to extract cells to  a new workbooks based on a unique criteria in a certain column

Posted on 2013-05-16
5
490 Views
Last Modified: 2013-05-16
Hi

Here is my issue:

Attached workbook contains a sample worksheet. I need to know how to extract the workbooks containing certain criteria

For example:

Column G contains the division
I want the macro to look into column AP first cell and for that cells content extract all details regarding that division into another workbook and save with the division name.

It would look for division A in column G and then copy all related fields for row 17 - 22 since those contain division A then export all those fields including  row 15 and 16 as headers into another workbook and save,


I need an answer very quickly please help

Thanks
ZHR-M04-macro.xlsm
0
Comment
Question by:FAH_
  • 3
  • 2
5 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39173626
Here is a macro that will export your data:
Sub ExportData()
Dim cel As Range, rg As Range, rgcopy As Range, rgData As Range, rgFilters As Range
Dim wb As Workbook
Dim flPath As String
flPath = Application.GetSaveAsFilename(ActiveWorkbook.FullName, FileFilter:="Excel workbooks (*.xlsx), *.xlsx", _
    Title:="Pick any workbook in the desired folder, then click 'Save'")
If flPath = "False" Then Exit Sub
Application.ScreenUpdating = False
flPath = Left(flPath, InStrRev(flPath, Application.PathSeparator))
With Worksheets("Table")
    Set rg = .Range("DF_Grid_1")
    Set rgData = rg.Offset(1, 0).Resize(rg.Rows.Count - 1, rg.Columns.Count)
    Set rgFilters = .Range("AP16")
    Set rgFilters = Range(rgFilters, rgFilters.End(xlDown))
End With
rgData.Cells(1, 1).AutoFilter
For Each cel In rgFilters
    If cel.Value <> "" Then
        rgData.AutoFilter Field:=2, Criteria1:=cel.Value
        Set rgcopy = rgData.SpecialCells(xlCellTypeVisible)
        If (rgcopy.Rows.Count > 1) Or (rgcopy.Areas.Count > 1) Then
            Set wb = Workbooks.Add
            rg.Copy ActiveSheet.Cells(1, 1)
            wb.SaveAs flPath & cel.Value & ".xlsx", FileFormat:=51
            wb.Close SaveChanges:=False
        End If
    End If
Next
rgData.AutoFilter Field:=2
End Sub

Open in new window

0
 

Author Comment

by:FAH_
ID: 39173639
How could I manipulate so It will export it based on the division name i.e.

AA.xlsx
A.xlsx
B.xlsx

etc
0
 
LVL 81

Expert Comment

by:byundt
ID: 39173662
FAH_,
Did you try the macro?

I anticipated your request, and wrote the macro accordingly.

Brad
0
 

Author Comment

by:FAH_
ID: 39173728
run-time error 1004

no cells were found
0
 

Author Comment

by:FAH_
ID: 39173732
Got it w

It works now


Thanks a lot :)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question