Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 83
Default use a range variable in advanced filter

Hi,
I have a variable range that I would like to filter for unique values. I am
having trouble defining the range and get a runtime error 1004, reference is
not valid.
I copy the field from a database to another sheet and would like to apply
the filter to just that range to get the uniquie values.
Code:
Sub FilterUniqueValues()
Dim CostCentre As Range
Sheets("Data").Select 'Data sheet containing database
Range("C1").Select 'top cell of field to copy
Range(Selection, Selection.End(xlDown)).Select 'select field
Selection.Copy
Sheets("Criteria").Select 'move to second sheet
Range("M2").Select 'paste cell for copied field
ActiveSheet.Paste
Application.CutCopyMode = False
Set CostCentre = Selection 'set range to coped field
'filter for unique values
CostCentre.AdvancedFilter xlFilterCopy, "I2:I3", "o2", True
End Sub

thanks for any help
Jake
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default use a range variable in advanced filter

I'm not quite sure what you're doing, but that .advancedfilter needs ranges
passed to it:

With Sheets("criteria")
CostCentre.AdvancedFilter Action:=xlFilterCopy, _
criteriarange:=.Range("I2:i3"), _
copytorange:=.Range("o2"), unique:=True
end with

Jake wrote:

Hi,
I have a variable range that I would like to filter for unique values. I am
having trouble defining the range and get a runtime error 1004, reference is
not valid.
I copy the field from a database to another sheet and would like to apply
the filter to just that range to get the uniquie values.
Code:
Sub FilterUniqueValues()
Dim CostCentre As Range
Sheets("Data").Select 'Data sheet containing database
Range("C1").Select 'top cell of field to copy
Range(Selection, Selection.End(xlDown)).Select 'select field
Selection.Copy
Sheets("Criteria").Select 'move to second sheet
Range("M2").Select 'paste cell for copied field
ActiveSheet.Paste
Application.CutCopyMode = False
Set CostCentre = Selection 'set range to coped field
'filter for unique values
CostCentre.AdvancedFilter xlFilterCopy, "I2:I3", "o2", True
End Sub

thanks for any help
Jake


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 91
Default use a range variable in advanced filter

On Jan 4, 1:58*pm, Jake wrote:
Hi,
I have a variable range that I would like to filter for unique values. *I am
having trouble defining the range and get a runtime error 1004, reference is
not valid.
I copy the field from a database to another sheet and would like to apply
the filter to just that range to get the uniquie values.
Code:
Sub FilterUniqueValues()
* * Dim CostCentre As Range
* * Sheets("Data").Select * 'Data sheet *containing database
* * Range("C1").Select * * *'top cell of field to copy
* * Range(Selection, Selection.End(xlDown)).Select 'select field
* * Selection.Copy
* * Sheets("Criteria").Select 'move to second sheet
* * Range("M2").Select 'paste cell for copied field
* * ActiveSheet.Paste
* * Application.CutCopyMode = False
* * Set CostCentre = Selection *'set range to coped field
* * 'filter for unique values
* * CostCentre.AdvancedFilter xlFilterCopy, "I2:I3", "o2", True
End Sub

thanks for any help
Jake


Why not just filter the values in the database before copying them
over? Instead of something like "SELECT CostCenter FROM tEmployees"
use the Distinct keyword to get only unique values, i.e.: "SELECT
DISTINCT(CostCenter) AS CostCenters FROM tEmployees"
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 83
Default use a range variable in advanced filter

Thanks Dave, that's works!
I want to get only the unique values from a field in a database. Perhaps
there is a better way, but this works.
Jake

"Dave Peterson" wrote:

I'm not quite sure what you're doing, but that .advancedfilter needs ranges
passed to it:

With Sheets("criteria")
CostCentre.AdvancedFilter Action:=xlFilterCopy, _
criteriarange:=.Range("I2:i3"), _
copytorange:=.Range("o2"), unique:=True
end with

Jake wrote:

Hi,
I have a variable range that I would like to filter for unique values. I am
having trouble defining the range and get a runtime error 1004, reference is
not valid.
I copy the field from a database to another sheet and would like to apply
the filter to just that range to get the uniquie values.
Code:
Sub FilterUniqueValues()
Dim CostCentre As Range
Sheets("Data").Select 'Data sheet containing database
Range("C1").Select 'top cell of field to copy
Range(Selection, Selection.End(xlDown)).Select 'select field
Selection.Copy
Sheets("Criteria").Select 'move to second sheet
Range("M2").Select 'paste cell for copied field
ActiveSheet.Paste
Application.CutCopyMode = False
Set CostCentre = Selection 'set range to coped field
'filter for unique values
CostCentre.AdvancedFilter xlFilterCopy, "I2:I3", "o2", True
End Sub

thanks for any help
Jake


--

Dave Peterson

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default use a range variable in advanced filter

I wasn't sure what you were doing when you pasted the values in M2.

This creates a unique list:

Option Explicit
Sub FilterUniqueValues2()
Dim CostCentre As Range
Dim LastRow As Long

With Worksheets("Data")
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
Set CostCentre = .Range("C1:C" & LastRow)
End With

With Sheets("criteria")
CostCentre.AdvancedFilter Action:=xlFilterCopy, _
copytorange:=.Range("o1"), unique:=True
End With

End Sub

The list is pasted into O1 (header) of the Criteria worksheet.

Jake wrote:

Thanks Dave, that's works!
I want to get only the unique values from a field in a database. Perhaps
there is a better way, but this works.
Jake

"Dave Peterson" wrote:

I'm not quite sure what you're doing, but that .advancedfilter needs ranges
passed to it:

With Sheets("criteria")
CostCentre.AdvancedFilter Action:=xlFilterCopy, _
criteriarange:=.Range("I2:i3"), _
copytorange:=.Range("o2"), unique:=True
end with

Jake wrote:

Hi,
I have a variable range that I would like to filter for unique values. I am
having trouble defining the range and get a runtime error 1004, reference is
not valid.
I copy the field from a database to another sheet and would like to apply
the filter to just that range to get the uniquie values.
Code:
Sub FilterUniqueValues()
Dim CostCentre As Range
Sheets("Data").Select 'Data sheet containing database
Range("C1").Select 'top cell of field to copy
Range(Selection, Selection.End(xlDown)).Select 'select field
Selection.Copy
Sheets("Criteria").Select 'move to second sheet
Range("M2").Select 'paste cell for copied field
ActiveSheet.Paste
Application.CutCopyMode = False
Set CostCentre = Selection 'set range to coped field
'filter for unique values
CostCentre.AdvancedFilter xlFilterCopy, "I2:I3", "o2", True
End Sub

thanks for any help
Jake


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Building criteria string for Advanced Filter variable not resolvin JEFFWI Excel Discussion (Misc queries) 1 August 29th 07 07:52 PM
Advanced Filter VB Script for Variable Criteria Range Jason Excel Programming 2 June 19th 06 07:15 AM
advanced filter a range Il Principe Excel Worksheet Functions 2 August 1st 05 03:27 PM
"Criteria Range" in the "Data/Filter/Advanced Filter" to select Du TC Excel Worksheet Functions 1 May 12th 05 02:06 AM
Advanced Filter & Named Range LS[_2_] Excel Programming 3 April 7th 04 02:28 AM


All times are GMT +1. The time now is 09:06 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"