Disable button until duplicates found

mdwasim

New Member
Joined
Dec 31, 2019
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
Guys,
Got something working somehow by re purposing the bits of code.
I have got a defined excel table "tbl_data" with columns "Name" and "Desc"
Based on Names entered, I am creating few excel sheets.

I have got bit of code which checks for duplicates in column "Name" and highlights those cells.
From the code what I understand is, each cell is being checked (of course it should).
I am looking to find a solution for, disabling the button if there is duplicates.
I tried using btn.enabled= false, but as use moves to next row for entering the data, the button is enabled back.
coz I am putting the btn.enabled = true in "else" block.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim myRange As Range
    Dim i As Integer
    Dim j As Integer
    Dim myCell As Range

    Set myRange = Range("tbl_data[Name]")

    For Each myCell In myRange
        If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
            myCell.Interior.ColorIndex = 3
            btn_create.Enabled = False
            Else
            myCell.Interior.ColorIndex = 0
            btn_create.Enabled = True
        End If
    Next
End Sub

Need your expertise here.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try
VBA Code:
btn_create.Enabled = True
 For Each myCell In myRange
        If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
            myCell.Interior.ColorIndex = 3
            btn_create.Enabled = False
            Else
            myCell.Interior.ColorIndex = 0
        End If
    Next
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,695
Members
449,117
Latest member
Aaagu

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