I've been working on a cashflow which ZVI was kind enough to help me get started, however, after working with it a bit I found the code didn't handle things the way I needed.
What the below code does is loop through a given column, totalling the rows based on cell colour. Yellow = y_total, light purple = x_total, everything else is added to total and it's all added into the grand_total.
Works great, except that not every item is purchased every month so there are blank cells throughout the data.
So I thought, simple enough, just a quick modification should solve things:
So rather than the loop going through the pricing data which has blank cells it goes through the item names which doesn't have blank cells. Then, inside the loop with the pricing column add up the totals. Seems to make sense but vba doesn't like it for whatever reason.
So, trying a different route... lets change the offset to reference the A column
Doesn't like this either, and I'm assuming it's because A column is not part of the with statement's object.
This went on and on... for each loops, .End(xlUp), etc, etc - something wrong with each and every method and most of them spitting out generic/unhelpful errors.
Meanwhile in php/mysql the exact same operation is as simple as:
Is it just me or should there not be a much simpler way to loop to the end of a set of data in a macro?
What the below code does is loop through a given column, totalling the rows based on cell colour. Yellow = y_total, light purple = x_total, everything else is added to total and it's all added into the grand_total.
Code:
With Worksheets(Worksheet).Range(start_cell)
Do Until IsEmpty(.Offset(i).Value)
With (.Offset(i))
v = .Value
CIndex = .Interior.ColorIndex
If CIndex = 39 Then
x_total = x_total + v
ElseIf CIndex = 36 Then
y_total = y_total + v
Else
total = total + v
End If
grand_total = grand_total + v
i = i + 1
End With
Loop
End With
Works great, except that not every item is purchased every month so there are blank cells throughout the data.
So I thought, simple enough, just a quick modification should solve things:
Code:
With Worksheets(Worksheet).Range(A2)
Do Until IsEmpty(.Offset(i).Value)
With Worksheets(Worksheet).Range(start_cell)
With (.Offset(i))
v = .Value
CIndex = .Interior.ColorIndex
If CIndex = 39 Then
x_total = x_total + v
ElseIf CIndex = 36 Then
y_total = y_total + v
Else
total = total + v
End If
grand_total = grand_total + v
i = i + 1
End With
End With
Loop
End With
So rather than the loop going through the pricing data which has blank cells it goes through the item names which doesn't have blank cells. Then, inside the loop with the pricing column add up the totals. Seems to make sense but vba doesn't like it for whatever reason.
So, trying a different route... lets change the offset to reference the A column
Code:
With Worksheets(Worksheet).Range(start_cell)
Do Until IsEmpty(Offset(A2, i, 0, 1, 1).Value)
With (.Offset(i))
v = .Value
CIndex = .Interior.ColorIndex
If CIndex = 39 Then
x_total = x_total + v
ElseIf CIndex = 36 Then
y_total = y_total + v
Else
total = total + v
End If
grand_total = grand_total + v
i = i + 1
End With
Loop
End With
Doesn't like this either, and I'm assuming it's because A column is not part of the with statement's object.
This went on and on... for each loops, .End(xlUp), etc, etc - something wrong with each and every method and most of them spitting out generic/unhelpful errors.
Meanwhile in php/mysql the exact same operation is as simple as:
Code:
$x = query("SELECT * FROM expenses WHERE type = $type AND month = $month);
while(fetch_assoc($x))
{
$x_total += $x['x_cost'];
$y_total += $x['y_cost'];
$total += $x['shared_cost'];
$grand_total += $x['shared_cost'] + $x['x_cost'] + $x['y_cost'];
}
Is it just me or should there not be a much simpler way to loop to the end of a set of data in a macro?