Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another object-defined error

Status
Not open for further replies.

sarmizzle

Programmer
Oct 15, 2007
2
US
So I'm getting another object-defined error. What am I doing wrong?

The hilighted parts are the four statements: 'if i=....and j=...'


for w=1 to 134
For I = 1 To 8
For j = I + 1 To 9

If I = 2 Then c = 28
If I = 3 Then c = 16
If I = 4 Then c = 17
If I = 5 Then c = 19
If I = 6 Then c = 20
If I = 7 Then c = 21
If I = 8 Then c = 23

If j = 2 Then d = 28
If j = 3 Then d = 16
If j = 4 Then d = 17
If j = 5 Then d = 19
If j = 6 Then d = 20
If j = 7 Then d = 21
If j = 8 Then d = 23
If j = 9 Then d = 25


If I = 1 And j = 9 Then Z = Z + Worksheets("Cor.Cov. Tables").Cells(I + 13, j + 12) * (1 / Worksheets("Coefficients").Cells(23, 8)) + Worksheets("Normalized Expanded").Cells(w, 25)
If I > 1 And j = 9 Then Z = Z + Worksheets("Cor.Cov. Tables").Cells(I + 13, j + 12) * Log(Worksheets("NOrmalized Expanded").Cells(w, c)) * Worksheets("Normalized Expanded").Cells(w, 25)
If I > 1 And j < 9 Then Z = Z + Worksheets("Cor.Cov. Tables").Cells(I + 13, j + 12) * Log(Worksheets("Normalized Expanded").Cells(w, c)) * Log(Worksheets("Normalized Expanded").Cells(w, d))
If I = 1 And j < 9 Then Z = Z + Worksheets("Cor.Cov. Tables").Cells(I + 13, j + 12) * (1 / Worksheets("Coefficients").Cells(23, 8)) * Log(Worksheets("Normalized Expanded").Cells(w, d))
Next j

Next I
 





faq707-4594

On the statement that ther error is on, put a BREAK in the VB Editor.

Use the Watch Window to observe the row & column values in the Cells objects.

Somewhere, you have an invalid row/col value.

Skip,

[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue]
 
1. Please use the TGML code tags. You have been asked already.

2. "The hilighted parts are the four statements: 'if i=....and j=...'" WHICH four????? There are EIGHT if j = statements. Look in the TGML help and actually highlight the error parts in your posts. That way we can see where you are talking about.

3. You may want to consider using Select Case instead of all those If statements.
Code:
If I = 2 Then c = 28
If I = 3 Then c = 16
If I = 4 Then c = 17
If I = 5 Then c = 19
If I = 6 Then c = 20
If I = 7 Then c = 21
If I = 8 Then c = 23
Say I = 2. OK, then c = 28. But your code will still execute each of the the other If statements:
Code:
If I = 3 Then c = 16
If I = 4 Then c = 17
If I = 5 Then c = 19
If I = 6 Then c = 20
If I = 7 Then c = 21
If I = 8 Then c = 23
even though I = 2. Why do that?????
Code:
Select Case I
    Case 2
       c = 28
    Case 3
       c = 16
    Case 4
       c = 17
    Case 5
       c = 19
    Case 6
       c = 20
    Case 7
       c = 21
    Case 8
       c = 23
End Select
No matter what the value of I is, only ONE case applies, and only ONE instruction is executed. Use Select Case when testing multiple values for a single variable.

For example: If I = 1, then all those If statements mean,

(For j = I + 1 To 9 OR j = 2 To 9 ie. 7 iterations)

15 If statements x 7 iterations = 105 instructions executed

With Select Case:

2 Select Case (one for I, and j) x 7 iterations = 14 instructions executed.


faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top