+ Reply to Thread
Results 1 to 7 of 7

Calling a macro from an event macro. Using nest loops with if statements

Hybrid View

  1. #1
    Registered User
    Join Date
    10-15-2012
    Location
    severn, md
    MS-Off Ver
    Excel 2010
    Posts
    9

    Calling a macro from an event macro. Using nest loops with if statements

    Hey guys,

    I am somewhat competent at VBA coding so I am a little embarrassed by this problem: When I run a macro normally it works but then when I call it during an event macro a variable changes unexpectedly. The whole point of the event is to trigger after each data entry so that the interface is automated and therefore much cooler! Its not an issue with public variable declaration because the event macro uses mostly its own variable and the necessary variables from the other macro are public. My only thought is that during the regular macro data is entered into cells and that might trigger the event macro again but I'm not sure. I've even identified where it jumps by using message boxes but I don't have a clue why it jumps.

    Here are the relevant parts of my event macro:
    'calling macros corresponding to step 1 activities
    'inserting Row
    If Target.Row = boundary2 - 1 And IsEmpty(Cells(Target.Row, 1)) = False And IsEmpty(Cells(Target.Row, 2)) = False And IsEmpty(Cells(Target.Row, 3)) = False Then
        Call StoreAccounts  'gathers and sets variables needed for next steps
        Call InsertRow1  'inserts a new line so user can add more data
        Call ProceedStep2  'takes relevant data and puts it into the next step on worksheet
        Exit Sub
    End If
    
    'deleting Row
    If Target.Row > boundary1 And Target.Row < boundary2 And IsEmpty(Cells(boundary2 - 1, 1)) = True And IsEmpty(Cells(boundary2 - 1, 2)) = True And IsEmpty(Cells(boundary2 - 1, 3)) = True Then
        rows(boundary2 - 1).delete shift:=xlUp
        Call StoreAccounts
        Call ProceedStep2
        If boundary1 + 3 >= boundary2 Then
            Cells(boundary2 - 2, 1).Select
        Else: Cells(boundary1 + 1, 1).Select
        End If
        Exit Sub
    End If
    Here are the relevant parts of my regular macro ProceedStep2 which is where the error occurs:
    'Inputing entered credit card, loan, and investment Accountholder's First Name into part 2
    For j = 1 To NumAccts  'numaccts=1
        If Accts(j, 3) = "credit card" Or Accts(j, 3) = "loan" Then  'this IF statement is triggered
            For i = 1 To 2
                If RowRef1 = RowRef3 Then
                    rows(RowRef3).Insert
                    RowRef3 = RowRef3 + 1
                End If
                MsgBox "j = " & j  'j=1
                MsgBox "i = " & i  'i=1
                If i = 1 Then
                    Cells(RowRef1, 4) = AcctTab(j) & " payment"
                    Cells(RowRef1, 4).Font.Color = -16776961
                    RowRef1 = RowRef1 + 1
                End If
                MsgBox "j = " & j   'j=4 WTF!?
                If i = 2 Then
                    Cells(RowRef1, 4) = AcctTab(j) & " interest"  'this line is where the error occurs because AcctTab is defined for j=1 only
                    Cells(RowRef1, 4).Font.Color = -16776961
                    RowRef1 = RowRef1 + 1
                End If
            Next i
        End If
        If Accts(j, 3) = "investment" Then
            If RowRef2 = RowRef3 Then
                rows(RowRef3).Insert
                RowRef3 = RowRef3 + 1
            End If
            Cells(RowRef2, 2) = AcctTab(j) & " return"
            Cells(RowRef2, 2).Font.Color = -16776961
            RowRef2 = RowRef2 + 1
        End If
    Next j
    The first message box checking the value of j reads 1 and the second reads 4, when it should be 1 because I have one account inputted. The variables are properly defined and have values set by the macro StoreAccounts. This is part of a complex project interface with about 25 different macros(~1500 lines total) so I don't think posting the whole thing would be a good idea. Also, I know the error has something to do with these parts due to the way I separated the macros. Again it works when I run manually but running by using the event macro causes "j" to jump to 4 for some reason. Please help!!!

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Calling a macro from an event macro. Using nest loops with if statements

    Without seeing all the code, my advice is to put:

    Application.EnableEvents = False
    at the very top of the event macro and

    Application.EnableEvents = True
    at the very bottom of the event macro
    Gary's Student

  3. #3
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Calling a macro from an event macro. Using nest loops with if statements

    Without seeing all the code, my advice is to put:

    Application.EnableEvents = False
    at the very top of the event macro and

    Application.EnableEvents = True
    at the very bottom of the event macro

  4. #4
    Registered User
    Join Date
    10-15-2012
    Location
    severn, md
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Calling a macro from an event macro. Using nest loops with if statements

    This is TOTALLY BIZARRE but I tried putting those commands around different sections of the code and nothing worked....I closed out without saving and then reopened and it just magically worked. This doesn't make sense since i have already manipulated code multiple times and closed/reopened and it didn't work. Could this be some sort of excel/file bug????

  5. #5
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Calling a macro from an event macro. Using nest loops with if statements

    You may be correct.

  6. #6
    Registered User
    Join Date
    10-15-2012
    Location
    severn, md
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Calling a macro from an event macro. Using nest loops with if statements

    Oh..actually I looked at it again and disabling events during the other called macros is what worked... Thanks so much! I didn't think that an event macro could be retriggered while executing itself...weird.

  7. #7
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Calling a macro from an event macro. Using nest loops with if statements

    Not only can Events be re-triggered (? miss-triggered ?), its nearly impossible to determine whats going on.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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