Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing global named range without referencing worksheet

Tags:

range

excel

vba

I want to export data from a set of named ranges in workbook x to another set of ranges in workbook y. The names of the ranges to be exported from and to are spesified in a table "Table_Export". Problem is I do not want to spesify in what sheets these respective ranges are located, seeing that I do not know the actual sheet names. But since the ranges are global it should somehow be possible to reference them in vba without referencing the respective worksheet?

The relevant code can be found below where either of the two starred lines within the if statement are what I want to accomplish.

Dim x As Workbook, y As Workbook    
Set x = ThisWorkbook
Set y = Workbooks.Open(Range("Export_to").Value)

Dim export_control As ListObject    
Set export_control = x.Sheets("Control").ListObjects("Table_Export")

Dim lr As Excel.ListRow    
For Each lr In export_control.ListRows
    If lr.Range(1).Value <> 0 Then    
        *'y.Names(lr.Range(2).Value).RefersToRange.Value = x.Names(lr.Range(1).Value).RefersToRange.Value*    
        *'y.Range(lr(Range(2).Value).Value = x.Range(lr(Range(1).Value).Value*    
    End If
Next
like image 610
Roger Grøndahl Avatar asked Aug 13 '14 11:08

Roger Grøndahl


People also ask

How do you reference a named range in another worksheet?

To reference a cell or range of cells in another worksheet in the same workbook, put the worksheet name followed by an exclamation mark (!) before the cell address. For example, to refer to cell A1 in Sheet2, you type Sheet2! A1.

How do you reference a named range in Excel?

Just select the name of interest in the Excel Name Manager, and type a new reference directly in the Refers to box, or click the button at the right and select the desired range on the sheet. After you click the Close button, Excel will ask if you want to save the changes, and you click Yes. Tip.

Can I use named ranges throughout the worksheet?

It is possible to use worksheet specific named ranges on other worksheets by prefixing the named range with its worksheet name (e.g. Sheet2! Sheet2_B3). Only the worksheet level named ranges on the active worksheet are displayed in the Name Box and in the (Insert > Name > Define) dialog box.

Can you reference named ranges in VBA?

Named Ranges are the name given for a group or range of cells. Named range is used to refer to the specific range of cells which needs to be accessed later on. We can name a range in both excel or VBA. While making a named range in VBA we need to use Add name function in VBA.


1 Answers

Use the Property Names of the respective Workbook objects to reference Global Names.

like image 189
Pieter Geerkens Avatar answered Nov 09 '22 23:11

Pieter Geerkens