Results 1 to 7 of 7

Thread: [RESOLVED] Possibly very simple vba code question.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Resolved [RESOLVED] Possibly very simple vba code question.

    Excel 2010 spreadsheet.

    I want to check if a cell has a value (not blank) and then add another cell in the same row to an ongoing total, but am drawing a blank on how to do it

    Pseudo code

    Code:
    Loop
       a+1
       If cell(a,3) <> "" then
          iTotal += cell(b,3)
       End if
    While row(a) <> ""
    For some reason I'm just not seeing how to do that
    http://www.scaryminds.com - Horror's last colonial outpost.

  2. #2
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Possibly very simple vba code question.

    if i understand u corectly, try like this
    Code:
    Dim LastCol As Long, iTotal As Single
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 1 To LastCol
        If Cells(1, i) <> "" Then
            iTotal = iTotal + Cells(1, i)
        End If
    Next i
    MsgBox iTotal
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Possibly very simple vba code question.

    @ seenu

    i would be reasonably sure that the OP would wand to loop through rows (not columns)
    and the total would be from some other cell on the same row, though i could not determine which

    personally i prefer the
    vb Code:
    1. if not isempty(cells(i, 1)) then
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Possibly very simple vba code question.

    @Pete
    other cell in same row means how come next row? i think op says column
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  5. #5
    New Member
    Join Date
    Apr 2012
    Location
    Right in the middle of Europe
    Posts
    12

    Re: Possibly very simple vba code question.

    @ Seenu & Pete
    I have to agree w Pete, to me it seems as rows are asked for rather than columns.

    Sample code suggests looping down thru rows rather than across columns.
    Roger

  6. #6
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Possibly very simple vba code question.

    Kiwi

    I too must go with Pete's interpretation.

    If we're right, then perhaps something like this ...
    Code:
        Dim iTotal As Integer
        For rr = 1 To 1000
            If IsEmpty(Cells(rr, 1)) Then
                Exit For
            Else
                iTotal = iTotal + Cells(rr, 2)
            End If
        Next rr
    The loop will sum the contents of col B .. Cells(rr, 2)
    The loop will exit at the first row that has a blank in col A .. Cells(rr, 1).

    If that is not what you are after, then perhaps you could
    elaborate further on your needs.

    Spoo

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Re: Possibly very simple vba code question.

    Thanks for the help, have it up and running like a JRT onto a cat ... unfort we're now changed requirements
    http://www.scaryminds.com - Horror's last colonial outpost.

Posting Permissions

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



Click Here to Expand Forum to Full Width