rarpro
09-29-2010, 06:03 PM
Hi all,
I have a very large code -probably very inefficient as I am not an expert on VBA- and i got the error of 'too large code", so I split the data on several sub routines. however, I added more code on the first subroutine which is a set of equations within a couple of loops, and again got into the error of too large to compile. So I realize that I might be able to shorten the code because there are many equations that are very similar. I'm posting a small example of the code. I think the code could be shortened, but I tried several ways with no luck. I would really appreciate any help with this.
regards,
For TP = 1 To NT
'THE DATA FOR LOOP TP TO NT IS IN SHEET TY
Sheets("TY").Select
Range("A" & TP + 1).Select
TNO(TP) = ActiveCell.Value
For REGNO = 1 To NREG
'FIRST READS DATA FROM SEVERAL SHEETS
Sheets("XF1").Select
Range("B" & REGNO + 1).Select
P11(REGNO) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
P12(REGNO) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
P13(REGNO) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
P13(REGNO) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
P15(REGNO) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
'....AND SO ON
Sheets("YF1").Select
Range("B" & REGNO + 1).Select
C11(REGNO) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
C12(REGNO) = ActiveCell.Value
'AND SO ON...THEN AFTER READING VALUES FROM THE SHEETS, THE ESTIMATION
'OF SOME EQUATIONS LIKE:
PK11(REGNO) = (P11(REGNO) * Y11(REGNO)) - C11(REGNO)
PK12(REGNO) = (P12(REGNO) * Y12(REGNO)) - C12(REGNO)
PK13(REGNO) = (P13(REGNO) * Y13(REGNO)) - C13(REGNO)
PK14(REGNO) = (P14(REGNO) * Y14(REGNO)) - C14(REGNO)
PK15(REGNO) = (P15(REGNO) * Y15(REGNO)) - C15(REGNO)
PK16(REGNO) = (P16(REGNO) * Y16(REGNO)) - C16(REGNO)
PK17(REGNO) = (P17(REGNO) * Y17(REGNO)) - C17(REGNO)
PK18(REGNO) = (P18(REGNO) * Y18(REGNO)) - C18(REGNO)
PK1(REGNO) = (PK11(REGNO) * W11(REGNO)) + (PK12(REGNO) * W12(REGNO)) + (PK13(REGNO) * W13(REGNO)) + (PK14(REGNO) * W14(REGNO) _
+ PK15(REGNO) * W15(REGNO)) + (PK16(REGNO) * W16(REGNO)) + (PK17(REGNO) * W17(REGNO)) + (PK18(REGNO) * W18(REGNO))
V11(REGNO) = (S11(REGNO)) ^ 2
V12(REGNO) = (S12(REGNO)) ^ 2
V13(REGNO) = (S13(REGNO)) ^ 2
V14(REGNO) = (S14(REGNO)) ^ 2
V15(REGNO) = (S15(REGNO)) ^ 2
V16(REGNO) = (S16(REGNO)) ^ 2
V17(REGNO) = (S17(REGNO)) ^ 2
V18(REGNO) = (S18(REGNO)) ^ 2
SV1(REGNO) = (V11(REGNO) * W11(REGNO) ^ 2 + V12(REGNO) * W12(REGNO) ^ 2 + V13(REGNO) * W13(REGNO) ^ 2 + V14(REGNO) * W14(REGNO) ^ 2 _
+ V15(REGNO) * W15(REGNO) ^ 2 + V16(REGNO) * W16(REGNO) ^ 2 + V17(REGNO) * W17(REGNO) ^ 2 + V18(REGNO) * W18(REGNO) ^ 2) _
+ (RHO1(REGNO) * 2) * (W11(REGNO) * W12(REGNO) * S11(REGNO) * S12(REGNO) _
+ W11(REGNO) * W13(REGNO) * S11(REGNO) * S13(REGNO) + W11(REGNO) * W14(REGNO) * S11(REGNO) * S14(REGNO) _
+ W11(REGNO) * W15(REGNO) * S11(REGNO) * S15(REGNO) + W11(REGNO) * W16(REGNO) * S11(REGNO) * S16(REGNO) _
+ W11(REGNO) * W17(REGNO) * S11(REGNO) * S17(REGNO) + W11(REGNO) * W18(REGNO) * S11(REGNO) * S18(REGNO) _
+ W12(REGNO) * W13(REGNO) * S12(REGNO) * S13(REGNO) + W12(REGNO) * W14(REGNO) * S12(REGNO) * S14(REGNO) _
+ W12(REGNO) * W15(REGNO) * S12(REGNO) * S15(REGNO) + W12(REGNO) * W16(REGNO) * S12(REGNO) * S16(REGNO) _
+ W12(REGNO) * W17(REGNO) * S12(REGNO) * S17(REGNO) + W12(REGNO) * W18(REGNO) * S12(REGNO) * S18(REGNO) _
+ W13(REGNO) * W14(REGNO) * S13(REGNO) * S14(REGNO) + W13(REGNO) * W15(REGNO) * S13(REGNO) * S15(REGNO) _
+ W13(REGNO) * W16(REGNO) * S13(REGNO) * S16(REGNO) + W13(REGNO) * W17(REGNO) * S13(REGNO) * S17(REGNO) _
+ W13(REGNO) * W18(REGNO) * S13(REGNO) * S18(REGNO) + W14(REGNO) * W15(REGNO) * S14(REGNO) * S15(REGNO) _
+ W14(REGNO) * W16(REGNO) * S14(REGNO) * S16(REGNO) + W14(REGNO) * W17(REGNO) * S14(REGNO) * S17(REGNO) _
+ W14(REGNO) * W18(REGNO) * S14(REGNO) * S18(REGNO) + W15(REGNO) * W16(REGNO) * S15(REGNO) * S16(REGNO) _
+ W15(REGNO) * W17(REGNO) * S15(REGNO) * S17(REGNO) + W15(REGNO) * W18(REGNO) * S15(REGNO) * S18(REGNO) _
+ W16(REGNO) * W17(REGNO) * S16(REGNO) * S17(REGNO) + W16(REGNO) * W18(REGNO) * S16(REGNO) * S18(REGNO) _
+ W17(REGNO) * W18(REGNO) * S17(REGNO) * S18(REGNO))
Next REGNO
Sheets("ax1").Select
Range("b1").Select
Next TP
'then estimated values are written in a different sheet
I have a very large code -probably very inefficient as I am not an expert on VBA- and i got the error of 'too large code", so I split the data on several sub routines. however, I added more code on the first subroutine which is a set of equations within a couple of loops, and again got into the error of too large to compile. So I realize that I might be able to shorten the code because there are many equations that are very similar. I'm posting a small example of the code. I think the code could be shortened, but I tried several ways with no luck. I would really appreciate any help with this.
regards,
For TP = 1 To NT
'THE DATA FOR LOOP TP TO NT IS IN SHEET TY
Sheets("TY").Select
Range("A" & TP + 1).Select
TNO(TP) = ActiveCell.Value
For REGNO = 1 To NREG
'FIRST READS DATA FROM SEVERAL SHEETS
Sheets("XF1").Select
Range("B" & REGNO + 1).Select
P11(REGNO) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
P12(REGNO) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
P13(REGNO) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
P13(REGNO) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
P15(REGNO) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
'....AND SO ON
Sheets("YF1").Select
Range("B" & REGNO + 1).Select
C11(REGNO) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
C12(REGNO) = ActiveCell.Value
'AND SO ON...THEN AFTER READING VALUES FROM THE SHEETS, THE ESTIMATION
'OF SOME EQUATIONS LIKE:
PK11(REGNO) = (P11(REGNO) * Y11(REGNO)) - C11(REGNO)
PK12(REGNO) = (P12(REGNO) * Y12(REGNO)) - C12(REGNO)
PK13(REGNO) = (P13(REGNO) * Y13(REGNO)) - C13(REGNO)
PK14(REGNO) = (P14(REGNO) * Y14(REGNO)) - C14(REGNO)
PK15(REGNO) = (P15(REGNO) * Y15(REGNO)) - C15(REGNO)
PK16(REGNO) = (P16(REGNO) * Y16(REGNO)) - C16(REGNO)
PK17(REGNO) = (P17(REGNO) * Y17(REGNO)) - C17(REGNO)
PK18(REGNO) = (P18(REGNO) * Y18(REGNO)) - C18(REGNO)
PK1(REGNO) = (PK11(REGNO) * W11(REGNO)) + (PK12(REGNO) * W12(REGNO)) + (PK13(REGNO) * W13(REGNO)) + (PK14(REGNO) * W14(REGNO) _
+ PK15(REGNO) * W15(REGNO)) + (PK16(REGNO) * W16(REGNO)) + (PK17(REGNO) * W17(REGNO)) + (PK18(REGNO) * W18(REGNO))
V11(REGNO) = (S11(REGNO)) ^ 2
V12(REGNO) = (S12(REGNO)) ^ 2
V13(REGNO) = (S13(REGNO)) ^ 2
V14(REGNO) = (S14(REGNO)) ^ 2
V15(REGNO) = (S15(REGNO)) ^ 2
V16(REGNO) = (S16(REGNO)) ^ 2
V17(REGNO) = (S17(REGNO)) ^ 2
V18(REGNO) = (S18(REGNO)) ^ 2
SV1(REGNO) = (V11(REGNO) * W11(REGNO) ^ 2 + V12(REGNO) * W12(REGNO) ^ 2 + V13(REGNO) * W13(REGNO) ^ 2 + V14(REGNO) * W14(REGNO) ^ 2 _
+ V15(REGNO) * W15(REGNO) ^ 2 + V16(REGNO) * W16(REGNO) ^ 2 + V17(REGNO) * W17(REGNO) ^ 2 + V18(REGNO) * W18(REGNO) ^ 2) _
+ (RHO1(REGNO) * 2) * (W11(REGNO) * W12(REGNO) * S11(REGNO) * S12(REGNO) _
+ W11(REGNO) * W13(REGNO) * S11(REGNO) * S13(REGNO) + W11(REGNO) * W14(REGNO) * S11(REGNO) * S14(REGNO) _
+ W11(REGNO) * W15(REGNO) * S11(REGNO) * S15(REGNO) + W11(REGNO) * W16(REGNO) * S11(REGNO) * S16(REGNO) _
+ W11(REGNO) * W17(REGNO) * S11(REGNO) * S17(REGNO) + W11(REGNO) * W18(REGNO) * S11(REGNO) * S18(REGNO) _
+ W12(REGNO) * W13(REGNO) * S12(REGNO) * S13(REGNO) + W12(REGNO) * W14(REGNO) * S12(REGNO) * S14(REGNO) _
+ W12(REGNO) * W15(REGNO) * S12(REGNO) * S15(REGNO) + W12(REGNO) * W16(REGNO) * S12(REGNO) * S16(REGNO) _
+ W12(REGNO) * W17(REGNO) * S12(REGNO) * S17(REGNO) + W12(REGNO) * W18(REGNO) * S12(REGNO) * S18(REGNO) _
+ W13(REGNO) * W14(REGNO) * S13(REGNO) * S14(REGNO) + W13(REGNO) * W15(REGNO) * S13(REGNO) * S15(REGNO) _
+ W13(REGNO) * W16(REGNO) * S13(REGNO) * S16(REGNO) + W13(REGNO) * W17(REGNO) * S13(REGNO) * S17(REGNO) _
+ W13(REGNO) * W18(REGNO) * S13(REGNO) * S18(REGNO) + W14(REGNO) * W15(REGNO) * S14(REGNO) * S15(REGNO) _
+ W14(REGNO) * W16(REGNO) * S14(REGNO) * S16(REGNO) + W14(REGNO) * W17(REGNO) * S14(REGNO) * S17(REGNO) _
+ W14(REGNO) * W18(REGNO) * S14(REGNO) * S18(REGNO) + W15(REGNO) * W16(REGNO) * S15(REGNO) * S16(REGNO) _
+ W15(REGNO) * W17(REGNO) * S15(REGNO) * S17(REGNO) + W15(REGNO) * W18(REGNO) * S15(REGNO) * S18(REGNO) _
+ W16(REGNO) * W17(REGNO) * S16(REGNO) * S17(REGNO) + W16(REGNO) * W18(REGNO) * S16(REGNO) * S18(REGNO) _
+ W17(REGNO) * W18(REGNO) * S17(REGNO) * S18(REGNO))
Next REGNO
Sheets("ax1").Select
Range("b1").Select
Next TP
'then estimated values are written in a different sheet