Unable to get named range in Excel - vba

I have a worksheet where there is a chart on the first sheet, showing some data from a named range.
The named range looks like this:
=OFFSET(chart_data!$B$2,0,0,COUNTA(chart_data!$B:$B)-1)
where chart_data is a different sheet.
I also have a VBA script that is supposed to set colors of the chart same as background colors of corresponding cells. The script follows:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cht As ChartObject
Dim i As Integer
Dim vntValues As Variant
Dim s As String
Dim myseries As Series
Dim nRange As Range
For Each cht In ActiveSheet.ChartObjects
For Each myseries In cht.Chart.SeriesCollection
If myseries.ChartType <> xlPie Then GoTo SkipNotPie
s = Split(myseries.Formula, ",")(2)
vntValues = myseries.Values
For i = 1 To UBound(vntValues)
If Range(s).Cells(i).Interior.Color <> 16777215 Then
myseries.Points(i).Interior.Color = Range(s).Cells(i).Interior.Color
End If
Next i
SkipNotPie:
Next myseries
Next cht
End Sub
My problem is that when I try to evaluate Range(s), where s = "report!values_list", I get
Run-time error '1004':
Method 'Range' of object '_Worksheet' failed
How can I solve this?

Try to do it on different Office. It works for me, some of Offices blocked the dynamic range. Try to make it on different PC with different type of MS Office and make them some simply task with dynamic range. If it will work, you just copy your code there and it will work even in your sheet.

You can get the range corresponding to a name like this:
ThisWorkbook.Names ("somename").RefersToRange

Related

activate method of worksheet class failed - vlookup - vba

I want to use VLOOKUP command and use a range which is in sheet B (not in the activated one A). Calling the new worksheet gives me an error of " 'runtime error 1004' activate method of worksheet class failed"
Public Sub Creation()
Worksheets("A").Activate
Randomize
Dim code As String
Dim hamid As Variant
Dim Lookup_Range As Range
Code = 100032
Set Lookup_Range = Worksheets("B").Range("O1:P8")
On Error Resume Next
hamid = Application.WorksheetFunction.VLookup(code, Lookup_Range, 2, False)
On Error GoTo 0
End sub
I have tried using With command to call the new worksheet but I was not successful. I am new to VBA so please bear with me.
Your lookup range seems to be in worksheet B. Try activating worksheet B before using the lookup function. I've encountered issues trying to define ranges on one worksheet while having another activated:
Public Sub Creation()
Worksheets("A").Activate
Randomize
Dim code As String
Dim hamid As Variant
Dim Lookup_Range As Range
code = 100032
'Try here:
Worksheets("B").Activate
Set Lookup_Range = Worksheets("B").Range("O1:P8")
On Error Resume Next
'or here:
Worksheets("B").Activate
hamid = Application.WorksheetFunction.VLookup(code, Lookup_Range, 2, False)
On Error GoTo 0
'also made 'Code' in lookup function 'code'.
End Sub

Select chart object based on position in sheet (VBA)

Is there a possibility to address a chart object in an excel sheet via its exact position? Google didn't help so far so any input is appreciated.
Best regards,
Stefan
You can use TopLeftCell and BottomRightCell to find a chart between given range.
Sub testChart()
Dim oChart As Chart
'/ Selecet the chart , if found in Range D4:F10
Set oChart = findChart(Sheet1.Range("D4:F10"))
If Not oChart Is Nothing Then
oChart.Parent.Select
Else
MsgBox "No chart found."
End If
End Sub
Function findChart(ByVal rngChart As Range) As Chart
Dim oChart As Chart
Dim oChartObj As ChartObject
Dim wksChart As Worksheet
Dim rngCArea As Range
Set wksChart = rngChart.Parent
For Each oChartObj In wksChart.ChartObjects
Set rngCArea = Intersect(wksChart.Range(rngChart.Address), wksChart.Range(oChartObj.TopLeftCell, oChartObj.BottomRightCell))
If Not rngCArea Is Nothing Then
Set oChart = oChartObj.Chart
End If
Next
Set findChart = oChart
End Function

VBA Excel 2010 - Assign unknown chart to varible

