Consulting

Results 1 to 6 of 6

Thread: VBA code working on one file, giving error on another

  1. #1
    VBAX Newbie
    Joined
    Mar 2023
    Posts
    2
    Location

    VBA code working on one file, giving error on another

    Hi, I've two Excel with Macro files. I copied a couple of pages from one to another. I had two macros, one for exporting to Pdf which is working fine also in the new file, and another one that would hide/show a section of a document.<br>This second one keeps giving error, while in the older file it's still working fine.<br>I don't understand why it's giving error. Can somebody help?

    Sub ShowHideHP2()
    ' RemovePbreak Macro
    Sheets("QUOTE").Select
    Rows("318:536").Select
        If Selection.EntireRow.Hidden = True Then
       Selection.EntireRow.Hidden = False
       'Add Row Page Break
       Worksheets("QUOTE").Rows(318).PageBreak = xlPageBreakManual
       Worksheets("QUOTE").Rows(405).PageBreak = xlPageBreakManual
       Worksheets("QUOTE").Rows(471).PageBreak = xlPageBreakManual
       Else
       Selection.EntireRow.Hidden = True  (ON THIS LINE IS GIVING ERROR)
       'Clear Row Page
       Worksheets("QUOTE").Rows(318).PageBreak = xlPageBreakNone
       Worksheets("QUOTE").Rows(405).PageBreak = xlPageBreakNone
       Worksheets("QUOTE").Rows(471).PageBreak = xlPageBreakNone
    End If
    Sheets("Main calcs").Select
    End Sub
    Last edited by Aussiebear; 03-08-2023 at 11:35 AM. Reason: Added code tags to supplied code

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    331
    Location
    In future, please post code between CODE tags to retain structure and readability.

    If you want to provide file for analysis, follow instructions at bottom of my post.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,054
    Location
    Does this work for you?

    Sub ShowHideHP2()
    ' RemovePbreak Macro
    With Sheets("QUOTE”).Rows("318:536")
     If Selection.EntireRow.Hidden = True Then
         'Clear Row Page
          Rows(318,405,471).PageBreak = xlPageBreakNone
         Elseif
         Selection.EntireRow.Hidden = False
         'Add Row Page Break
          Rows(318,405,471).PageBreak = xlPageBreakManual
    End If
    Sheets("Main calcs").Activate
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Newbie
    Joined
    Mar 2023
    Posts
    2
    Location
    Hi Aussiebear,
    thanks for this. It doesn't work. It gives me syntax Error in lines
    With Sheets("QUOTE”).Rows("318:536")
    and Elseif

    However, I found what was wrong in my code... nothing... I locked the Excel page and that prevented the Macro to work property.
    However, thanks for this, because you made me realise what the error was.

    Thanks

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,054
    Location
    Thank you for that. I am struggling with the logic of your argument however. If Rows (318:536) are visible insert page breaks but if Rows (318:536)are not visible remove page breaks..... my question is if the rows are not visible why do anything with them?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    VBAX Regular
    Joined
    May 2018
    Location
    Sydney
    Posts
    57
    Location
    Greaty point Aussiebear. I wonder if he has any merged cells in range 318 to 536?
    If so, the following might help:

    Sub ShowHideHP2()    ' RemovePbreak Macro
        Dim cell As Range
        
        Sheets("QUOTE").Select
        Rows("318:536").Select
        
        ' Unmerge any merged cells in the selected range
        For Each cell In Selection
            If cell.MergeCells Then
                cell.MergeArea.UnMerge
            End If
        Next cell
        
        If Selection.EntireRow.Hidden = True Then
            Selection.EntireRow.Hidden = False
            'Add Row Page Break
            Worksheets("QUOTE").Rows(318).PageBreak = xlPageBreakManual
            Worksheets("QUOTE").Rows(405).PageBreak = xlPageBreakManual
            Worksheets("QUOTE").Rows(471).PageBreak = xlPageBreakManual
        Else
            Selection.EntireRow.Hidden = True
            'Clear Row Page
            Worksheets("QUOTE").Rows(318).PageBreak = xlPageBreakNone
            Worksheets("QUOTE").Rows(405).PageBreak = xlPageBreakNone
            Worksheets("QUOTE").Rows(471).PageBreak = xlPageBreakNone
        End If
        Sheets("Main calcs").Select
    End Sub
    If you only ever do what you can , you'll only ever be what you are.

Posting Permissions

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