Consulting

Results 1 to 7 of 7

Thread: Creating two buttons and code programmatically

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location

    Question Creating two buttons and code programmatically

    I need to create two buttons (at the same time) and add code to them. Not the same code for both. Each one must call a sub. I know how to add a code to the first button or add code to subsequente buttons, but only if i run the code twice and i need them to be added both the first time i run the code. Tried for ... next already but didnt succeed.

    Any suggestions?
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Carlos,

    Nate Oliver helped me with this one a while ago. You should be able to modify the button names and procedure names to suit. Remember to set a reference to the VBA Extensibility Library and also to ensure that you've trusted access to the VB Project. (Tools|Options|Security|Macro Security|Trusted Publishers... or something like that!)

    Sub Add_Command_Buttons()
    'Macro purpose: To create 2 pre-formatted command buttons on the worksheet
    'SECTION 1
     'Declare required variables and working environment
     Dim cl As Range, ctrl As OLEObject, i, LineNum As Integer
     Dim TargetSheet As Worksheet, TargetBook As Workbook
     Application.ScreenUpdating = False
     Set TargetSheet = ActiveSheet
     Set TargetBook = ActiveWorkbook
    'SECTION 2
     'Add preformated buttons to worksheet
     For Each cl In [A1,I1] 'update this range to place buttons
         i = i + 1
         Set ctrl = TargetSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
             Left:=cl.Left + 1, Top:=cl.Top + 1, Width:=Choose(i, 48, 72.75), Height:=56.25)
         With ctrl
             .Name = Choose(i, "RollForward", "PrintAll_Standard")
             .Placement = xlMove
             .PrintObject = False
             With .Object
                 .BackColor = Choose(i, &HC0FFC0, &HFFC0C0)
                 .Caption = Choose(i, "Roll Forward To New Month", _
                 "Print Working Paper & AJE")
                 .Enabled = True
                 .Font.Name = "Times New Roman"
                 .Font.Size = 10
                 .Font.Bold = True
                 .TakeFocusOnClick = False
                 .WordWrap = True
             End With
         End With
     Next
     'TargetBook.VBProject.VBComponents.Add(1) 'to add a new Module1
     With TargetBook.VBProject.VBComponents(TargetSheet.CodeName).CodeModule
             LineNum = .CountOfLines + 1
             .InsertLines LineNum, _
                 "Private Sub RollForward_Click()" & vbLf & _
                 "     Msgbox ""Here is the new procedure"" " & vbLf & _
                 "End Sub"
     End With
    With TargetBook.VBProject.VBComponents(TargetSheet.CodeName).CodeModule
             LineNum = .CountOfLines + 1
             .InsertLines LineNum, _
                 "Private Sub PrintAll_Standard_Click()" & vbLf & _
                 "     Msgbox ""Here is the new procedure"" " & vbLf & _
                 "End Sub"
     End With
     Application.ScreenUpdating = True
     End Sub
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Hi Ken,

    gee this is great! Exactly what I needed. Of course now I will try to alter it a bit as now I want to add 3 buttons, but reading the code believe its gonna be easy.

    Thank you very much!
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  4. #4
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Hi again Ken,

    worked just fine, this is my final code:


    Sub Add_Command_Buttons()
        Dim cl As Range, myCmdObj As OLEObject, i, LineNum As Integer
        Dim TargetSheet As Worksheet, TargetBook As Workbook
        Application.ScreenUpdating = False
        Set TargetSheet = ActiveSheet
        Set TargetBook = ActiveWorkbook
    For Each cl In [A14,A20,A25]
            i = i + 1
            Set myCmdObj = TargetSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
            Left:=cl.Left + 1, Top:=cl.Top + 1, Width:=202.5, Height:=32.25)
        With myCmdObj
                .Name = Choose(i, "cmdReexibirNC", "cmdSubtotalNC", "cmdResumo")
                .Placement = xlMove
                .PrintObject = False
                With .Object
                    .Caption = Choose(i, "Reexibe todas colunas e remove subtotais", _
                    "Gera subtotais e oculta colunas", _
                    "Analisa outras opera??es")
                    .Enabled = True
                    .Font.Size = 10
                    .Font.Bold = True
                    .TakeFocusOnClick = False
                    .WordWrap = True
                End With
            End With
        Next
        Set myCmdObj = Nothing
        With TargetBook.VBProject.VBComponents(TargetSheet.CodeName).CodeModule
            LineNum = .CountOfLines + 1
            .InsertLines LineNum, "Private Sub cmdReexibirNC_Click()"
            .InsertLines LineNum + 1, vbNewLine
            .InsertLines LineNum + 2, vbTab & "Reexibe_NC"
            .InsertLines LineNum + 3, vbNewLine
            .InsertLines LineNum + 4, "End Sub"
        End With
    With TargetBook.VBProject.VBComponents(TargetSheet.CodeName).CodeModule
            LineNum = .CountOfLines + 1
            .InsertLines LineNum, "Private Sub cmdSubtotalNC_Click()"
            .InsertLines LineNum + 1, vbNewLine
            .InsertLines LineNum + 2, vbTab & "Subtotal_NC"
            .InsertLines LineNum + 3, vbNewLine
            .InsertLines LineNum + 4, "End Sub"
        End With
    With TargetBook.VBProject.VBComponents(TargetSheet.CodeName).CodeModule
            LineNum = .CountOfLines + 1
            .InsertLines LineNum, "Private Sub cmdResumo_Click()"
            .InsertLines LineNum + 1, vbNewLine
            .InsertLines LineNum + 2, vbTab & "Analisa"
            .InsertLines LineNum + 3, vbNewLine
            .InsertLines LineNum + 4, "End Sub"
        End With
        Application.ScreenUpdating = True
    End Sub
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Glad it worked!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  6. #6
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location

    Ignore this one

    Please ignore this thread, I have posted it here http://www.vbaexpress.com/forum/show...4586#post14586, answer there please.

    Hi,

    gee here I am again. Another problem. I need to put the buttons side by side and bellow a row I dont know which is.

    I tried this:

    For Each cl In [replace(Range("B65536").End(xlUp).Address,"$",""), replace(Range("D65536").End(xlUp).Address,"$",""), replace(Range("H65536").End(xlUp).Address,"$","")]

    Didnt work. Any suggestions?
    Last edited by Paleo; 02-02-2005 at 01:01 PM. Reason: Post transferred
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  7. #7
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location

    Ignore this one, just read the previous.

    This one is already solved, just read the previous, please.

    This is the one I wanna change and t has another error. Two buttons are being positioned one above another. The ones from column "B".


    For Each cl In [B5664,B5668,H5664] 'update this range to place buttons
    i = i + 1
    Set myCmdObj = TargetSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
    Left:=cl.Left + 1, Top:=cl.Top + 1, Width:=202.5, Height:=32.25)

    Any suggestions about this thread and the one before it?
    Last edited by Paleo; 02-02-2005 at 12:03 PM. Reason: Solved
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •