?
Solved

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

Posted on 2011-03-17
1
Medium Priority
?
571 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

801 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