Consulting

Results 1 to 8 of 8

Thread: Solved: BeforeSave Event

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location

    Solved: BeforeSave Event

    Im trying to have vba code automatically change the active cell to cell a1 on all of the worksheets in the workbook beforesave event. This is so when the file is opened the next time all of the sheets will be set to cell a1. The following code is what I came up with but it only works on the activesheet.
    [VBA]
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim wkSheet As Worksheet

    For Each wkSheet In Worksheets
    Range("a1").Select
    Next wkSheet
    End Sub

    [/VBA]
    Any suggestions?
    Thanks
    Gary

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Each worksheet needs to be active when you want to select a range. i.e.

    [vba]
    Thisworkbook.Activate
    For Each wkSheet In Worksheets
    wkSheet.Activate
    Range("A1").Select
    Next
    [/vba]

  3. #3
    VBAX Regular
    Joined
    Mar 2005
    Location
    Helena, MT
    Posts
    90
    Location
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        Dim sh As Worksheet
        For Each sh In Worksheets
            sh.Activate
            Range("a1").Select
        Next sh
        Sheet1.Select
    End Sub
    This seems to work

    lenze

  4. #4
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Thanks DRJ you fixed my code and solved it.
    Gary

  5. #5
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You're Welcome

    Take Care

  6. #6
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Thanks for the replies. I came across another issue with this. No matter what sheet you are on, when you hit save it brings you to the last sheet. If you are on the 5th sheet of 18, how do you have it automatically bring you back to the 5th sheet when you hit save since it no longer is the active sheet?

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Untested, but should work

    [vba]

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim thisSheet As Worksheet
    Dim sh As Worksheet
    Set thisSheet = ActiveSheet
    For Each sh In Worksheets
    sh.Activate
    Range("a1").Select
    Next sh
    thisSheet.Activate
    End Sub
    [/vba]

  8. #8
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Thanks xld that does work without a hitch.
    Gary

Posting Permissions

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