Solved

Excel VB to Copy out rows of data based upon Multiple criteria

Posted on 2011-03-17
1
567 Views
Last Modified: 2012-06-27
Hello all you experts!

I have a project that I am trying to extract (copy / paste values) of info from a worksheet to another worksheet all within the same workbook.

My data is on worksheet "Test Export" - data labels are a4:bb4
My criteria and where I want the extracted data to reside on on the worksheet "Testing" - criteria labels on b2:d2 - data extracted labels start on a4:bb4 with copied data on a5:bb??

The code I have stops working with a Run-time error 1004: at :
    DataRng = Range(Cells(TopRow, LeftCol), Cells(LastDataRow, RightCol)).Address

Anyone see my hangup?


Sub ExtractCriteriaQuery()
    Dim MaxResults As Integer, MyCol As Integer, ResultsRng As String
    Dim MyRow As Integer, LastDataRow As Integer, DataRng As String
    Dim CritRow As Integer, CritRng As String, RightCol As Integer
    Dim TopRow As Integer, BottomRow As Integer, LeftCol As Integer
    
    LastDataRow = Worksheets("Test Export").Range("C1").Value
    
    DataRng = "A4:BB4" ' range of column headers for Data table
    CritRng = "B2:D3" ' range of cells for Criteria table
    ResultsRng = "a30:bb30" ' range of headers for Results table
    MaxResults = 100 ' any value higher than the number of possible results
    
    
    TopRow = Range(DataRng).Row
    LeftCol = Range(DataRng).Column
    RightCol = LeftCol + Range(DataRng).Columns.Count - 1
    DataRng = Range(Cells(TopRow, LeftCol), Cells(LastDataRow, RightCol)).Address
    
    
    TopRow = Range(ResultsRng).Row
    LeftCol = Range(ResultsRng).Column
    RightCol = LeftCol + Range(ResultsRng).Columns.Count - 1
    ResultsRng = Range(Cells(TopRow + 1, LeftCol), Cells(MaxResults, RightCol)).Address
    Range(ResultsRng).ClearContents ' clear any previous results but not headers
    ResultsRng = Range(Cells(TopRow, LeftCol), Cells(MaxResults, RightCol)).Address
    
    
    TopRow = Range(CritRng).Row
    BottomRow = TopRow + Range(CritRng).Rows.Count - 1
    LeftCol = Range(CritRng).Column
    RightCol = LeftCol + Range(CritRng).Columns.Count - 1
    CritRow = 0
    
    For MyRow = TopRow + 1 To BottomRow
    For MyCol = LeftCol To RightCol
    If Cells(MyRow, MyCol).Value <> "" Then CritRow = MyRow
    Next
    Next
    
    If CritRow = 0 Then
    MsgBox "No Criteria detected", "xxxxxxx"
    Else
    CritRng = Range(Cells(TopRow, LeftCol), Cells(CritRow, RightCol)).Address
    Debug.Print "DataRng, CritRng, ResultsRng: ", DataRng, CritRng, ResultsRng
    
    Worksheets("Test Export").Range(DataRng).AdvancedFilter Action:=xlFilterCopy, _
    CriteriaRange:=Range(CritRng), CopyToRange:=Range(ResultsRng), _
    Unique:=False
    End If
    Range("A5").Select
End Sub

Open in new window

0
Comment
Question by:wlwebb
1 Comment
 

Accepted Solution

by:
wlwebb earned 0 total points
ID: 35160793
Never mind.  My problem was my value on Test Export C1 was null
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

815 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now