-
Apr 18th, 2012, 12:31 AM
#1
Thread Starter
Hyperactive Member
[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
-
Apr 18th, 2012, 04:45 AM
#2
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
-
Apr 18th, 2012, 06:13 AM
#3
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:
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
-
Apr 18th, 2012, 07:51 AM
#4
Re: Possibly very simple vba code question.
@Pete
other cell in same row means how come next row? i think op says column
-
Apr 18th, 2012, 02:21 PM
#5
New Member
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
-
Apr 18th, 2012, 02:59 PM
#6
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
-
Apr 18th, 2012, 11:38 PM
#7
Thread Starter
Hyperactive Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|