Is it possible to simplify this code with a loop? x

katieexcel

New Member
Joined
Feb 20, 2013
Messages
4
Hiya,
I have some code that I think can be simlified, but I'm not sure how...
Code:
Sub graph()

Sheets("New QC").Select
Dim i As Variant
Dim rrange1 As Range
Dim rrange2 As Range
Dim rcol1 As Range
Dim rcol2 As Range
Set rrange1 = Range(("e8"), ("s8"))
rrange1.Select
With Selection
Set rrange1 = Selection(1).Resize(Selection.Rows.Count)
Set rcol1 = rrange1
For i = 2 To 16 Step 2
Set rcol1 = Application.Union(rcol1, rrange1(, i - 1))
Next
End With
Set rrange2 = Range(("e9"), ("s9"))
rrange2.Select
With Selection
Set rrange2 = Selection(1).Resize(Selection.Rows.Count)
Set rcol2 = rrange2
For i = 2 To 16 Step 2
Set rcol2 = Application.Union(rcol2, rrange2(, i - 1))
Next
End With
 

ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnStacked
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Values = rcol1
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Values = rcol2

End Sub

So I have defined two ranges, rcol1 and rcol2, and then I am plotting them.
rcol1 and rcol2 have the same columns but different rows (8 and 9 respectively). Is there a way of saying "rcol(j)" and "for j=0 to ..." and setting the row number in terms of j? Then plotting the series values in j?

xxxx
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
What are you actually trying to do with this line of code:

Code:
Set rcol1 = Application.Union(rcol1, rrange1(, i - 1))[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]</pre>
 
Upvote 0
What are you actually trying to do with this line of code:

Code:
Set rcol1 = Application.Union(rcol1, rrange1(, i - 1))



</PRE>

I'm selecting every second cell (column) in the range RC5 to RC19. The code is definitely correct for what I want to do (plot every second cell in a row). As I want to do this for 10 consequtive rows in total (the code I've posted is for only 2 rows), I'd like to simplify it rather than have 10 lots of similar code.
xx
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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