How to get range assigned to Named Range in excel vba? - excel

I have already defined the named ranges in my workbook. I want to use the range for placing my pie chart. I am trying to write a code which sets range to variable and move the chart to the specific location.
Dim Rng As Range
Dim ChtObj As ChartObject
Set Rng = ThisWorkbook.Name("BT_GATE1").RefersTo
Set ChtObj = ActiveChart.Parent
ChtObj.Top = Rng.Top
I think I am missing something or using a worng method. Can some one help me assigning a range to variable 'Rng'?

A named range is either one cell or a collection of more cells which have been given a name. Ultimately it is a range. So in your case you can directly set it to a range as shown below
Dim Rng As Range
Set Rng = Range("BT_GATE1")
Debug.Print Rng.Address
Debug.Print Rng.Top
Debug.Print Rng.Parent.Name

Related

How to change the source data for a chart in a chart sheet Excel vba

all together,
I am trying to create a regional map chart that shows data per country. The data is provided by a dynamic table.
I created a chart sheet and inserted the regional map chart into it.
The chart now shows the data for the current countries, but I can't change the data.
The string mapInput is correct and also shows the right Range.
For example: "A4293:A4295,BJ4293:BJ4295"
The error 1004 comes up in the last line.
I also tried without "Source:="
And also without "chart"
l = Split(ws.Cells(lastRowR, lastColR - 2).Address, "$")(1)
mapInput = "A" & lastRowR - 1 - cCount & ":A" & lastRowR - 1 & "," & l &
lastRowR - 1 - cCount & ":" & l & lastRowR - 1
Sheets("map").ChartObjects(1).Chart.SetSourceData Source:=ws.Range(mapInput)
Runtime Error 1004 “Application-defined or Object-defined error”
I didn't solve your exact issue, but I set up a working example. If you correctly specify your chart and your data range, it should work for you.
I have two ranges on my sheet, one named MapInputNE and the other MapInputSW. I have two "Filled Mpa" type charts, one embedded in the worksheet, the other moved to a chart sheet. The maps initially draw their data from MapInputNE.
I wrote a general routine to change the data of a chart, regardless of where it is. Feed it the chart and the data range, and it updates the chart, and also adjusts the projection and mapping level.
Sub UpdateChartData(cht As Chart, rng As Range)
With cht
.SetSourceData Source:=rng
With .SeriesCollection(1)
.GeoProjectionType = xlGeoProjectionTypeMercator
.GeoMappingLevel = xlGeoMappingLevelDataOnly
End With
End With
End Sub
Then I made some specific routines to change either the embedded map or the chart sheet map to use either the New England or Southwest data:
Sub UpdateEmbeddedChartDataNE()
Dim cht As Chart
Set cht = Worksheets("Sheet1").ChartObjects(1).Chart
Dim rng As Range
Set rng = Worksheets("Sheet1").Range("MapInputNE")
UpdateChartData cht, rng
End Sub
Sub UpdateEmbeddedChartDataSW()
Dim cht As Chart
Set cht = Worksheets("Sheet1").ChartObjects(1).Chart
Dim rng As Range
Set rng = Worksheets("Sheet1").Range("MapInputSW")
UpdateChartData cht, rng
End Sub
Sub UpdateChartSheetDataNE()
Dim cht As Chart
Set cht = Charts(1)
Dim rng As Range
Set rng = Worksheets("Sheet1").Range("MapInputNE")
UpdateChartData cht, rng
End Sub
Sub UpdateChartSheetDataSW()
Dim cht As Chart
Set cht = Charts(1)
Dim rng As Range
Set rng = Worksheets("Sheet1").Range("MapInputSW")
UpdateChartData cht, rng
End Sub
Each of these routines defines the chart and the data range, and feeds them into the first routine, and the corresponding chart chart switches to the indicated data source.
Works well with both maps, though I've only shown the embedded one here.
If you are using Chart Sheet, use following code to set data source:
Dim Cht As Chart
Set Cht = Charts("map")
Cht.SetSourceData ws.Range(mapInput)
Make sure mapInput represent correct data range
Note: Chart Sheet does not contains ChartsObject collection

how to find a value in a range in worksheet code

