Closed Thread
Results 1 to 5 of 5

run a macro from other workbook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-28-2010
    Location
    delhi,india
    MS-Off Ver
    Excel 2007
    Posts
    128

    run a macro from other workbook

    Hi all,
    I want to run a macro named findandcopyusedrange, present in workbook test.xls, from my new active workbook.
    Any suggestions will be appreciated.
    Kp

  2. #2
    Forum Contributor johnjohns's Avatar
    Join Date
    11-19-2007
    Location
    Dubai, UAE
    MS-Off Ver
    2003 and 2007
    Posts
    526

    Re: run a macro from other workbook

    If both the workbooks are open, you can eaily select and run the macro. Else a better option I could find is, save the macro as addin and run from any book you open.

    rgds

    johnjohns

  3. #3
    Forum Contributor
    Join Date
    08-28-2010
    Location
    delhi,india
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: run a macro from other workbook

    Thanks for responding.
    Well actually I am new to excel-vb.So,can you help me with a vb code..
    Kp

  4. #4
    Forum Contributor johnjohns's Avatar
    Join Date
    11-19-2007
    Location
    Dubai, UAE
    MS-Off Ver
    2003 and 2007
    Posts
    526

    Re: run a macro from other workbook

    This is a part of the macro I wrote for a custom menu.
    Sub CreateMenu()
        Dim NewMenu As CommandBarPopup
    
    '   Delete the menu if it already exists
        Call DeleteMenu
        
    '   Find the Help Menu
        Set HelpMenu = CommandBars(1).FindControl(ID:=30010)
        
        If HelpMenu Is Nothing Then
    '       Add the menu to the end
            Set NewMenu = CommandBars(1).Controls.Add _
              (Type:=msoControlPopup, _
               temporary:=True)
        Else
    '      Add the menu before Help
            Set NewMenu = CommandBars(1).Controls.Add _
              (Type:=msoControlPopup, _
               Before:=HelpMenu.Index, _
               temporary:=False)
        End If
    
    '   Add a caption for the menu
        NewMenu.Caption = "&JohnsMenu"
        
    '------------01-------------------------
    '   FIRST MENU ITEM
        Set MenuItem = NewMenu.Controls.Add _
          (Type:=msoControlButton)
        With MenuItem
            .Caption = "Sum &VisibleCells"
            .OnAction = "SumVisibleCells"
            .FaceId = 233
        End With
    
    '------------02-------------------------
    '   SECOND MENU ITEM
    .
    .
    .
    .
    .
    .
    .
    .
    Sub DeleteMenu()
        On Error Resume Next
        CommandBars(1).Controls("JohnsMenu").Delete
    End Sub
    
    Sub SumVisibleCells()
    Dim OneCell
    Dim AllVisibleCells() As String
    Dim TotalVisibleCells As Integer
    Dim SumFormula As String
    Dim TheCellAddress As String
    Dim NextCellAddress As String
    On Error Resume Next
    SumFormula = ""
    i = 0
    For Each OneCell In Selection.SpecialCells(xlCellTypeVisible)
       ReDim Preserve AllVisibleCells(i) As String
       AllVisibleCells(i) = OneCell.Address
       i = i + 1
    Next
    TotalVisibleCells = UBound(AllVisibleCells, 1) + 1
    For i = 1 To TotalVisibleCells
       TheCellAddress = AllVisibleCells(i - 1)
       NextCellAddress = IIf(i >= TotalVisibleCells, AllVisibleCells(UBound(AllVisibleCells, 1)), AllVisibleCells(i))
       If Not IsEmpty(ActiveSheet.Range(TheCellAddress)) Then
         SumFormula = SumFormula & ActiveSheet.Range(TheCellAddress).Address(rowabsolute:=False, columnabsolute:=False) & _
                      IIf(IsEmpty(ActiveSheet.Range(NextCellAddress)), "", "+")
       End If
       If IsEmpty(ActiveSheet.Range(TheCellAddress)) Then
          ActiveSheet.Range(TheCellAddress).Formula = "=" & SumFormula
          SumFormula = ""
       End If
    Next i
    End Sub
    I have given the macro for creating a menu and run a macro from it. you can copy this as a module to a new workbook, make the the required changes and then save as an addin. Select your addin and then run the macro 'createmenu' so that you get an additional menu in excel and you can run your macro(s) from there.

    PS

    CreateMenu I got from the book of John Walkenbach

    rgds

    johnjohns

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,332

    Re: run a macro from other workbook

    This is a duplicate post of this one, so I'm locking it.
    Note: the original post actually contains the answer too.
    Everyone who confuses correlation and causation ends up dead.

Closed 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