+ Reply to Thread
Results 1 to 5 of 5

Run a macro from a workbook on an other workbook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    593

    Run a macro from a workbook on an other workbook

    Hello,

    - I have a macro called "MacroInMain" which resides in a module of a macro enabled workbook called "Main.xlsm".

    - I have an open Excel File called MyFile.xls.

    - I want the "MacroInMain" to perform a task in MyFile.xls, say write the value "test" in cell A1 of the activesheet.

    Any ideas?

    Thanks

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,249

    Re: Run a macro from a workbook on an other workbook

    Sub MacroInMain()
      ActiveSheet.Range("A1").Value = "Test"
    End Sub


    Artik

  3. #3
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    593

    Re: Run a macro from a workbook on an other workbook

    Artic,
    Thanks for looking into this;
    The code you are suggesting, will write the "test" into the "Main.xlsm" file.
    I am trying to activate somehow the other file called "MyFile.xls" and write that value into there.
    FYI

    Thanks

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,249

    Re: Run a macro from a workbook on an other workbook

    Sub MacroInMain()
      Dim wkb As Workbook
      
      If Not IsWorkbookOpen("MyFile.xls") Then
        MsgBox "MyFile.xls is not open", vbExclamation
        Exit Sub
      End If
      
      Set wkb = Application.Workbooks("MyFile.xls")
      
      'without activate, into first worksheet
      wkb.Worksheets(1).Range("A1").Value = "Test"
      
      'or
      'with workbook activation and entry to the active sheet (not necessarily the first one)
      'wkb.Activate
      'ActiveSheet.Range("A1").Value = "Test"
    End Sub
    
    
    Function IsWorkbookOpen(strName As String) As Boolean
      Dim wkb As Workbook
      
      On Error Resume Next
      Set wkb = Application.Workbooks(strName)
      On Error GoTo 0
      
      IsWorkbookOpen = Not wkb Is Nothing
    End Function
    Artik

  5. #5
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    593

    Re: Run a macro from a workbook on an other workbook

    Thank you Artik!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 7
    Last Post: 06-22-2018, 12:07 PM
  2. [SOLVED] VBA Macro pull a certain cell in closed workbook and copy/paste into current workbook
    By Hoover5896 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-08-2017, 01:36 AM
  3. [SOLVED] VBA to open new workbook and run macro from new workbook, which closes old workbook
    By Rerock in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2014, 10:45 AM
  4. [SOLVED] Macro to find data in source workbook and copy paste to target workbook
    By D.Lovell in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 04-23-2014, 06:21 AM
  5. [SOLVED] Save the split the workbook file type as Excel Binary Workbook From Run Macro
    By breadwinner in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-25-2013, 05:09 AM
  6. Replies: 0
    Last Post: 01-07-2013, 01:22 PM
  7. Replies: 0
    Last Post: 07-27-2011, 09:48 PM

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