VBA Code Too Long

appleglaze28

New Member
Joined
Aug 11, 2011
Messages
4
Hi! I'm not really good with VBA Script but I'm familiar with the logic of how it works. I'm kinda troubleshooting something in Excel VBA and something was not working right.

Apparently whoever created that VBA Script had a very long conditional statement...too long that he had to cut it. He wrote another SUB process where the rest of the condition are written. But the main process has this condition where it checks the value and if it is meant a certain value is suppose to be linked to a variable. Then that variable goes through another conditional statement.

The programmer who created the script cut the script during the first condition. And I assume since the rest of the condition is in another SUB process. That the variable which is still found in the main process is equivalent to 0 cause the SUB process can't have the value be an output in the main process. Is my assumption right? If so? What's the best way I can remedy this.

And in case someone would recommend cutting the 1st condition short...there's no way I can do that cause it needs to be that specific.

Thanks in advance.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
if the variable is defined globally then no worries, otherwise it needs to be referance through the main sub.

eg sub 1 has a variable = 2

sub 2 wants to know what variable in sub1 is

sub1.variablename.value
 
Upvote 0
How do I have the variable in the 2nd sub to the main sub?

Below is the code
**********************************************************
Sub EvaluateTotal()
'Dim mec, phase, endorse, i
'Dim total As Boolean
total = True
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
For i = 13 To 139
<o:p> </o:p>
Dim char, cc, desc
char = Left(Sheet1.Range("B" & i).Value, 3)
cc = Sheet2.Range("C5").Value

endorse = Run("DBR", Sheet2.Range("C4").Value, Sheet2.Range("D1").Value, Sheet2.Range("D2").Value, Sheet2.Range("D3").Value, Sheet2.Range("D4").Value, Sheet2.Range("M7").Value, Sheet2.Range("M8").Value, Sheet2.Range("C5").Value, Sheet2.Range("M6").Value, Sheet2.Range("D5").Value, char, Sheet2.Range("M9").Value)

If char = 416 Or char = 420 Or char = 421 Then
mec = Run("DBR", Sheet2.Range("C20").Value, Sheet2.Range("D1").Value, Sheet2.Range("D2").Value, Sheet2.Range("D3").Value, Sheet2.Range("D4").Value, Sheet2.Range("M7").Value, Sheet2.Range("M6").Value, Sheet2.Range("M8").Value, Sheet2.Range("F1").Value, Sheet2.Range("F2").Value, Sheet2.Range("F3").Value, Sheet2.Range("C5").Value, Sheet1.Range("B" & i).Value, "Total Line", Sheet2.Range("C13").Value)
phase = Run("DBR", Sheet2.Range("C20").Value, Sheet2.Range("D1").Value, Sheet2.Range("D2").Value, Sheet2.Range("D3").Value, Sheet2.Range("D4").Value, Sheet2.Range("M7").Value, Sheet2.Range("A13").Value, Sheet2.Range("M8").Value, Sheet2.Range("F1").Value, Sheet2.Range("F2").Value, Sheet2.Range("F3").Value, Sheet2.Range("C5").Value, Sheet1.Range("B" & i).Value, "Total Line", Sheet2.Range("A14").Value)
Run "DBSW", mec, "ASC:Monitor Status", Sheet2.Range("M7").Value, Sheet2.Range("C5").Value, Sheet1.Range("B" & i).Value, "MEC"
Else

continue

End If
<o:p> </o:p>
If Round(endorse, 2) = Round(mec, 2) And Round(endorse, 2) = Round(phase, 2) Then
'If Round(endorse, 2) = Round(mec, 2) Then 'And Round(endorse, 2) = Round(phase, 2) Then
Sheet1.Range("V" & i).Borders.ColorIndex = 2
Run "DBSS", "True", "ASC:Monitor Status", Sheet2.Range("M7").Value, Sheet2.Range("C5").Value, Sheet1.Range("B" & i).Value, "Endorsed = MEC"
Else
Sheet1.Range("V" & i).Borders.ColorIndex = 3
Run "DBSS", "False", "ASC:Monitor Status", Sheet2.Range("M7").Value, Sheet2.Range("C5").Value, Sheet1.Range("B" & i).Value, "Endorsed = MEC"
Run "DBSS", "False", "ASC:Monitor Status", Sheet2.Range("M7").Value, "1", "NET EXPENSES", "Endorsed = MEC"
total = False
End If

Next i
<o:p> </o:p>
If total = False Then
Run "DBSS", "False", "ASC:Monitor Status", Sheet2.Range("M7").Value, Sheet2.Range("C5").Value, "NET EXPENSES", "Endorsed = MEC"
MsgBox "New Endorsement not tallied.", vbCritical, "ASC"
Else
Run "DBSS", "True", "ASC:Monitor Status", Trim(str(Sheet2.Range("M7").Value)), Sheet2.Range("C5").Value, "NET EXPENSES", "Endorsed = MEC"
MsgBox "New Endorsement equal to Proposed.", vbInformation, "ASC"
End If
<o:p> </o:p>
End Sub

**********************************************************

CONTINUE is the name of the 2nd SUB or secondary sub...it has the 1st conditional statement format for different "char" value that couldnt fit in the main sub. I put the 2nd conditional statement after CONTINUE for the formatting inside continue. Since base on what I tested out...any variable like "mec & phase" from the 2nd SUB is not being put in the main sub.

Also the Message box doesn't work the correctly...how do I fix it. Cause I experience where the looping doesn't bring the right messagebox message.

I think this code derive the message box during the last loop. Am I right?
 
Upvote 0
I know...took me awhile to figure out what was wrong with it...for some reason the things that are suppose to happen for the Main Sub isnt working for the secondary sub in it. Thanks for the help!
 
Upvote 0
do you have the code for continue?

is there a particular line thats not working? or maybe an example of something that doesnt come out right
 
Upvote 0
Actually its the same as the main....its just that it variable "char" could be a lot of different number that is why its that long...its like char is a list of specific code.

**********************************************************
If char = 416 Or char = 420 Or char = 421 Then
mec = Run("DBR", Sheet2.Range("C20").Value, Sheet2.Range("D1").Value, Sheet2.Range("D2").Value, Sheet2.Range("D3").Value, Sheet2.Range("D4").Value, Sheet2.Range("M7").Value, Sheet2.Range("M6").Value, Sheet2.Range("M8").Value, Sheet2.Range("F1").Value, Sheet2.Range("F2").Value, Sheet2.Range("F3").Value, Sheet2.Range("C5").Value, Sheet1.Range("B" & i).Value, "Total Line", Sheet2.Range("C13").Value)

phase = Run("DBR", Sheet2.Range("C20").Value, Sheet2.Range("D1").Value, Sheet2.Range("D2").Value, Sheet2.Range("D3").Value, Sheet2.Range("D4").Value, Sheet2.Range("M7").Value, Sheet2.Range("A13").Value, Sheet2.Range("M8").Value, Sheet2.Range("F1").Value, Sheet2.Range("F2").Value, Sheet2.Range("F3").Value, Sheet2.Range("C5").Value, Sheet1.Range("B" & i).Value, "Total Line", Sheet2.Range("A14").Value)
Run "DBSW", mec, "ASC:Monitor Status", Sheet2.Range("M7").Value, Sheet2.Range("C5").Value, Sheet1.Range("B" & i).Value, "MEC"
*******************************************************

Specifically the message box. Cause I can't for some reason bring the value of the secondary out to the main. But even that I'm not sure if the message box will work correct.

Cause I'm suppose to make sure the value in the columns match for everyline and if it doesn't it should have the else statement as the message box.
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top