Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying Unique Values from Non-Static Column 1

Status
Not open for further replies.

Rzrbkpk

Technical User
Mar 24, 2004
84
0
0
US
I am attempting to copy all unique values from a specific column on a worksheet titled "Raw_Data" to a a worksheet titled "temp". This was working for a bit, but due to raw data input columns varying for multiple reports, I can't place a static column such as "I" in my code for the advanced filter. I've successfully managed to identify the needed column by header name(which will not change), but can't pass that variable into my advanced filter. I get a type mismatch. Any help would be appreciated.

Code:
    Sub Sample()
    
    Dim ws As Worksheet
    Dim lRow As Long, aCol As Long
    Dim aCell As Range
    Dim fRange As Range
    
    Set ws = Sheets("Raw_Data")        
        With ws
        Set aCell = .Rows(1).Find("Current Lead Case Manager")
        aCol = aCell.Column
        .Range(Cells(1, "aCol")).AdvancedFilter xlFilterCopy, , Sheets("Temp").Range("A1"), True
        End With
    
    End Sub
 
.Range(.Cells(1, aCol)).AdvancedFilter xlFilterCopy, , Sheets("Temp").Range("A1"), True

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV, but now I'm getting Run-time error '1004': Application-defined or Object-defined error.
 
Do you ACTUALLY have a sheet named "Temp"?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi Skip!
Yes, the sheet I'm trying to paste to is labeled "Temp".
 
post your current code


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
This is with the correction PHV advised.

Code:
Sub Sample()
    
    Dim ws As Worksheet
    Dim lRow As Long, aCol As Long
    Dim aCell As Range
    Dim fRange As Range
    
    Set ws = Sheets("Raw_Data")
        
        With ws
        Set aCell = .Rows(1).Find("Current Lead Case Manager")
        aCol = aCell.Column
        .Range(.Cells(1, aCol)).AdvancedFilter xlFilterCopy, , Sheets("Temp").Range("A1"), True
        End With
    
End Sub
 
on what statement did the error occur?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
It's the following line that the error occurs:

Code:
        .Range(.Cells(1, aCol)).AdvancedFilter xlFilterCopy, , Sheets("Temp").Range("A1"), True

Specifically the cell reference is the issue. When I hover of the cell reference before executing that line, I get the error.
 
what is the value of aCol when this error occurs?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
The value aCol = 10 which corresponds to the column that contains the header "Current Lead Case Manager".
 
And this ?
Code:
.Cells(1, aCol).AdvancedFilter xlFilterCopy, , Sheets("Temp").Range("A1"), True

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
try doing it MANUALLY.

Cant copy to another sheet!!!

I'd use MS Query. faq68-5829

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
...or set up a Pivot Table on Temp for this data.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
PHV - I get the same run-time error when I try that method.

Skip - Well that's no fun at all :) ...that and it disrupts the larger part of the rest of my code. I was able to to copy to another sheet as long as the column didn't change with the following code:

Code:
Sub Sample()
    
    Dim ws As Worksheet
    Dim lRow As Long, aCol As Long
    Dim aCell As Range
    Dim fRange As Range
    
    Set ws = Sheets("Raw_Data")
        
    With Sheets("Raw_Data")
        .Range("I1", .Range("I" & Rows.Count).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, _
        CopyToRange:=Sheets("Temp").Range("A1"), Unique:=True
    End With

End Sub

 
And this ?
Code:
With Sheets("Raw_Data")
  .Range(.Cells(1, aCol), .Cells(.Rows.Count, aCol).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, _
    CopyToRange:=Sheets("Temp").Range("A1"), Unique:=True
End With

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV - YES!!! Two days in a row you have made my headache go away. Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top