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!!!
Bookmarks