Delete button code inside its own click event

Shameem Khan

New Member
Joined
Aug 9, 2020
Messages
13
Office Version
  1. 2013
Platform
  1. Windows
Hello. I have a Userform with dynamically created Commandbutton. I am using a class called “Class1” to add click events to the commandbutton.

What I am looking for is a way to delete the Button itself when I click on it. I tried adding the code to remove the button inside its own click event. Obviously its not working. Is there any way to make this happen? Please help.

Thanks in advance

Note: FR_LIST is a Frame, Userform name is UF_HOME

Code in Userform:

VBA Code:
Dim Btncls As New Class1

Private Sub BT_ADD_Click() ‘ADD is a button inside Userform UF_HOME

  ‘ Some other codes here

  Dim Btn as Object
  Set Btn = Me.FR_LIST.Controls.Add("Forms.CommandButton.1", "BT_REM")

  With Btn
    .Left = 402
    .Top = Slno * 15
    .Width = 13
    .Height = 13
    .Picture = LoadPicture("C:\ Remove.jpg")
    .PicturePosition = 12
  End With

FR_LIST.Visible = True
Set Btncls.CmdEvents = Btn
Slno = Slno + 1

End Sub

Code in Class1:

VBA Code:
Public WithEvents CmdEvents As MSForms.CommandButton

Private Sub CmdEvents_Click()

  ‘ Some other codes here

  UF_HOME.FR_LIST.Controls.Remove CmdEvents.Name

End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Yes. Error 91 - Object Variable or With Block variable not set
Worked for me when testing.

It looks like you have more code than what you are sowing us.
Try adding Option Explicit at the top of your modules, run the code again and see at which line(s) the code errors out
 
Upvote 0
Solution
Hi Jaafar. You helped me find the error. There was an error in the userform name that I have given. It was a stupid mistake. Sorry for that. Thank you for your reply.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,597
Members
449,038
Latest member
Arbind kumar

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