Im trying to set a chart to a varible, however since I have multiple charts in my spreadsheet I need to be able to activate a chart and assign it to a varible.
Ffor the record ChartHandel = ActiveSheet.ChartObject(1) doesn't work,I have also tried .Shape(1) and Chart("Name of chart") and these too dont work
Dim ChartHandel2 As Chart
ActiveSheet.ChartObjects(1).Activate
ChartHandel2 = ActiveChart
Even this gets error '91, Object Variable or with block varible not set' which it looks like it should work and I was sure I had had this working at one point(as a workaround)
My question is basicly can you assign a chart to a varible if it isnt active (and if possible how)?
when assigning object you must use the Set keyword in the left part of the assignment statement
furthemore Chart object is a member of the ChartObject object
here's a small example of dealing with them
Option Explicit
Sub ChartObjects()
Dim chartObj As ChartObject
With ThisWorkbook.Worksheets("charts")
For Each chartObj In .ChartObjects
With chartObj ' to deal with current "ChartObject" object
With .Chart ' to deal with "Chart" object of "ChartObject" object
.ChartType = xlXYScatter ' or another XlChartType Enumeration (https://msdn.microsoft.com/en-us/library/office/ff838409.aspx)
MsgBox .ChartArea.Name
.HasLegend = False
.ChartTitle.Caption = "chart title you need"
End With
End With
Next chartObj
End With
End Sub
You can use for each to loop through charts if you arent sure with indexes. Something like this
Sub testChart()
Dim testSheet As Worksheet
Set testSheet = Sheets("Sheet1")
Dim myChart As ChartObject
Dim chartVariable As Chart
With testSheet
For Each myChart In .ChartObjects
Set chartVariable = myChart.Chart
Next myChart
End With
End Sub
warning: its only demo version, it will need further changes depends on what you need

Excel VBA to fill pie chart colors from cells with conditional formatting

I don't have experience with VBA and I'm trying to format all of the pie charts on one active sheet based on the colors of their data cells in Excel 2010. I found this code from: http://datapigtechnologies.com/blog/index.php/color-pie-chart-slices-to-match-their-source-cells/
Sub ColorPies()
Dim cht As ChartObject
Dim i As Integer
Dim vntValues As Variant
Dim s As String
Dim myseries As Series
For Each cht In ActiveSheet.ChartObjects
For Each myseries In cht.Chart.SeriesCollection
If myseries.ChartType <> xlPie Then GoTo SkipNotPie
s = Split(myseries.Formula, ",")(2)
vntValues = myseries.Values
For i = 1 To UBound(vntValues)
myseries.Points(i).Interior.Color = Range(s).Cells(i).Interior.Color
Next i
SkipNotPie:
Next myseries
Next cht
End Sub
This code works well, however it is unable to pick up the colors from conditional formatting.
I came across this solution for VBA to read conditional formatting colors:
Selection.FormatConditions(1).BarColor.Color
However I've been unable to implement it in the above block of VBA. I tried replacing Interior.Color with different parts of it and none seem to work. Does anyone know a simple way to do this?
Thank you in advance!
Since you have 2010, you can use the DisplayFormat property:
For i = 1 To UBound(vntValues)
myseries.Points(i).Interior.Color = Range(s).Cells(i).DisplayFormat.Interior.Color
Next i

VBA looping through all series within all charts

I'm having an issue with the looping through of several charts in my VBA code. I'm 99.7% sure that this is a really easy and quick fix but my brain isn't working today.
I want the code to loop through every chart on the ActiveSheet, and for every data series that the chart contains I want it to add the last value of the series. In my example I have 9 charts, each with 3 series in them (bound to change, some have 2 but I digress).
I have the following code
Sub AddLastValue()
Dim myChartObject As ChartObject
Dim myChart As Chart
Dim mySrs As Series
Dim myPts As Points
With ActiveSheet
For Each myChartObject In .ChartObjects
For Each myChart In .Chart
For Each mySrs In .SeriesCollection
Set myPts = .Points
myPts(myPts.Count).ApplyDataLabels Type:=xlShowValue
Next
Next
Next
End With
End Sub
If I remove the looping code and just do a
Set myPts = ActiveSheet.ChartObjects(1).Chart. _
SeriesCollection(1).Points
myPts(myPts.Count).ApplyDataLabels type:=xlShowValue
Then it works for that specific chart and series, so I'm positive it is the looping that I'm messing up.
Could someone tell me where I mess up the looping code?
Try following code:
Sub AddLastValue()
Dim myChartObject As ChartObject
Dim mySrs As Series
Dim myPts As Points
With ActiveSheet
For Each myChartObject In .ChartObjects
For Each mySrs In myChartObject.Chart.SeriesCollection
Set myPts = mySrs.Points
myPts(myPts.Count).ApplyDataLabels Type:=xlShowValue
Next
Next
End With
End Sub
Not work for empty values.
This code find last not empty value and then adds label.
For Each mySrs In myChartObject.Chart.SeriesCollection
Set myPts = mySrs.Points
Dim i As Integer
i = myPts.Count
Do Until i < 2 Or mySrs.Values(i) <> ""
i = i - 1
Loop
myPts(i).ApplyDataLabels Type:=xlShowValue
Next