0

I am trying to store a named range like that

    Dim sValue As String
sValue = ThisWorkbook.Names("MyRange").Value

I got the range address instead of the values in the named range. What I am trying to do exactly is the following Suppose the named range is "MyRange" and it is for A1:C4 In a cell, if we typed equal sign and typed "MyRange" and then pressed F9, we got the values as array like that

{"Head1","Head2","Head3";1,2,3;4,5,6;7,8,9}

How to store that in variable string?

4
  • Dim sValue As Variant Commented Apr 12, 2020 at 9:23
  • Thanks a lot. Using Variant is the same as the result is the address of the range not the values .. I need to store the same output that appears after pressing F9 in the worksheet formula and I have posted it above. Commented Apr 12, 2020 at 9:25
  • 1
    sValue = ThisWorkbook.Names("MyRange").RefersToRange.Value Commented Apr 12, 2020 at 9:30
  • that will give you back an array with named range values Commented Apr 12, 2020 at 9:31

2 Answers 2

3

to have a String variable formatted as {"Head1","Head2","Head3";1,2,3;4,5,6;7,8,9}, you can do as follows

    Dim sValue As String, r As Range
    For Each r In ThisWorkbook.Names("MyRange").RefersToRange.Rows
        sValue = sValue & Join(Application.Transpose(Application.Transpose(r.Value)), ",") & ";"
    Next
    sValue = "{" & Left(sValue, Len(sValue) - 1) & "}"

while to store a named range in an array is much simpler (and I'd say, easy to use):

Dim sValue As Variant
sValue = ThisWorkbook.Names("MyRange").RefersToRange.Value
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for great help. At the end of your code I tried such a line a = Evaluate(sValue) but I got Error 2015 for the value of a in the Locals Window. Why?
You are welcome. I'm not an Evaluate() expert, but I'd say it would "eat" formulas, not arrays
0

I think you are looking for

sValue = ThisWorkbook.Names("MyRange"),name

to find all names on Activesheet:


Sub test_names()
    Dim wsName As String
    wsName = ActiveSheet.Name
    Dim nameRange As Variant
    For Each nameRange In ThisWorkbook.Names
        Set rngName = Range(nameRange)
        wsParentName = rngName.Parent.CodeName
If (wsParentName = wsName) Then
Debug.Print "Found range " & nameRange.Name
End If
    Next nameRange
End Sub

1 Comment

Thanks a lot but this is irrelevant to the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.