+ Reply to Thread
Results 1 to 11 of 11

Thread: Run macro with more than 1 workbook open

  1. #1
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    183

    Run macro with more than 1 workbook open

    Hi All,
    I currently have a macro set up to run once a workbook is saved/ closed. This works fine unless tehre is more than one workbook open. When I try it with more than 1 different workbook open I get a run error.

    My code begins:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    and then performs the macro. I don't really want to specify the filename as this is liable to change. Is there anyway of adding a "this workbook" command to the text string, and if so where would it go?

    Thanks

  2. #2
    Forum Guru mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,984

    Re: Run macro with more than 1 workbook open

    Please take a couple of minutes and read ALL theForum Rules then wrap your VBA code (Rule 3)
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Forum Guru martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    10,780

    Re: Run macro with more than 1 workbook open

    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and a dabbler in Cisco
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  4. #4
    Valued Forum Contributor
    Join Date
    04-18-2009
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    256

    Thumbs up Re: Run macro with more than 1 workbook open

    Hi,

    I hope you are using ThisWorkbook everywhere in the code.

    If you post the entire code, I can try to sort it out. Please give it in Code Tags so that it is easy to read.

    --Karan--

  5. #5
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    183

    Re: Run macro with more than 1 workbook open

    Hi Karan,

    please see the below code

    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
         
    
    
    Sheets("Main").Select
    ActiveSheet.Unprotect ("password")
    Dim lRow As Long, lCounter As Long
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    For lCounter = 2 To lRow
        If Cells(lCounter, 17) = "X" Then
            With Cells(lCounter, 13).Resize(1, 3)
            .Value = .Value
        End With
        End If
    Next lCounter
    Range("A5:O310").Select
        Selection.Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    ActiveSheet.Protect DrawingObjects:=True, contents:=True, Scenarios:=True, AllowFiltering:=True, Password:="password"
    Range("B5").Select
    Sheets("Open").Select
    
    
    End Sub
    Thanks

  6. #6
    Valued Forum Contributor
    Join Date
    04-18-2009
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    256

    Thumbs up Re: Run macro with more than 1 workbook open

    Use this code:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    ThisWorkbook.Sheets("Main").Select
    ThisWorkbook.ActiveSheet.Unprotect ("password")
    Dim lRow As Long, lCounter As Long
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    For lCounter = 2 To lRow
        If ThisWorkbook.ActiveSheet.Cells(lCounter, 17) = "X" Then
            With ThisWorkbook.ActiveSheet.Cells(lCounter, 13).Resize(1, 3)
            .Value = .Value
        End With
        End If
    Next lCounter
    ThisWorkbook.ActiveSheet.Range("A5:O310").Select
        Selection.Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    ThisWorkbook.ActiveSheet.Protect DrawingObjects:=True, contents:=True, Scenarios:=True, AllowFiltering:=True, Password:="password"
    ThisWorkbook.ActiveSheet.Range("B5").Select
    ThisWorkbook.Sheets("Open").Select
    
    End Sub
    --Karan--

  7. #7
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    183

    Re: Run macro with more than 1 workbook open

    many thanks, will give it a go.

  8. #8
    Valued Forum Contributor
    Join Date
    04-18-2009
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    256

    Re: Run macro with more than 1 workbook open

    Anytime

  9. #9
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    183

    Re: Run macro with more than 1 workbook open

    Ok, sort of works. The problem is – if I have 2 workbooks open (both with the same code) and I click on the big red X – and select “Yes to all” on the save option it still flags up and error on one of the workbooks.

  10. #10
    Forum Guru martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    10,780

    Re: Run macro with more than 1 workbook open

    now did you bother to read the link i gave you?
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and a dabbler in Cisco
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  11. #11
    Forum Contributor
    Join Date
    04-21-2009
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    183

    Re: Run macro with more than 1 workbook open

    Quote Originally Posted by martindwilson View Post
    now did you bother to read the link i gave you?
    Yes, but I must have not set the coding up specifically.

    Karan's coding worked, but I still encounter an error when closing and saving multiple documents

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0