Adding a VBA CommandButton with its respective the code

Last update on July 9, 2009 10:18 AM by jak58
Published by netty5

Adding a VBA CommandButton with its respective the code







Paste these two sub in a general module (Module1 for example).


Sub CreateButton()

Dim Obj As Object
Dim Code As String

Sheets("Sheet1").Select


'create button
    Set Obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
    Link:=False, DisplayAsIcon:=False, Left:=200, Top:=100, Width:=100, Height:=35)
    Obj.Name = "TestButton"
'buttonn text
    ActiveSheet.OLEObjects(1).Object.Caption = "Test Button"

'macro text
    Code = "Sub ButtonTest_Click()" & vbCrLf
    Code = Code & "Call Tester" & vbCrLf
    Code = Code & "End Sub"
'add macro at the end of the sheet module
    With ActiveWorkbook.VBProject.VBComponents(ActiveSheet.Name).CodeModule
        .insertlines .CountOfLines + 1, Code
    End With
End Sub

Sub Tester()
    MsgBox "You have click on the test button"
End Sub
Best answers for « Adding a VBA CommandButton with its respective the code » in :
[VBA] Detecting changes in cell Show [VBA] Detecting changes in cell The Event Change feature of a sheet will detects the change in the active cell but it gives no information about the content. The example given below will help you to find out if the cell was changed,...
[VBA: VB6] Using excel from another application Show[VBA: VB6] Using excel from another application Here is a little routine to call Excel from VB6 or another Office application. Paste in a general module (eg Module1) In VBA>> Insert>> Module and paste in the window ... In VB6>>...
[CoD 4]Call of Duty 4 - Installing a new map Show[CoD 4]Call of Duty 4 - Installing a new map Patching the game with the latest version Installing the new map Launching the game Improve your gaming experience on COD4, by adding new maps. Patching the game with the latest...
VBA A simple second Timer ShowVBA A simple second Timer In VBA, there is Timer feature available,but you can create one very easily. In a module sheet: Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'For example: Start / Stop the timer every...
Download K-Lite Codec Pack Full ShowK-Lite Codec Pack is a collection of VFW/ACM codecs, DirectShow filters and tools. Codecs and DirectShow filters are needed for encoding and decoding (playing) audio and video formats. Contents of version 4.7.0 : Player: Media Player...
Download XviD codec ShowIt is an open source which focuses on the compression of video. Xvid video codec allows the compression and the decompression of digital videos with the aim of reducing the bandwidth during the transmission through a network. Xvid is specialized in...
ASCII Code ShowData coding Morse code was the first code used for long-distance communication. Samuel F.B. Morse invented it in 1844. This code is made up of dots and dashes (a sort of binary code). It was used to carry out communication much faster than could...
IP Address ShowWhat is an IP address? Computers communicate over the Internet using the IP protocol (Internet Protocol), which uses numerical addresses, called IP addresses, made up of four whole numbers (4 bytes) between 0 and 255 and written in the format...
Worksheet - Cells ShowThe Concept of a Cell A "cell" is the intersection between a line (horizontal) and a column (vertical) on a worksheet. Thus, the name of the line combined with the name of the column gives the cell's coordinates (the term address is sometimes also...