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

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,291
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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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