Change .onaction of a button created as part of a macro

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,285
Office Version
  1. 365
Platform
  1. Windows
As part of a routine I create a button to perform an advance filter.

Code:
With Sheets("Data").Buttons.Add(300, 0, 100, 25)
    .OnAction = "PO_Filterinplace"
    .Characters.Text = "View Supplier"
End With

Rather than having a separate clear filter button I would like to change this button when the filter is active

Code:
    .OnAction = "Clear_Filter"
    .Characters.Text = "Clear Filter"

However I am not sure how to interact with this specific button after the initial ".add".

I'm guessing there must be a way of naming the button, but am not sure of the syntax.

Appreciate any support you can offer.

Thanks
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Just worked it out...

Code:
With Sheets("Data").Buttons.Add(300, 0, 100, 25)
    .Name = "PO_Filterinplace"
    .OnAction = "PO_Filterinplace"
    .Characters.Text = "View Supplier"
End With

Code:
With Sheets("Data").Buttons("PO_Filterinplace")
    .OnAction = "Clear_Filter"
    .Characters.Text = "Clear Filter"
End With

Code:
Sub Clear_Filter()
Sheets("Data").ShowAllData
With Sheets("Data").Buttons("PO_Filterinplace")
    .OnAction = "PO_Filterinplace"
    .Characters.Text = "View Supplier"
End With
End Sub


I was attempting With Sheets("Data").Buttons.name("PO_Filterinplace") which didn't work
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,962
Latest member
Fenes

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