VBA Repeatable Function on button click

0 votes

Currently, I'm developing a worksheet to track and manage drivers for my business. A button I have activates some VBA code to eliminate duplicates. Once, it works incredibly well. But, the button stops functioning if I remove some of the data and then add new data to the set. Every time I click the button, I want it to perform the same action. It's just not practical for me to open a new worksheet every time I want to add more drivers to be tracked. My skills in this area are quite minimal, so any assistance is greatly welcomed. The more extensive your explanation or teaching, the better (and patience). Thank you in advance.

Below I have added the code I have for the Remove Duplicates button and a screenshot of what my sheet looks like.

enter image description here

Private Sub CommandButton2_Click()

Dim rg As Range

With Worksheets("Driver Info Tool")
        lgLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        Set rgData = .Range("A1:D" & lgLastRow)
    End With
With rgData
        .Sort Key1:=.Columns(4), Order1:=xlDescending, Header:=xlYes
        .RemoveDuplicates Columns:=1, Header:=xlYes
    End With
    
End Sub
Apr 9, 2023 in Others by Kithuzzz
• 38,010 points
213 views

1 answer to this question.

0 votes

Remove Duplicates Based on a Single Column

  • The following is still a basic code but with a few tweaks. BTW, you haven't declared either variable. Use Option Explicit at the top of each module, to never let it happen again.
  • There are a few ways to do this. If it doesn't work, let me know I'll try something else.
Option Explicit

Private Sub CommandButton2_Click()

    Dim rg As Range

    With ThisWorkbook.Sheets("Driver Info Tool")
        If .FilterMode Then .ShowAllData
        Set rg = .Range("A1:D" & .Range("A" & .Rows.Count).End(xlUp).Row)
    End With
    
    With rg.Resize(rg.Rows.Count - 1).Offset(1)
        .Sort Key1:=.Columns(4), Order1:=xlDescending, Header:=xlNo
        .RemoveDuplicates Columns:=1, Header:=xlNo
    End With
    
End Sub
answered Apr 9, 2023 by narikkadan
• 63,420 points

Related Questions In Others

0 votes
1 answer

Disable Postback on button ASP.NET c#

If you're dynamically adding controls to the ...READ MORE

answered May 30, 2022 in Others by rajiv
• 1,620 points
6,311 views
0 votes
1 answer

Excel VBA to change background image of shape by clicking on shape

You need to keep track of what ...READ MORE

answered Sep 23, 2022 in Others by narikkadan
• 63,420 points
2,093 views
0 votes
1 answer

I have a problem with counta function in VBA

When the worksheet name has a space, ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,420 points
678 views
0 votes
1 answer

How can I use a command button in excel to set the value of multiple cells in one click?

Try this: Private Scan As Integer Private Sub CommandButton1_Click() ...READ MORE

answered Oct 24, 2022 in Others by narikkadan
• 63,420 points
530 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
905 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,225 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
515 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,670 points
757 views
0 votes
1 answer

Trying to redo my Export Button Function on my document

You could directly publish the data to ...READ MORE

answered Mar 30, 2023 in Others by narikkadan
• 63,420 points
305 views
0 votes
1 answer

Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

You misunderstand the purpose of the function ...READ MORE

answered Sep 23, 2022 in Others by narikkadan
• 63,420 points
3,122 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP