Consulting

Results 1 to 6 of 6

Thread: Solved: vba code for adding same row next column if condition met

  1. #1
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    244
    Location

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

    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.

    [vba]
    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
    [/vba]

  2. #2
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    244
    Location
    I think I figure it out
    . thanks

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,455
    Location
    Can you post a workbook with expected results as well, I don't get it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Thanks chungtinhlak, can you share your question and provide the solution?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    244
    Location
    [VBA]
    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
    [/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •