Help to write range(cells(),cells())

Akbarov

Active Member
Joined
Jun 30, 2018
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hello I am trying to update chart source data automatically.

I need to use something like : = sh.Range(sh.Cells(3, 6 + x).Address, sh.Cells(LR, lc).Address) to make row and column dynamic.

How can I write this '='Sheet1'!$E$2:$E$57,'Sheet1'!$P$2:$Y$57'

Thanks in advance.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
How can I write this '='Sheet1'!$E$2:$E$57,'Sheet1'!$P$2:$Y$57'
In rng1 you have the first part and in rng2 the second part.

Try this:
VBA Code:
Sub testrng()
  Dim sh As Worksheet
  Dim lr As Long, lc As Long
  Dim rng1 As String, rng2 As String
  
  Set sh = Sheets("Sheet1")
  
  lr = sh.Range("E" & Rows.Count).End(3).Row
  lc = sh.Cells(2, Columns.Count).End(1).Column
  rng1 = sh.Name & "!" & sh.Range("$E$2:$E$" & lr).Address
  rng2 = sh.Name & "!" & sh.Range(sh.Cells(2, lc), sh.Cells(lr, lc)).Address
  
  ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
  ActiveChart.SetSourceData Source:=Range(rng1 & "," & rng2)
End Sub
 
Upvote 0
Thank you for your time Danta, I tryed your code. But at last line I get error Method range of object _Global failed
 
Upvote 0
I am only giving an example of how to obtain range1 and range2. And I applied it in the creation of a graph.

But you're not really putting how you have your data in the sheet or what you want to do.

Give an example using XL2BB to of what you have on the sheet and explain what you want to do.
 
Upvote 0
I made some changes to use it in my sheet. Here it is:
VBA Code:
Sub testrng()
  Dim sh As Worksheet
  Dim lr As Long, lc As Long
  Dim rng1 As String, rng2 As String
 
  Set sh = ActiveSheet
 
  'lr = sh.Range("E" & Rows.Count).End(3).Row
  'lc = sh.Cells(2, Columns.Count).End(1).Column
  lr = 57
  lc = 24
  rng1 = sh.Name & "!" & sh.Range("$E$2:$E$" & lr).Address
  rng2 = sh.Name & "!" & sh.Range(sh.Cells(2, 16), sh.Cells(lr, lc)).Address
 
  sh.ChartObjects("Cluster" & 2).Activate
  ActiveChart.SetSourceData Source:=Range(rng1 & "," & rng2), PlotBy:=xlColumns
End Sub

I also changed this part: sh.Range(sh.Cells(2, 16) thought that was a typo.
Some rows are hidden, can that cause any kind of problem?
 
Upvote 0
sh.ChartObjects("Cluster" & 2).Activate
That line doesn't work for me.

But if I put the following it works with your code:
VBA Code:
sh.Shapes.AddChart2(201, xlColumnClustered).Select

What error do you get?
In which line do you have the error?

Again, you can put a sample of your data.
 
Upvote 0
I get error in last line: ActiveChart.SetSourceData Source:=Range(rng1 & "," & rng2), PlotBy:=xlColumns
error: Method range of object _Global failed
 
Upvote 0
Again, you can put a sample of your data.
I can't reproduce your problem because you don't want to share an example of your data.

I just can't go further, the tests with my data and with the type of graph that I use for my example work.

Another option is to share your file on google drive.

You could upload a copy of your file to a free site such google drive. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Try this

Rich (BB code):
Sub testrng_v2()
  Dim sh As Worksheet
  Dim lr As Long, lc As Long
  Dim rng1 As Range, rng2 As Range
 
  Set sh = ActiveSheet
 
  'lr = sh.Range("E" & Rows.Count).End(3).Row
  'lc = sh.Cells(2, Columns.Count).End(1).Column
  lr = 57
  lc = 24
  Set rng1 = sh.Range("$E$2:$E$" & lr)
  Set rng2 = sh.Range(sh.Cells(2, 16), sh.Cells(lr, lc))
 
  sh.ChartObjects("Cluster" & 2).Activate
  ActiveChart.SetSourceData Source:=Range(rng1, rng2), PlotBy:=xlColumns
End Sub
 
Upvote 1

Forum statistics

Threads
1,215,011
Messages
6,122,680
Members
449,091
Latest member
peppernaut

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