Code suggestions or criticisms

rob.barnes01

Board Regular
Joined
Aug 26, 2010
Messages
94
Hello all,

We're constantly filtering unique values from our data files. As such, I've been asked to create a macro which will simplify the unique values filter, making it faster and simpler to use for the less tech-savvy within our company.

Below is what I've come up with, but before I present it to the men upstairs, I'd very much appreciate any suggestions for improvement.

Code:
Option Explicit

Sub CopyUniqueValues()
Dim Rng As Range
Dim ws As Worksheet

'Prompt user to select a range
On Error Resume Next
Set Rng = Application.InputBox(prompt:="Please select the range of cells you'd like to filter:", Type:=8)
If Rng Is Nothing Then Exit Sub

Application.ScreenUpdating = False

'Validate user selection
If TypeName(Rng) <> "Range" Then
    Beep
    MsgBox "You must select a range of values first.", vbExclamation + vbOKOnly, "Select a Range"
    Exit Sub
ElseIf Rng.Areas.Count > 1 Then
    Beep
    MsgBox "Multiple selections are not permitted.", vbExclamation + vbOKOnly, "Invalid Selection"
    Exit Sub
ElseIf Rng.Count < 3 Then
    Beep
    MsgBox "You must select a range containing a label and at least two unique values", vbExclamation + vbOKOnly, "Invalid Selection"
    Exit Sub
End If

'If UNIQUEVALUES sheet exists, delete it
Application.DisplayAlerts = False
On Error Resume Next
Sheets("UNIQUEVALUES").Delete
On Error GoTo 0
Application.DisplayAlerts = True

'Create UNIQUEVALUES sheet, paste selected range and filter unique values
    Set ws = ActiveSheet
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "UNIQUEVALUES"
    ws.Range(Rng.Address).Copy Sheets("UNIQUEVALUES").Range("A1")
    Cells.Select
    Selection.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range( _
        "AZ1"), Unique:=True
    Columns("A:AY").Delete Shift:=xlToLeft
    Range("A1").Select
    
Application.ScreenUpdating = True
 
End Sub

Thanks, all, and have a great weekend!

Rob

UPDATE: Added a line of code to turn alerts back on. Didn't notice that one until I closed Excel and my personal macro workbook didn't save.
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I like it.

How about trying to guess and select a range as a default, say if the user simply has the cursor in the middle of a starting range, pick that and suggest it:


Code:
Sub CopyUniqueValues()
Dim Rng As Range
Dim Rng0 As String
Dim ws As Worksheet
Dim TopLeft, BottomRight As String

[COLOR="Green"]'Prompt user to select a range[/COLOR]
On Error Resume Next
[COLOR="Green"]'First try to guess the range as a default[/COLOR]
TopLeft = ActiveCell.End(xlUp).End(xlToLeft).Address
BottomRight = ActiveCell.End(xlDown).End(xlToRight).Address
Rng0 = TopLeft & ":" & BottomRight
[COLOR="Green"]'Use the selected range values as "first guess" default[/COLOR]
Set Rng = Application.InputBox(prompt:="Please select the range of cells you'd like to filter:", Default:=Rng0, Title:="Selection Dialog", Type:=8)
If Rng Is Nothing Then Exit Sub
 
Upvote 0

Forum statistics

Threads
1,203,491
Messages
6,055,727
Members
444,814
Latest member
AutomateDifficulty

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