PDA

View Full Version : Solved: vba code for adding same row next column if condition met



chungtinhlak
01-14-2009, 09:55 AM
I have many calculation that I have to do. This is just a small example:

I have columns, A, C, and D as category. Column B is value. I want to say that if any cell column A = "whatever it is", then I wan to add the value for that same row in column B., I also need a way to count, if a certain condition matches, then count as 1 and add as it goes.

I tried this but it does not work.


Sub test()
Dim i As Long, y As Long, usedcell As Long
Dim checkrange As Range
usedcell = Application.WorksheetFunction.CountA(Range("A:A"))
For Each checkrange In Range("A1:A" & usedcell)
If checkrange = "Thaison" Then
i = checkrange
Range("C").Value = Range("B" & i).Value + Range("B" & i).Value
End If
Next
End Sub

lucas
01-14-2009, 09:59 AM
No example and please give your thread titles meaninful names. Please help is not a meaningful name and when folks are searching the forum they don't know what the thread is about.

chungtinhlak
01-14-2009, 10:25 AM
I think I figure it out
. thanks

Bob Phillips
01-14-2009, 10:29 AM
Can you post a workbook with expected results as well, I don't get it.

lucas
01-14-2009, 10:30 AM
Thanks chungtinhlak, can you share your question and provide the solution?

chungtinhlak
01-14-2009, 11:03 AM
Sub test()
Dim i As Long, x As Long, y As Long, usedcell As Long
Dim checkrange As Range
usedcell = Application.WorksheetFunction.CountA(Range("A:A"))
For i = 2 To usedcell
If Range("A" & i).Value = "thaison" And Range("C" & i).Value = "1" Then
x = Range("B" & i).Value + x
'count how many data set met condition
y = y + 1
End If
Next
Range("G1").Value = x
Range("G2").Value = y

End Sub