VBA Extract unique values in multiple sheets

rplohocky

Active Member
Joined
Sep 25, 2005
Messages
292
Office Version
  1. 365
Platform
  1. Windows
Hello,
I found this awesome code that extracts unique values found in column A in every sheet, creates a new sheet, pastes them into column A and then alphabetizes them.

Code:
Sub UniqueValues()
Dim newWS As Worksheet, r As Long, N As Long, i As Integer
Application.ScreenUpdating = False
For Each ws In Sheets

Application.DisplayAlerts = False
If ws.Name = "UNIQUE_DATA" Then ws.Delete
Application.DisplayAlerts = True

Next
Set newWS = Sheets.Add(after:=Sheets(Sheets.Count))
newWS.Name = "UNIQUE_DATA"
N = 1
For i = 1 To Sheets.Count - 1
r = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row
Sheets(i).Range("A3:A" & r).Copy
Cells(N, 1).PasteSpecial xlValues
N = Cells(Rows.Count, "A").End(xlUp).Row + 1

Next
r = Cells(Rows.Count, "A").End(xlUp).Row
Range("A3:A" & r).AdvancedFilter _
Action:=xlFilterInPlace, Unique:=True
Range("A3:A" & r).Copy
Range("B1").PasteSpecial xlValues
Application.CutCopyMode = False

Range("A3:A" & r).AdvancedFilter _
Action:=xlFilterInPlace, Unique:=False
Columns(1).Delete
r = Cells(Rows.Count, "A").End(xlUp).Row

'Below sorts the data alphabeticaly.
Range("A1:A" & r).Sort key1:=Range("A1"), Header:=xlNo
Application.ScreenUpdating = True

End Sub
I was wondering what I need to change so it will look in A3 through the rest of column A. My header row is in row 2 and my data starts in row 3. The issue now is the macro grabs row 2 as well. I changed the range where i thought it should be changes to A3:A but nothing I do seems to have any affect.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try it like this (untested):

Code:
Sub UniqueValues()

Dim ws As Worksheet, newWS As Worksheet, r As Long, N As Long, i As Integer
Application.ScreenUpdating = False

For Each ws In Sheets
    Application.DisplayAlerts = False
    If ws.Name = "UNIQUE_DATA" Then ws.Delete
    Application.DisplayAlerts = True
Next

Set newWS = Sheets.Add(after:=Sheets(Sheets.Count))
newWS.Name = "UNIQUE_DATA"

N = 1
For i = 1 To Sheets.Count - 1
    r = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row
    Sheets(i).Range("A3:A" & r).Copy
    Cells(N, 1).PasteSpecial xlValues
    N = Cells(Rows.Count, "A").End(xlUp).Row + 1
Next

r = Cells(Rows.Count, "A").End(xlUp).Row
Range("A1:A" & r).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Range("A1:A" & r).Copy
Range("B1").PasteSpecial xlValues
Application.CutCopyMode = False

Range("A1:A" & r).AdvancedFilter Action:=xlFilterInPlace, Unique:=False
Columns(1).Delete

r = Cells(Rows.Count, "A").End(xlUp).Row

'Below sorts the data alphabeticaly.
Range("A1:A" & r).Sort key1:=Range("A1"), Header:=xlNo
Application.ScreenUpdating = True

End Sub

WBD
 
Upvote 0
Try it like this (untested):

Code:
Sub UniqueValues()

Dim ws As Worksheet, newWS As Worksheet, r As Long, N As Long, i As Integer
Application.ScreenUpdating = False

For Each ws In Sheets
    Application.DisplayAlerts = False
    If ws.Name = "UNIQUE_DATA" Then ws.Delete
    Application.DisplayAlerts = True
Next

Set newWS = Sheets.Add(after:=Sheets(Sheets.Count))
newWS.Name = "UNIQUE_DATA"

N = 1
For i = 1 To Sheets.Count - 1
    r = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row
    Sheets(i).Range("A3:A" & r).Copy
    Cells(N, 1).PasteSpecial xlValues
    N = Cells(Rows.Count, "A").End(xlUp).Row + 1
Next

r = Cells(Rows.Count, "A").End(xlUp).Row
Range("A1:A" & r).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Range("A1:A" & r).Copy
Range("B1").PasteSpecial xlValues
Application.CutCopyMode = False

Range("A1:A" & r).AdvancedFilter Action:=xlFilterInPlace, Unique:=False
Columns(1).Delete

r = Cells(Rows.Count, "A").End(xlUp).Row

'Below sorts the data alphabeticaly.
Range("A1:A" & r).Sort key1:=Range("A1"), Header:=xlNo
Application.ScreenUpdating = True

End Sub

WBD

I found the code this way which is why I changed the A1:A to A3:A thinking it would only look at A3 and down but it still grabs the entire column including the header row. Any other ideas?
 
Upvote 0
It grabs A3 down on the data sheets but then has to remove duplicates / sort the entire column on the new sheet.

WBD
 
Upvote 0

Forum statistics

Threads
1,223,887
Messages
6,175,199
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top