Consulting

Results 1 to 5 of 5

Thread: Help needed resolving Compile Error: Expected End Sub issue

  1. #1
    VBAX Regular
    Joined
    May 2019
    Posts
    9
    Location

    Help needed resolving Compile Error: Expected End Sub issue

    Good day,

    I am creating an Excel template, and I am trying to create a macro to insert a new row at row 27, looping until cells X22 and Y22 are the same value, and to have this macro trigger whenever a change is made to cell D22. Cell Y22 has a formula that will (hopefully) revise itself upward each time a new row is inserted.

    The code I have right now is as follows:
    Private Sub Worksheet_Change(ByVal Target As Range) Dim KeyCells As Range
    Set KeyCells = Range("D22")
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
    Is Nothing Then
    Sub DoWhile_Loop()
    Do While Range("X22").Value <> Range("Y22").Value
    Sub insertRowFormatFromAbove()
    Worksheets(ActiveSheet.Name).Rows(27).Insert Shift:=xlShiftDown
    End Sub
    Loop
    End Sub
    End If
    End Sub
    However, when I try making a change to D22, I get a the "Compile Error: Expected End Sub" error message. Does anyone know what might be wrong and how to fix it?

    Currently using Microsoft Visual Basic for Applications 7.1 and Microsoft Excel for Office 365 version 1904.

    Thank you very much.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I'm guessing that it's because you have a Sub declared within a Sub declared within a Sub

    I'm further guessing that you meant to call the second and third level subs


    Option Explicit
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim KeyCells As Range
        Set KeyCells = Range("D22")
        If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
            DoWhile_Loop    '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< call to sub
        End If
    End Sub
    
    
    Sub DoWhile_Loop()
        Do While Range("X22").Value <> Range("Y22").Value
            insertRowFormatFromAbove 
    '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< call to sub
    Loop End Sub
    Sub insertRowFormatFromAbove() Worksheets(ActiveSheet.Name).Rows(27).Insert Shift:=xlShiftDown End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    May 2019
    Posts
    9
    Location
    Thanks a lot for the help.

    I'm running into another issue, in that I need (and neglected to account for initially ) a way to delete unneeded rows as well. I tried using an if...then setup based on whether X22 was larger or smaller than Y22 (if larger, insert a row, if smaller, delete a row) the DoWhile Loop subroutine, but I just got a Loop without Do error.

    What is the syntax supposed to look like?

    Sub DoWhile_Loop() If Range("X22").Value > Range("Y22").Value Then
    insertRowFormatFromAbove '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< call to sub
    ElseIf Range("X22").Value < Range("Y22").Value Then
    deleteSpecificRow '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< call to sub
    Else
    End If
    Loop
    End Sub
    Thank you very much.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    The macro recorder will usually give you pretty good syntax, but also records EVERYTHING you do, so you have to edit the result (remove .Select is the most common)

    For deleting row, it's better to start with the last row and work your way to the top

    You'll need to disable events before changing the sheet otherwise you'll trigger the event handler from inside the event handler (usually not a good thing)

    I think that your loop will keep on going since I didn't see X22 or Y22 change

    You can simplify a little


    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Application.Intersect(
    Range("D22")
    , Target) Is Nothing Then Exit Sub
    Application.EnableEvents = False ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Do While Range("X22").Value <> Range("Y22").Value Rows(27).Insert Shift:=xlShiftDown Loop Application.EnableEvents = True ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Regular
    Joined
    May 2019
    Posts
    9
    Location
    Everything seems to be working more-or-less as I'd envisioned. Thank you very much for your help.

Posting Permissions

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