Solved

Macro to delete blank rows and run from drop down list

Posted on 2016-08-17
14
42 Views
Last Modified: 2016-08-18
Hi Experts Using Excel 2013

See attached file

I want the macro to run from the drop down in sheet “Master” cell B2. i have the following code in the Worksheet module

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$2" Then
        Call CopyandPasteValues
    End If
End Sub


The following copy and paste macro works fine I believe See below…
Sub CopyandPasteValues()

    Sheets("Data").Columns("AW").Copy
    Sheets("Unique List").Range("$A$1:$A:$A$3500").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                                          :=False, Transpose:=False
    Application.CutCopyMode = False
    Sheets("Unique List").Range("$A$1:$A$3500").RemoveDuplicates Columns:=Array(1, 2), _
                                                                 Header:=xlYes

End Sub

Open in new window


But need to add function to remove blank rows from the data set, not sure how to integrate this part..

well this all work...
Test-1234.xlsx
0
Comment
Question by:route217
  • 7
  • 4
  • 3
14 Comments
 
LVL 30

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41760611
If your intention is to copy the Employee's names from col. A of Data into col. A of Unique List sheet, try this.....


Sub CopyandPasteValues()
Application.ScreenUpdating = False
Sheets("Unique List").Columns(1).ClearContents
Sheets("Data").Range("A:A").SpecialCells(xlCellTypeConstants).Copy
Sheets("Unique List").Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Sheets("Unique List").Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:route217
ID: 41760619
And remove blank rows too
0
 

Author Comment

by:route217
ID: 41760623
Excellent the macro work but can i use the following to run the macro from a drop down list...

Private Sub Worksheet_Change(ByVal Target As Range) 
    If Target.Address = "$B$2" Then
        Call CopyandPasteValues
    End If
End Sub

Open in new window


Add the following to ThisWorksheet
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41760624
That's because your drop down list is in B1 not in B2, so change the following line...

If Target.Address = "$B$2"

Open in new window


TO THIS
If Target.Address = "$B$1"

Open in new window

0
 
LVL 30

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41760627
Right click the Master Tab --> View Code and place the following code into the opened code window.

Private Sub Worksheet_Change(ByVal Target As Range) 
    If Target.Address = "$B$1" Then
        Call CopyandPasteValues
    End If
End Sub

Open in new window

0
 

Author Comment

by:route217
ID: 41760628
Many thank excellence feedback.
0
 
LVL 30

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41760630
You're welcome. Glad to help.
0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41761275
SpecialCells will cause an error if no cells match the criteria so it's best to use an error handler. If an error occurs and the macro fails then Screen Updating will not be restored automatically. I would amend the code slightly
Option Explicit

Sub CopyandPasteValues()
    On Error GoTo exit_proc
    With Application
        .ScreenUpdating = False
        With Sheets("Unique List")
            .Columns(1).ClearContents
            Sheets("Data").Range("A:A").SpecialCells(xlCellTypeConstants).Copy
            .Range("A1").PasteSpecial Paste:=xlPasteValues
            .Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
        End With
exit_proc:
MsgBox "An error occurred", vbCritical, "Quitting"
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
End Sub

Open in new window

0
 
LVL 30

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41761305
@Roy

There are so many ifs and buts...
like what if Sheet Unique List or Sheet Data doesn't exists in the workbook?
With the kind of error handling you included in your code will suppress the error only, if any, during the code execution and will prompt the user in the end that "An error occurred" but with no clue of what error has been occurred. In other words, this will only stop the debug window to appear.
Won't it be better if the error message also includes the Err.Description, I think then only your error handling makes any sense.
0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41761323
Makes more sense than no error handling. It was only a quick suggestion
0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41761325
Amended code

Option Explicit

Sub CopyandPasteValues()

    On Error GoTo exit_proc

    On Error GoTo exit_proc
    With Application
        .ScreenUpdating = False
        With Sheets("Unique List")
            .Columns(1).ClearContents
            Sheets("Data").Range("A:A").SpecialCells(xlCellTypeConstants).Copy
            .Range("A1").PasteSpecial Paste:=xlPasteValues
            .Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
        End With
exit_proc:
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure CopyandPasteValues of Module Module1"
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
    On Error GoTo 0

End Sub

Open in new window

0
 
LVL 30

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41761327
I am happy that you got my point. :)
0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41761356
I didn't get your point, I was only trying to help  the OP by suggesting a slight amendment to your code. Didn't expect criticism from you when I didn't start by criticizing your original post. My point was simply really to avoid an error using SpecialCells. I then noticed ScreenUpdating, which can be very confusing to a beginner if it is not restored by the code, I don't really see what advantage there is in using it with your code, but it wouldn't be a problem unless an un-trapped error occurred.

I've just spotted a typo in my code, it should be
Option Explicit


Sub CopyandPasteValues()

    On Error GoTo exit_proc
   
    With Application
        .ScreenUpdating = False
        With Sheets("Unique List")
            .Columns(1).ClearContents
            Sheets("Data").Range("A:A").SpecialCells(xlCellTypeConstants).Copy
            .Range("A1").PasteSpecial Paste:=xlPasteValues
            .Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
        End With
exit_proc:
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure CopyandPasteValues of Module Module1"
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
    On Error GoTo 0

End Sub

Open in new window


Although I think this is all that is bneeded

Option Explicit


Sub CopyandPasteValues()

    On Error GoTo exit_proc

        With Sheets("Unique List")
            .Columns(1).ClearContents
            Sheets("Data").Range("A:A").SpecialCells(xlCellTypeConstants).Copy
            .Range("A1").PasteSpecial Paste:=xlPasteValues
            .Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
        End With
exit_proc:
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") . Probable cause is that no cells could be found for the macro to work with"
        Application.CutCopyMode = False
    On Error GoTo 0

End Sub
0
 
LVL 30

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41761389
haha
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question