0

I'm trying to create few arrays from named range in my workbook instead of defining that arrays manually.

Example: This is the array I want to create

Array("bardzo lekka", "lekka", "średnia", "ciężka") The same data is definied in named range $A$5:$A$8. I want to load that range into array like above.

I've tried to do this like that:

kategoria_a = ActiveWorkbook.Names("kategoria_agronomiczna_gleby").RefersToRange(1, 1)

It creates only one object array.

kategoria_a = ActiveWorkbook.Names("kategoria_agronomiczna_gleby")

That makes

kategoria_a = ='Dane wyjściowe'!$A$5:$A$8

Thanks in advance.

4
  • 1
    Is your question, how to load all a workbook's named ranges into an array? Commented Apr 3, 2018 at 15:06
  • Please show an example with expected output Commented Apr 3, 2018 at 15:10
  • Your manually-created array contains strings, not named ranges. Your question is unclear. Commented Apr 3, 2018 at 15:44
  • I want to load array kategoria_a from named value. It's definied in range $A$5:$A$8. Sorry if I made unclear question. Commented Apr 3, 2018 at 18:42

4 Answers 4

1

if your values are listed in along a column you could use:

kategoria_a = Application.Transpose(ActiveWorkbook.Names("kategoria_agronomiczna_gleby").RefersToRange.Value)

while if they are listed along a row then you could use

kategoria_a = Application.Transpose(Application.Transpose(ActiveWorkbook.Names("kategoria_agronomiczna_gleby").RefersToRange.Value))

to obtain a 1D array

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I intended to do. Than you.
Prosze bardzo :-)
1

Try this:

Dim a As Variant
Dim i As Long
Dim rng As Range, r As Range

a = Array("bardzo lekka", "lekka", "średnia", "ciężka")

For i = LBound(a) to UBound(a)
    If i = LBound(a) Then
        Set rng = Range(a(i))
    Else
        Set rng = Union(rng, a(i))
    End If
Next i

a contains all named ranges. To access the values you could use this:

For Each r In rng
    debug.print r.value
Next r

1 Comment

IMO this is most likely what OP is asking about - although, there's no way to be sure until they clarify their question.
0

This will take all the named ranges, and put them in an array (myArr):

Sub get_named_ranges()
Dim rng As Range
Dim nm
Dim myArr() As Variant

ReDim myArr(1 To ThisWorkbook.Names.Count)

Dim i As Long
i = 1
For Each nm In ThisWorkbook.Names
    myArr(i) = nm.Name
    i = i + 1
Next nm

For i = LBound(myArr) To UBound(myArr)
    Debug.Print (myArr(i))
Next i

End Sub

Comments

0

If you mean you want to set an array equal to the values in a named range,instead of entering them manually (like your example), then just set the array equal to the range. As long as "kategoria_agronomiczna_gleby" is your named range.

kategoria_a = ActiveWorkbook.Range("kategoria_agronomiczna_gleby")

You can use this loop to check the values in your array

 For i = LBound(kategoria_a, 1) To UBound(kategoria_a, 1)
    Debug.Print kategoria_a(i, 1)
  Next

Comments

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.