Solved

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

Posted on 2011-03-17
1
565 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

706 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

19 Experts available now in Live!

Get 1:1 Help Now