438 error, when trying to set a variable to a cell on another sheet

Boogerbut74

New Member
Joined
Oct 17, 2022
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Right now im trying to set the variable rng as cell H39 (H39 will be populated with a capital letter of a column) in my reference sheet and then have rng defing the range in the variable Graph Range.


VBA Code:
Sub Test_7()
   
    Dim lastrow As Integer
    Dim GraphRange As Range
    Dim rng As Range
   
    Worksheets("Reference").rng = Range("H39")
   
   
   
    lastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 10).End(xlUp).Row
   
    'set chart data range (including series names)
    Set GraphRange = ActiveSheet.Range("D10:" & rng & lastrow)
   
    'Create a chart
    Set cht = ActiveSheet.ChartObjects.Add( _
    Left:=ActiveCell.Left, _
    Width:=450, _
    Top:=ActiveCell.Top, _
    Height:=250)
   
    'Give chart some data
    cht.Chart.SetSourceData Source:=GraphRange

   
   
    'Determine the chart type
    cht.Chart.ChartType = xlXYScatterSmooth
    'edits chart detailes
    cht.Chart.Axes(xlCategory).MinimumScale = 0
    cht.Chart.Axes(xlCategory).MaximumScale = 100
   
    cht.Chart.Axes(xlValue).MinimumScale = 115
    cht.Chart.Axes(xlValue).MaximumScale = 140
   
    cht.Chart.Legend.Position = xlLegendPositionBottom
 
    cht.Select
ActiveChart.FullSeriesCollection(1).Delete
   
   
End Sub

Sub Test_6()
   
    Dim lastrow As Integer
    Dim GraphRange As Range
   
    lastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 10).End(xlUp).Row
   
    'set chart data range (including series names)
    Set GraphRange = ActiveSheet.Range("D10:T" & lastrow)
   
    'Create a chart
    Set cht = ActiveSheet.ChartObjects.Add( _
    Left:=ActiveCell.Left, _
    Width:=450, _
    Top:=ActiveCell.Top, _
    Height:=250)
   
    'Give chart some data
    cht.Chart.SetSourceData Source:=GraphRange
   
    'Determine the chart type
    cht.Chart.ChartType = xlXYScatterSmooth
    'edits chart detailes
    cht.Chart.Axes(xlCategory).MinimumScale = 0
    cht.Chart.Axes(xlCategory).MaximumScale = 100
   
    cht.Chart.Axes(xlValue).MinimumScale = 115
    cht.Chart.Axes(xlValue).MaximumScale = 140
   
    cht.Chart.Legend.Position = xlLegendPositionBottom
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Declare your variables.

VBA Code:
Dim rng as Range

Remember to use the Set statement when assigning a range variable.

Worksheets("Reference").rng = Range("H39")

VBA Code:
Set rng = Range("H39")
 
Upvote 0
the range is only for a specific cell in another sheet when using your code it reference the active sheet
 
Upvote 0
...when using your code it reference the active sheet

VBA Code:
Worksheets("Reference").rng = Range("H39")

The line above is your code. The right side (Worksheets("Reference").rng) is a gross syntax error. You cannot reference a range variable that way. The left side of the equation (Range("H39")) references cell H39 on the active sheet. Your code references the active sheet, so my response to you also referenced the active sheet. If you want something different, you have to explain what you mean. If cell H39 is intended to be on another sheet then you have to fully qualify the reference. Something like this:

VBA Code:
Set rng = ActiveWorkbook.Worksheets("Reference").Range("H39")
 
Upvote 0
Solution

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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