VBA Named range in Array of worksheets

Katy Jordan

Well-known Member
Joined
Jun 28, 2008
Messages
596
Hi, can a named range be used inside an array?

So

Worksheets(Wary(a))Array("NamedRange")
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You could loop through each cell in the named range adding it to the array - not sure if you can add it all in one though.

D
 
Upvote 0
In sheet3 i have a list of worksheets which i have named "rngsheets", the sheets in this range are "Test", "Test2"

I dont want to hardcode the sheets in Array(I want to input named range)

Is this possible?
 
Upvote 0
Something like..

Code:
Dim wksAry, i As Long

wksAry = Range("NamedRange")

For i = 1 To UBound(wksAry, 1)
    With Worksheets(CStr(wksAry(i, 1)))
        'do something
    End With
Next
 
Upvote 0
Something like..

Code:
Dim wksAry, i As Long

wksAry = Range("NamedRange")

For i = 1 To UBound(wksAry, 1)
    With Worksheets(CStr(wksAry(i, 1)))
        'do something
    End With
Next

How would i use your method with the below?

WSary = Array("Sheet1", "Sheet2")


Code:
With ShtRaw
  LR = .Cells(Rows.Count, 1).End(xlUp).Row
  .AutoFilterMode = False
  With .Range("A5:O" & LR)
    For a = LBound(WSary) To UBound(WSary)
      .AutoFilter Field:=2, Criteria1:=WSary(a)
      NR = Worksheets(WSary(a)).Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      'Copy Data to destination worksheets
      On Error Resume Next
      .Columns("A:C").Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).Copy
      Worksheets(WSary(a)).Range("A" & NR).PasteSpecial Paste:=xlPasteValues
      .Columns("F:O").Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).Copy
      Worksheets(WSary(a)).Range("D" & NR).PasteSpecial Paste:=xlPasteValues
      NR = Worksheets(WSary(a)).Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      
      'Apply Formatting
      With Worksheets(WSary(a)).Range("A6:M" & NR & ",Q6:R" & NR)
        .Borders.LineStyle = xlNone
        .Borders.LineStyle = xlContinuous
        .Interior.ColorIndex = 0
        .Borders.Weight = xlHairline
       End With
</pre>
 
Upvote 0
Here's a way to do it without looping:
Code:
Dim vSheet As Variant
vSheet = Application.Transpose(Range("shtname"))


"shtname" is the name of your named range.
 
Upvote 0
Do you get any error ? It seems OK to me.

How do i reflect your method with this, wherever i have (Wsary(a)) what do i replace it?


For a = LBound(WSary) To UBound(WSary)

NR = Worksheets(WSary(a)).Range("A" & Rows.Count).End(xlUp).Offset(1).Row

</pre>
 
Upvote 0
Here's a way to do it without looping:
Code:
Dim vSheet As Variant
vSheet = Application.Transpose(Range("shtname"))
"shtname" is the name of your named range.

So do i do this

vSheet = Application.Transpose(Range("shtname"))
WSary = Array(vSheet)
 
Upvote 0

Forum statistics

Threads
1,214,797
Messages
6,121,629
Members
449,041
Latest member
Postman24

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