Reputation: 213
I want to set all the named range in one variable and export to PDF. I am able to do when I manually enter all the named range. My problem is named range are variable, sometime it will have one some time it will have more than 10. Please advice...I tried with following codes.
Dim wbBook As Workbook
Dim nName As Name
Set wbBook = ActiveWorkbook
Set nName=wbBook.Names
Set rs = wbBook.Range(nNames)
rs.Select
Selection.ExportAsFixedFormat xlTypePDF, strPath, , , False
But below code works for me, when I enter range name manually..
Set rs = wbBook.Range("Page_1,Page_2,Page_3")
rs.Select
Selection.ExportAsFixedFormat xlTypePDF, strPath, , , False
Upvotes: 0
Views: 247
Reputation: 5426
You can't pass a collection (many objects) to an object variable (single object). You need to loop through the collection's objects. Usually we do this with a For Each
loop, but in this case, as I needed to set the first element separately because of the Union
, I have used a simple For
loop.
Also, avoid using .Select
if you can. Just use your objects directly.
Also note that you are talking about all the named ranges in the Workbook, and this might cause problems if you have named ranges in multiple worksheets. I haven't tested, but I doubt it would work.
Dim wbBook As Workbook
Dim i As Integer
Dim rs As Range
Set wbBook = ActiveWorkbook
Set rs = wbBook.Names(1).RefersToRange
For i = 2 To wbBook.Names.Count
Set rs = Union(rs, wbBook.Names(i).RefersToRange)
Next
rs.ExportAsFixedFormat xlTypePDF, strPath, , , False
Upvotes: 4