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
Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

856 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