+ Reply to Thread
Results 1 to 4 of 4

Macro runs, reverts to previous worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    06-03-2009
    Location
    Saskatoon, Canada
    MS-Off Ver
    Excel 2003
    Posts
    15

    Macro runs, reverts to previous worksheet

    I have a workbook which has 9 separate worksheets incorporated in to it. The problem I seem to be encountering is that when I run a macro which is designated for example Sheet4(Payment3), the macro runs fine but then it automatically reverts back to Sheet2(Payment1). The same thing happens no matter which worksheet I'm on...it always reverts back to Sheet2.

    Here is the code that is being activated to run the macro

    Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
        If Target.Range.Address = Sheet4.Range("C8").Address Then Sheet4.Worksheet_Hide_Rows
        If Target.Range.Address = Sheet4.Range("C9").Address Then Sheet4.Worksheet_Unhide_Rows
    
    End Sub
    
    Sub Worksheet_Hide_Rows()
        Dim i As Long
            With Worksheets("Payment3")
            For i = 11 To .Cells(.Rows.Count, "C").End(xlUp).Row
                .Rows(i).Hidden = .Cells(i, "F").Value = 0
            Next i
        End With
    End Sub
    
    Sub Worksheet_Unhide_Rows()
        Worksheets("Payment3").Rows.Hidden = False
    End Sub
    The code is the same as above on all of the worksheets with corresponding sheet references except Sheet1 which is completely different.

    Anyone have any idea why it keeps doing this or what I need to correct this?

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Macro runs, reverts to previous worksheet

    Hi Bruce

    Do you hide sheet1?
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Registered User
    Join Date
    06-03-2009
    Location
    Saskatoon, Canada
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Macro runs, reverts to previous worksheet

    Yes, Sheet 1 would generate the first payment and then as the client bring in more receipts, we'd use subsequent sheets. So when you'd use sheet 2,..3, etc, sheet1 would have already hidden the unused rows

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro runs, reverts to previous worksheet

    Are all the hyperlinks on a given sheet to cells on the same sheet?

    The code would be less confusing (and identical among sheets) is you used Me rather than sheet names or codenames:

    Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
        If Target.Range.Address = "C8" Then
            Me.Worksheet_Hide_Rows
        ElseIf Target.Range.Address = "C9" Then
            Me.Rows.Hidden = False
        End If
    End Sub
    
    Sub Worksheet_Hide_Rows()
        Dim i           As Long
        
        With Me
            For i = 11 To .Cells(.Rows.Count, "C").End(xlUp).Row
                .Rows(i).Hidden = .Cells(i, "F").Value = 0
            Next i
        End With
    End Sub
    Entia non sunt multiplicanda sine necessitate

+ Reply to 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