+ Reply to Thread
Results 1 to 8 of 8

Macro to assign a Macro

Hybrid View

jordan2322 Macro to assign a Macro 01-16-2012, 05:17 PM
OnErrorGoto0 Re: Macro to assign a Macro 01-16-2012, 05:24 PM
jordan2322 Re: Macro to assign a Macro 01-16-2012, 06:05 PM
OnErrorGoto0 Re: Macro to assign a Macro 01-16-2012, 06:38 PM
snb Re: Macro to assign a Macro 01-16-2012, 06:51 PM
jordan2322 Re: Macro to assign a Macro 01-16-2012, 07:17 PM
OnErrorGoto0 Re: Macro to assign a Macro 01-17-2012, 04:11 AM
snb Re: Macro to assign a Macro 01-17-2012, 04:53 AM
  1. #1
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Question Macro to assign a Macro

    Hi guys,

    I have a macro that prompts the sheet user to select the reports to 'load'. This opens up an external workbook, copies the data, then shuts the workbook down, and inserts command buttons onto the worksheet where the data was copied to.
    Is it possible to assign a code to these command buttons that doesnt reside in the worksheet. i.e. can i have hte code in a module then assign to each of these newly created command buttons my macro all in the same operation?
    the macro i wish to assign is below
    Sub HideCrews()
    
        Dim i As Integer
        Dim j As String
        'Dim k As Integer
        Dim Col As String
        Dim Sht As String
        Dim This
        
        Application.ScreenUpdating = False
        
        Col = Sheets("Data").Cells(8, 1).Value
        
        Sht = ActiveSheet.Name
        
        Sheets("Data").Visible = True
        Sheets("Data").Select
        Sheets("Data").Range(Col & "3").Select
        Selection.End(xlDown).Select
        j = ActiveCell.Row
        
        For i = 3 To j
        Sheets(Sht).Select
        If (Sheets("Data").Cells(i, Col).Offset(, 1).Value = "0") Then
        Sheets(Sht).Range(Sheets("Data").Range(Col & i)).EntireRow.Hidden = True
        End If
        Next i
    
        Application.ScreenUpdating = True
        
    End Sub
    Sub ShowCrews()
        Application.ScreenUpdating = False
        Cells.Select
        Selection.EntireRow.Hidden = False
        Range("K14").Select
            Application.ScreenUpdating = True
    End Sub
    All this code does is references my control sheet "data" and hides Named Ranges

    So i want my macro to assign a different macro to the command buttons that my macro creates.

    thanks in advance
    Last edited by jordan2322; 01-16-2012 at 07:17 PM.

  2. #2
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro to assign a Macro

    Hi,
    If you are using Buttons.Add to create the buttons (rather than using ActiveX buttons) then you can simply assign the macro name to the OnAction property of said buttons
    btn.ONAction = "HideCrews"
    as an example.
    Good luck.

  3. #3
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Re: Macro to assign a Macro

    Sub AddButtons()
    
        ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
            , DisplayAsIcon:=False, Left:=1059.75, Top:=8.25, Width:=99.75, Height _
            :=36).Select
        ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
            , DisplayAsIcon:=False, Left:=1064.25, Top:=52.5, Width:=96.75, Height _
            :=30.75).Select
        Selection.ShapeRange.IncrementLeft -3#
    End Sub
    This is what i'm using to add my buttons.
    Thank you for your continuing support OnError!

  4. #4
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro to assign a Macro

    Those are ActiveX buttons. Unless you have a particular need to use them, I would recommend using Forms buttons instead
    Dim btn As Button
        Set btn = ActiveSheet.Buttons.Add(Left:=1059.75, Top:=8.25, Width:=99.75, Height:=36)
    btn.Onaction = "HideCrews"
    as an example.

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Macro to assign a Macro

    Here you can find how to do add eventcode for an activeX control.



  6. #6
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Re: Macro to assign a Macro

    Thanks heaps guys, got this to work using form control buttons and
    Sub AssignMacro()
    Dim i As Integer
    
    With ActiveSheet
    For i = 1 To .Shapes.Count
    If .Shapes.Item(i).Type = msoFormControl Then
    .Select
        If Selection = xlButtonControl Then
        .Shapes.Item(i).OnAction = "HideCrews"
        End If
    End If
    Next i
    End With
    
    End Sub

  7. #7
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Macro to assign a Macro

    Much simpler to iterate the Buttons collection, but never mind.

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Macro to assign a Macro

    Like OnError said:

    sub snb()
      for each btn in sheets(1).buttons
        btn.onaction="HideCrews"
      next
    end sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1