Using variables to place the chart on the spreadsheet

bmajor

New Member
Joined
Feb 2, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

I draw multiple charts on a spreadsheet.

for placing them I use this code: Set rng: Activesheet.Range(A100:N125) that draws me a 25-row high chart to A 100 to N125, working fine

when not all the charts are required, I want to be able to select them and place them automatically in the next empty place.

I created a checkbox, where users can select which chart to display, and the related excel sheet calculates the A and N positions of the chart.
I created variables and made this code:

Set rng: Activesheet.Range("ChartFirstRow:ChartLastRow")

apparently, the variables are working because with SHIFT+F9 I can see the right value from the table, yet VN gives 1004 run-time error
Application-defined or object-defined error.

the chart is drawn, but with the wrong size and wrong location...


any idea how to fix it?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Sorry, but with such level of details you should ask on thesorcer.com forum...
In general you can set the position of a chart using, for example:
VBA Code:
With Sheets("SheetName")
    .ChartObjects(1).Top = .Range("H10").Top
    .ChartObjects(1).Left = .Range("H10").Left
End With
Try adapting the concept to your situation, or please give us some "additional" information on your case

Bye
 
Upvote 0
If those are variables, it should be:

Code:
Set rng = Activesheet.Range(ChartFirstRow & ":" & ChartLastRow)
 
Upvote 0
Thanks,

Roy, that didn't work.

I found a workaround.


I have ChartFirstRow as a sting variable still, for the top left corner.

then I set the width and height of the chart, so the next chart could be placed properly after that.

And apparently, VBA accepts single cell reference as a variable so this one
VBA Code:
Set rng=ActiveSeet.Range(ChartFirstRow)
seems working.

The full code for the chart positioning is like this now:

VBA Code:
 Dim rng As Range
    Set cht = ActiveChart.Parent
    
Set rng = ActiveSheet.Range(ChartFirstRow)  ' I tried different ways to bring here both variables without success. Breaking the address to a column letter "A" & CahartFirstRow: "N" & ChartLastRow and any permutation of those were unsuccessful


    cht.Left = rng.Left
    cht.Width = 1080
    cht.Top = rng.Top
    cht.Height = 345
       
    ActiveChart.Legend.Select
    Selection.Position = xlBottom
    ActiveChart.HasLegend = True
    
    ActiveSheet.Select
    
    Range("A1").Select


And the original one is this

VBA Code:
 Dim rng As Range
    Set cht = ActiveChart.Parent
    Set rng = ActiveSheet.Range("E2:Q30")   this part 
    cht.Left = rng.Left
    cht.Width = rng.Width
    cht.Top = rng.Top
    cht.Height = rng.Height
       
    ActiveChart.Legend.Select
    Selection.Position = xlBottom
    ActiveChart.HasLegend = True
    
    ActiveSheet.Select
    
    Range("A1").Select
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,454
Members
448,898
Latest member
drewmorgan128

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