Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ...

How to multiply a value of a row of a column to all the values in another column and do the loop for all rows.Then, at the end, sum the values in each row?

The loop works only for the 1st two rows in Column 3..!!? I can not figure it out! Thanks a lot for your help!

Here is an example.
Please note that I do not need to have or show these three columns (I, II, III). I just put them in there to show the steps. Column 2 could have lots of rows...




Column 1	Column 2	I	II	III	   Column 3 (I+II+III)
  40	           1    40			      40
  30	           2	30	80		      110
  20	           5	20	60	200	      280
  10		            10  40	150	      200
   5		            5	20	100	      125
		                    10   50	      60
		       	                 25	      25


What I have tried:

Dim i As Integer
For k As Integer = 0 To Data1.ColumnCount - 4

For j = 0 to Data1.Rows.Count - 2

Data1.Rows(j).Cells(2).Value = Data1.Rows(j).Cells(0).Value * _
Data1.Rows(j).Cells(1).Value 


For i = 0 To EOF()

i = i + 1

Data1.Rows(j).Cells(3).Value = Data1.Rows(j).Cells(0).Value *_ 
Data1.Rows(k+i).Cells(1).Value + Data1.Rows(j).Cells(2).Value

Next i 
Next j
Next k
Posted
Updated 11-Oct-18 1:52am
v3
Comments
Alek Massey 27-Sep-18 11:48am    
Use the debugger to step through your code.
Member 13998781 27-Sep-18 12:50pm    
Thanks for the comment. I did try but I could not find out why?! any help or tip would be appreciated! thanks.

1 solution

See the line
VB
For k As Integer = 0 To Data1.ColumnCount - 4
You are ignoring the last 3 columns. That line should probably read
For k As Integer = 0 To Data1.ColumnCount - 1
Or you could use For Each to avoid the problem.
Also look at line
VB
For j = 0 to Data1.Rows.Count - 2
You are ignoring the last row. That line should probably read
For j = 0 to Data1.Rows.Count - 1
Now look at your inner loop
VB
For i = 0 To EOF()

i = i + 1
You are incrementing the counter i within the For loop, so the first time through that loop i = 1 (not 0). The second time through the loop i = 3 not 1, third time 5 not 2 and so on. I can't really understand why you have three loops to traverse a two-dimensional object and I have no idea what the function EOF() is doing.

You also need to understand that each column in the grid has the same number of "rows" - Grids have Rows (and Columns), Columns do not.

You've already had a go at debugging but didn't notice these issues, so hopefully this article - Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^] - will help you become more proficient
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900