disable and enable button inside sheet like commandbutton on userform

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,494
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi

is there any way to disable buttons and enable buttons inside the sheet like disable and enable commandbutton on userform?

I want to enable button name is ("search") and disable buttons names are ("copy","delete") when press button name is ("search") then will button name is ("search") becomes disable and enable buttons names are ("copy","delete") and when press button name is ("copy ") becomes disable and enable button name ("delete") and when press button name is("delete") then will button name is ("delete") becomes disable and enable button name is ("search")
I hope this is logical .
thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Please try this. I created an ActiveX button and named it Search_btn. The first code is stored on the SHEET level module, the second code is stored in a standard module

When a user clicks the search button the first code gets called, disables the search button so it can't run until the search has been completed


VBA Code:
Private Sub Search_btn_Click()
  
  ToggleOleEnable Me.Name, ActiveSheet, False 'disable while search is continuing
  
  'Go do other things and come back
  
  ToggleOleEnable Me.Name, ActiveSheet, True  'Search is finished, enable button

End Sub


VBA Code:
'BtnNAme Name of button or other control
'Sht is the the sheet the control is on as object
'EnBool is either True for Enabled or False for Disabled
Sub ToggleOleEnable(BtnNAme As String, Sht As Worksheet, EnBool As Boolean)
  Dim oOLE As OLEObject
  For Each oOLE In Sht.OLEObjects
    If oOLE.Name = BtnNAme Then
      oOLE.Enabled = EnBool
      Exit For
    End If
  Next oOLE
End Sub
 
Upvote 0
thanks
I created an ActiveX button
no I'm not talking about ActiveX button . I want doing with normal button which use linking with the macro .
 
Upvote 0
Hi abdelfattah,

the buttons are named btnSearch, btnCopy and btnDelete in this sample, the codes being called are of the same name.

This code needs to be run first in order to set up the buttons (you may call it from Workbook_Open if opening the workbook):

VBA Code:
Sub OnlyBtnSearch()
  'needs to be run in order to set up the buttons accrosingly
  With Worksheets("Sheet1")
    With .Buttons("btnCopy")
      .Font.Color = 8421504
      .OnAction = ""
    End With
    With .Buttons("btnDelete")
      .Font.Color = 8421504
      .OnAction = ""
    End With
    With .Buttons("btnSearch")
      .Font.Color = 0
      .OnAction = "btnSearch"
    End With
  End With
End Sub

I altered the request to have only one of the 3 buttons available:

VBA Code:
Sub btnSearch()
  'code to perform for Search here

  With Worksheets("Sheet1")
    With .Buttons("btnCopy")
      .Font.Color = 0
      .OnAction = "btnCopy"
    End With
    With .Buttons("btnDelete")
      .Font.Color = 8421504
      .OnAction = ""
    End With
    With .Buttons("btnSearch")
      .Font.Color = 8421504
      .OnAction = ""
    End With
  End With
End Sub

Sub btnCopy()
  'code to perform for Copy here

  With Worksheets("Sheet1")
    With .Buttons("btnCopy")
      .Font.Color = 8421504
      .OnAction = ""
    End With
    With .Buttons("btnDelete")
      .Font.Color = 0
      .OnAction = "btnDelete"
    End With
    With .Buttons("btnSearch")
      .Font.Color = 8421504
      .OnAction = ""
    End With
  End With
End Sub

Sub btnDelete()
  'code to perform for Delete here

  With Worksheets("Sheet1")
    With .Buttons("btnCopy")
      .Font.Color = 8421504
      .OnAction = ""
    End With
    With .Buttons("btnDelete")
      .Font.Color = 8421504
      .OnAction = ""
    End With
    With .Buttons("btnSearch")
      .Font.Color = 0
      .OnAction = "btnSearch"
    End With
  End With
End Sub

HTH,
Holger
 
Upvote 0
@HaHoBe
thanks
shows error Unable to set property OnAction of the Button class in this line
VBA Code:
      .OnAction = "btnSearch"
for module Sub OnlyBtnSearch()
 
Upvote 0
Hi abdelfattah,

a view to my IDE:

2023-01-17 20 09 24.png



Sample workbook is available here

HTH
Holger
 
Upvote 0
thanks for the file
it's works excellently , but I have to studing my bad to understand how your code works .
I will come back to inform you how code goes.;)
 
Upvote 0
Hi abdelfattah,

it's almost the same for all: for the next button in the wanted "chain" change the font colour to black and attach the wanted procedure while for the other 2 the font colour is dimmed and the attached macro is stripped so that every click only one of the three buttons can start a macro.

Holger
 
Upvote 0
now it wrorks as I expect .
two questions please?
first : macro for Sub OnlyBtnSearch() , just I need run one time and I don't need anymore?
second if I want enabling all of the buttons , what's the procedure?
 
Upvote 0
Hi abdelfattah,

first : macro for Sub OnlyBtnSearch() , just I need run one time and I don't need anymore?

Just change the code for the btnDelete:

VBA Code:
Private Sub btnDelete()
  'code to perform for Delete here

  With Worksheets("Sheet1")
    With .Buttons("btnCopy")
      .Font.Color = 8421504
      .OnAction = ""
    End With
    With .Buttons("btnDelete")
      .Font.Color = 8421504
      .OnAction = ""
    End With
'    '/// activate btnSearch
'    With .Buttons("btnSearch")
'      .Font.Color = 0
'      .OnAction = "btnSearch"
'    End With
    '/// do not activate btnSearch
    With .Buttons("btnSearch")
      .Font.Color = 8421504
      .OnAction = ""
    End With
  End With
End Sub

Going this way would mean that the other two macros can't be started as well. By the way: I changed the procedure from Public to Provate so they may not be started via the Macro-Window. If you want anything else feel free to post back and describe what you are looking for.

second if I want enabling all of the buttons , what's the procedure?

I posted procedure OnlyBtnSearch. In a renamed copy of that procedure all you would need to do is copy in the parts for each button where a macro is assigned under OnAction...

VBA Code:
Sub ActivateAllButtons()
  'will activate all buttons and attach macros to them
  With Worksheets("Sheet1")
    With .Buttons("btnCopy")
      .Font.Color = 0
      .OnAction = "btnCopy"
    End With
    With .Buttons("btnDelete")
      .Font.Color = 0
      .OnAction = "btnDelete"
    End With
    With .Buttons("btnSearch")
      .Font.Color = 0
      .OnAction = "btnSearch"
    End With
  End With
End Sub

Holger
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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