Thread: Help me4
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Ben McClave Ben McClave is offline
external usenet poster
 
Posts: 173
Default Help me4

Timothy,

I think from your posts that you are looking for a way to filter your data down to only the records meeting certain criteria. Advanced filter is a great way to do this. The macro below will take your data range and filter it based on three columns. Then it will paste that data to a new sheet.

Hope this helps,
Ben


Sub FilterResults()
Dim DataRange As Range
Dim Destination As Worksheet

Set DataRange = Sheet1.Range("A1:D20") 'Where your data is
Set Destination = ThisWorkbook.Sheets.Add 'New sheet to store results

DataRange.Range("A1").EntireRow.Copy Destination.Range("A1") 'Copy headers
ThisWorkbook.Names.Add Name:="Data", _
RefersTo:="='" & Sheet1.Name & "'!" & DataRange.Address 'Add named range
Destination.Range("A1:D1").EntireColumn.Insert (xlToRight) 'Insert columns for criteria
With Destination.Range("A1")
.Value = "pubid" 'Criteria 1 column header
.Offset(1, 0).Value = "'=0877" 'Criteria 1 value
.Offset(0, 1).Value = "royalty" 'Criteria 2 column header
.Offset(1, 1).Value = "'=10" 'Criteria 2 value
.Offset(0, 2).Value = "type" 'Criteria 3 column header
.Offset(1, 2).Value = "'=trad_cook" 'Criteria 3 value
End With
Range("Data").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Destination.Range("A1:C2"), _
CopyToRange:=Destination.Range("E1").CurrentRegion , _
Unique:=False 'Filter results using criteria
Destination.Range("A:D").Delete 'Delete criteria range
Destination.Activate

End Sub