Consulting

Results 1 to 7 of 7

Thread: Can't call several subs in a macro

  1. #1
    VBAX Newbie
    Joined
    Aug 2017
    Posts
    3
    Location

    Can't call several subs in a macro

    Hi! I am pretty new with macros and new in this forum,

    I am trying to make several macros work in the same spreadsheet, but:

    Call Worksheet_Change(ByVal Target As Excel.Range)

    doesn't work.

    Here is the whole code:



    Sub InsertNewRowAndReorder()
    
    
    If Not Target.Column = "7" Then Exit Sub
    Application.EnableEvents = False
    
    
    
    Call Worksheet_Change(ByVal Target As Excel.Range)
    Call sbInsertingRows
    Call Makro1
    
    
    End Sub


    Sub Worksheet_Change(ByVal Target As Excel.Range)
    
    
    Me.UsedRange.Sort Key1:=Columns("D"), Order1:=xlAscending, _
    Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom
    
    
    Application.EnableEvents = True
    
    
    Me.UsedRange.Sort Key1:=Columns("C"), Order1:=xlAscending, _
    Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom
    
    
    Application.EnableEvents = True
    Me.UsedRange.Sort Key1:=Columns("A"), Order1:=xlAscending, _
    Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom
    
    
    Application.EnableEvents = True
    
    
    
    
    End Sub


    Sub sbInsertingRows()
    
    
    Range("A2").EntireRow.Insert
    End Sub


    Sub Makro1()
    
    
    Dim i As Integer
    
    
    With ActiveSheet
    totalRows = .Cells(.Rows.Count, "A").End(xlUp).Row
    
    
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    
    
    For i = 2 To totalRows
    If .Cells(i, 100).Value > 0 Then
    Number = .Cells(i, 19).Value
    Do While Number > 0
    lastRow = lasRow + 1
               
    .Rows(i).Copy
    .Rows(lastRow).PasteSpecial xlPasteValues
    Number = Number - 1
    Loop
    End If
    Next i
    End With
    End Sub



    Any Ideas very or tips welcome, thanks!!
    Last edited by Paul_Hossler; 08-24-2017 at 05:22 AM.

  2. #2
    Worksheet_Change is an event macro and is not supposed to be called directly, but runs whenever a change is made to a cell on the worksheet the event belongs to.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    1. I added CODE tags for you around your macros - you can use the [#] icon to insert them next time and paste your macro between them

    2. It's really not clear what you're trying to do. Can you describe it in words?
    ---------------------------------------------------------------------------------------------------------------------

    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

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Some Basic VBA

    One does not call a Change Event, One makes a change and that triggers the Event.
    Some examples that will trigger a Worksheet Change Event:
    Change the value of a cell
    Insert a Row
    Sort a Table

    Setting Application.EnableEvents = True is only effective after setting it to False
    Application.EnableEvents = False will prevent Changes from triggering a Change Event.
    As written, your Sub Worksheet_Change(ByVal Target As Excel.Range) will trigger three Change Events, (Each Sort is a Change)

    Sub Makro1 is very well written and should work. However, it will trigger many Change Events, equal in number to the Sum of all the values in column(19).

    I hopt this helps you
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Newbie
    Joined
    Aug 2017
    Posts
    3
    Location
    Thank you all, sorry, I am just starting with VBA. What I am doing is a database, you insert values in a row and when a value is inserted in column G, the whole row is arranged and a new empty row appears in row 2 (so you dont have to scroll all the way down to put new values)

    I understood your comments and changed the code to:

    Sub Worksheet_Change(ByVal Target As Excel.Range)
    
        If Not Target.Column = "7" Then Exit Sub
        Application.EnableEvents = False
    
    
          Me.UsedRange.Sort Key1:=Columns("D"), Order1:=xlAscending, _
          Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
          Orientation:=xlTopToBottom
    
    
        Application.EnableEvents = True
    
    
          Me.UsedRange.Sort Key1:=Columns("C"), Order1:=xlAscending, _
          Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
          Orientation:=xlTopToBottom
    
    
        Application.EnableEvents = True
          Me.UsedRange.Sort Key1:=Columns("A"), Order1:=xlAscending, _
          Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
          Orientation:=xlTopToBottom
    
    
        Application.EnableEvents = True
    
    
    
    
        Range("A2").EntireRow.Insert
    
    
    Dim i As Integer
    
    
    With ActiveSheet
        totalRows = .Cells(.Rows.Count, "A").End(xlUp).Row
    
    
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    
    
        For i = 2 To totalRows
            If .Cells(i, 100).Value > 0 Then
                Number = .Cells(i, 19).Value
                Do While Number > 0
                    lastRow = lasRow + 1
               
                    .Rows(i).Copy
                    .Rows(lastRow).PasteSpecial xlPasteValues
                    Number = Number - 1
                Loop
            End If
        Next i
    End With
    End Sub
    It works better but doesnt copy a row in row 2 (has to be copied because the cell settings for the new row, including conditional formating)

    Thanks!!!

  6. #6
    VBAX Newbie
    Joined
    Aug 2017
    Posts
    3
    Location
    Oki, I was over complicating it. It works now, thanks!!

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If Not Target.Column = "7" Then Exit Sub
    Target.Column will never be "7" . It may be 7, a numeric value.
    lastRow = lasRow + 1
    Typo; Use Option Explicit and declare all your variables.
    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'

Tags for this Thread

Posting Permissions

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