I need to find a value that the user inputs in one cell, in a range of values in the same workbook. In the worksheet, the user selects a value and then I need to write a code that finds that value in the range or delivers an error message. I was directed to put this in the worksheet code.
I tried specifying the worksheet of the criteria value but that hasn't seemed to make a difference as I am still getting the same error.
Range("Dcust").Offset(2, 0).End(xlDown).End(xlToRight).ClearContents
Dim shuttleNum As Range
Set shuttleNum = Range("Dsched").Offset(2, 0).End(xlDown).End(xlToRight)
Set driverSheet = ActiveWorkbook.Sheets("Driver")
Dim DSnumView As Integer
DSnumView = driverSheet.Range("DSnumView").Value
'''''Here is where I get the error'''
If shuttleNum.Columns(2).Find(DSnumView, lookat:=xlWhole) Is Nothing Then
MsgBox "You are not scheduled to drive this shuttle", vbCritical, "Error"
Exit Sub
Else
Dim ctable As Range
Set ctable = Range("CTableStart").Offset(1,0).End(xlDown).Range("CTableStart").Offset(0, 3)
End If
This line of code:
Range("Dsched").Offset(2, 0).End(xlDown).End(xlToRight)
returns a single cell.
So basically shuttleNum is set to a single cell and consequently this: shuttleNum.Columns(2) will give you an error because there is only one column in this range.
If I understand correctly what you're trying to do, the folowing should help.
Let's assume you have some data which starts from cell A1 but you don't know where it ends. For example:
To get the range that contains the data, which in this case is A1:C5, you would need to do the following:
Dim sht As Worksheet
Dim rng As Range
Set sht = ThisWorkbook.Worksheets("Worksheet Name")
Set rng = sht.Range("A1")
Set rng = sht.Range(rng, rng.End(xlToRight)) 'A1:C1
Set rng = sht.Range(rng, rng.End(xlDown)) 'A1:C5
Alternatively you can merge the last two steps into one:
Set rng = sht.Range(rng, rng.End(xlToRight).End(xlDown))

Transposing named range from table, into another named range

I have this issue with taking named range in Worksheet "Team overview" (which is setup to table named "tbl", column no. 2), and then pasting it transposed into another named range in another worksheet called "Knowledge matrix" (could be solved by directing it to absolute reference, but in case I change number of columns in tables/worksheet I would need to update code in VBA).
I've tried using loop through array and only switching the x,y, coordinates, but the struggle I have is with copying that selection to another named range and assigning the source named range to variable rng (which returns run-time error 5 - Invalid procedure).
The code worked for testing file without named ranges and tables with just absolute references.
Sub Transpose()
Dim rng As Range
Dim rngT As Range
Dim oarray As Variant
Dim cl As Long
Dim rw As Long
Set rng = ThisWorkbook.Sheets("Team overview").Range("Name")
Set rngT = ThisWorkbook.Sheets("Knowledge Matrix").Range("Target")
oarray = rng
For cl = 1 To UBound(oarray)
For rw = 1 To UBound(oarray, 2)
Cells(rw, cl) = oarray(cl, rw)
Next
Next
End Sub
The whole picture:
Table "tbl" is for a clear overview of team members. When the team lead adds/deletes one, I need it to be updated in all other table headers. Since headers cannot have formulas in them, I was thinking of copying the column in transposed way into named range for the other table headers.
There is also slight complication of condition the member being "Active" or "Butterfly", but that will be just cherry on the top.
So basically:
If column 1 has "Active" or "Butterfly" copy table/named range (Vertically spaced)
Transpose that array and paste it into another named array (Horizontally spaced)
I would prefer not to use .Select if possible.
Source table with named range
Target table with named range
There is already a method called Transpose.
Sub MyTranspose()
Dim rng As Range
Dim rngT As Range
Dim oarray As Variant
Set rng = ThisWorkbook.Sheets("Team overview").Range("Name")
Set rngT = ThisWorkbook.Sheets("Knowledge Matrix").Range("Target")
oarray = rng.value2
rngT.resize(ubound(oarray, 2), ubound(oarray, 1)) = _
application.transpose(oarray)
End Sub

Range Empty for Dynamic Chart VBA

I'm trying to create a dynamic sourcedata on a chart in VBA as this can vary depending on the data being pulled in via a macro. So far I have:
Set Rng = Sheets("Mapping Tables").Range("J13", Sheets("Mapping Tables").Range("J13").End(xlDown).End(xlToRight)).Select
Worksheets("Vintage").Charts("Vintage_1").SetSourceData Rng, PlotBy:=xlColumns
However, when I step through the code in the set range command, my cells are being selected. When I run through the SetSourceData step nothing happens. When I hover over the Rng variable it says = nothing.
I've never really done dynamic charts before but I cannot understand why my range equals nothing when my range is being selected.
Thanks in advance.
Try this.
Sub test()
Dim Rng As Range
Dim obj As ChartObject
Set Rng = Sheets("Mapping Tables").Range("J13", Sheets("Mapping Tables").Range("J13").End(xlDown).End(xlToRight))
Set obj = Worksheets("Vintage").ChartObjects("Vintage_1")
With obj.Chart
.SetSourceData Rng, PlotBy:=xlColumns
End With
End Sub
actually the first line should give you an error, if I'm not mistaken. You cannot set a range to a ".select" statement.
Try deleting the ".select" at the end
Edit: when you're defining a continuous range (e.g. a table), you can use something cleaner:
set Rng = Sheets("Mapping Tables").Range("J13").CurrentRegion

Define named range from databodyrange

I want to define a named range from a table's databodyrange.
I manage to get the address of the databodyrange and can select this range
Dim acSh As Worksheet
Dim oLo As ListObject
Sub StelDatabereikIn()
Set acSh = ActiveSheet
For Each oLo In acSh.ListObjects
Application.Goto oLo.Range
MsgBox "Databodyrange is " & oLo.DataBodyRange.Address
oLo.DataBodyRange.Select
I would like to define a named range based on the databodyrange.
Not sure why, since it already is a named range, but you can use:
oLo.DataBodyRange.Name = "whatever"

Resources