How to loop through all rows of a range?

ConfusedFrank

New Member
Joined
Mar 13, 2015
Messages
6
Hi,

I have coded a macro that effectively copies a dynamic named range to a sheet called Sales chart. The working named range, called ItemsSold, is a single column consisting of uniquely filtered data.

My problem is the second named range; I want it to look up the source data, called Sales tracking (worksheet), and then sumif each row of itemsSold to find the total number of item sales. I want each of the values returned in the QuantitySold named range (Sales chart).

i know I need to create a loop, but I'm really new to VBA, and I don't know how.

From research, I obtained the following code and amended it to fit my question:



Code:
Dim rng As Range
Dim row As Range
Dim cell As Range


Set rng = Range("ItemsSold")


For Each row In rng.Rows
  For Each cell In row.Cells
    Sales Chart.Range("B4").Formula = "=SUM(SUMIF('Sales tracking'B2:B16,'Sales chart'!A4,'Sales tracking'!C2:C16)) 'Relative to each item
  Next cell
Next row

How do I make a macro do this?

If more information is required, please let me know. Thanks in advance.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Sure, I've added two tables below.

Sales tracking: I didn't mention it in my original post, but I have two additional named ranges that my working macro copies from, Item_code and Quantity_sold.
A B C
1 Reference Item code Quantity
2 9 H18 36
3 4 H18 22
4 4 J23 5
Sales chart:
A B
1 Item code Quantity
2 H18 58
3 J23 5
 
Upvote 0
2 9 H18 36refcodequan
3 4 H18 229h1836
4 4 J23 54h1822Sum of quan
Sales chart:4j235codeTotal
A Bh1858
1 Item code Quantityj235
2 H18 58Grand Total63
3 J23 5
a simple pivot table
will get the quantities
for each code
these can be extracted to
a second table
that will drive the chart
(start with I5 and check if
I6 I7 I8 etc are blank or
equal to Grand Total

<colgroup><col span="8"><col><col><col span="2"></colgroup><tbody>
</tbody>
 
Upvote 0
Sorry, I don't know how to extract the data on to a dynamic named range.

I know you can use =GetPivotData to return the total of individual item sales, but I'm really struggling with coding a macro to automatically refer to new potential items.
 
Upvote 0
you don't use a macro to make a chart, it can be done straight from the pivot table or by lifting the data from the pivot table

OR IS MAKING A CHART FROM 2 TO SAY 99 ROWS ACCORDING TO THE DATA NOT YOUR REQUIREMENT ?

in this example the third row of data is Grand Total so you return na() as you do for 4th and subsequent which are blank.....
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top