Range plus chart

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I'd like to do 3 simple things with VBA, which I haven't done before.

i) I'd like to select a range, but ignore the last row, when selected.

So if I had the values a, b, and c in cells A1, A2, and A3, and the values 1, 2, 3, and 4 in cells B1, B2, B3 and B4, I'd like to select the range A1:B3, instead of A1:B4.

Please find below sample data:

[TABLE="width: 128"]
<tbody>[TR]
[TD="width: 64"]a[/TD]
[TD="width: 64, align: right"]1[/TD]
[/TR]
[TR]
[TD]b[/TD]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD]c[/TD]
[TD="align: right"]3[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]4[/TD]
[/TR]
</tbody>[/TABLE]
I tried this code, but it didn't work?

Code:
Sub ABC()

Sheet1.Activate
Range("A1", Range("B1").End(xlDown)).Offset(-1, 0).Select

End Sub

ii) I'd like to create a chart based on the data above (I recorded a macro - see below - but recorded macros only use a fixed range):

Code:
Sub Chart()'
' ChartMacro 

    Range("A1:B5").Select
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    ActiveChart.SetSourceData Source:=Range("'count tab'!$A$1:$B$5")
End Sub

iii) I'd like to cut the newly created chart and paste it into cell M5 of Sheet 2.

Has anyone done this before?

If so, your help would be greatly appreciated.

TIA
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Code:
Sub ABC()
    Dim lr&
    lr = Cells(Rows.Count, 2).End(3).Row
    Range("A1:B" & lr - 1).Select
End Sub
 
Upvote 0
Great - that worked! Thanks Paul Ked .

Do you know how to do the other parts?

i.e create a chart with the data in the example and then move that chart into a different sheet? eg cell M5 of Sheet 2?

Thanks in advance.
 
Upvote 0
Actually, I managed to work out how to create the chart, using your code!

So thanks for that! I just need to work out how to move it to a new tab now.
 
Upvote 0
I also managed to do the last bit by modifying this

Code:
[COLOR=#000000][FONT=verdana]ActiveSheet.ChartObjects(1).Cut[/FONT][/COLOR]
[COLOR=#000000][FONT=verdana]Sheets("Sheet2").Select[/FONT][/COLOR]
[COLOR=#000000][FONT=verdana]Range("B5").Select[/FONT][/COLOR]
[COLOR=#000000][FONT=verdana]ActiveSheet.Paste[/FONT][/COLOR]
 
Upvote 0
Hi, sorry for lack of response but have been on other duties!

Glad you've got it sorted.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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