Consulting

Results 1 to 9 of 9

Thread: Solved: select previous sheet that I viewed

  1. #1

    Solved: select previous sheet that I viewed

    I'm using this code that selects sheet f2106 before it prints. How do i select the previous sheet i was viewing after the code finishes?

    i.e.
    Sheet1 displayed.
    when the code finishes
    re-select sheet1


    With Sheets("f2106")
        .Select
        .PageSetup.PrintArea = "A1:AC36"
        Application.Dialogs(xlDialogPrint).Show
    End With
    Last edited by Aussiebear; 04-04-2023 at 04:58 AM. Reason: Adjusted the code tags

  2. #2
    I found the answer...thanks

    ActiveSheet.Previous.Select
    Last edited by Aussiebear; 04-04-2023 at 04:58 AM. Reason: Adjusted the code tags

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That doesn't select the sheet you were previously viewing, but the previous sheet in the tabs, and will fail if the activesheet is the first sheet.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Actually that's not what I'm looking for. Still need help. That code selects the previous sheet in succession instead of selecting the sheet i was viewing.

  5. #5
    ok...I got it this time and I tested it..lol

    Dim stLast As String
    stLast = ActiveSheet.Name
    Sheets("f2106").Range("B17") = ActiveSheet.Range("B7")
    With Sheets("f2106")
        .Select
        .PageSetup.PrintArea = "A1:AC36"
        Application.Dialogs(xlDialogPrint).Show
        Sheets(stLast).Activate
    End With
    Last edited by Aussiebear; 04-04-2023 at 04:59 AM. Reason: Adjusted the code tags

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Another way


    'in ThisWorkbook
    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Set LstSht = Sh
    End Sub
    'in Standard Module
    Public LstSht As Worksheet
    
    Sub GoToLast()
    LstSht.Activate
    End Sub
    Last edited by Aussiebear; 04-04-2023 at 05:00 AM. Reason: Adjusted the code tags
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Newbie
    Joined
    Nov 2013
    Posts
    2
    Location
    Quote Originally Posted by mdmackillop View Post
    Another way

    [VBA]
    'in ThisWorkbook
    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Set LstSht = Sh
    End Sub

    'in Standard Module
    Public LstSht As Worksheet

    Sub GoToLast()
    LstSht.Activate
    End Sub[/VBA]
    I can't get that to work. It says object required
    here

    Sub GoToLast()
        LstSht.Activate
    End Sub

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Did you define LastSht, did you set it in the deactivate event?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    or
    Private Sub Workbook_SheetDeActivate(ByVal Sh As Object)
        Me.BuiltinDocumentProperties("subject") = Sh.Name
    End Sub
    
    Sub M_previoussheet()
        Application.Goto Sheets(ThisWorkbook.BuiltinDocumentProperties("subject").Value).Cells(1)
    End Sub

Posting Permissions

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