Dynamically add code for a object using Macro

budhaditya

New Member
Joined
Jul 30, 2010
Messages
1
Dear All,
I am a beginner to VBA.
The situation is -:
When i click on a button in a existing sheet (A), a new sheet opens.
Apart from all the formatting etc, I am addding a Check box in this new sheet.
I want to assign a macro to this checkbox and want to store it in that workbook itself.
While i am able to access the checkbox using the following statement below -:
newWorkSheet.Shapes("Check Box 1").Select


In the Selection.OnAction I want to direct it to a peice of code which i have written but i am unable to transfer it to the new workbook.

Because this excel program has to be used by many people, i am not inclined to use the following code, becuase it requries them to enable the macro security setting of "Trust accesss to VBA project object model"

**** NOT WANTING TO USE THE FOLLOWING CODE*******
'Dim FName As String
'With Workbooks("AuctionRequestForm- Draft 3")
'FName = .Path & "\code.txt"
'.VBProject.VBComponents("Module1").Export FName
'End With
'ActiveWorkbook.VBProject.VBComponents.Import FName

**** NOT WANTING TO USE THE ABOVE CODE*******

Is there a way, I can dynamically type the peice of code in the new workbook?

I tried using ActiveWorkbook.VBProject.VBComponents.Add vbext_ct_StdModule, however, i am unable to do it.


Kindly help.
 

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)

Forum statistics

Threads
1,215,886
Messages
6,127,586
Members
449,385
Latest member
KMGLarson

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