Getting and activating the sheet name iof a named range

TamirBasin

New Member
Joined
Apr 11, 2017
Messages
49
Office Version
  1. 365
Platform
  1. Windows
Hi,
When I run my macro, I have to activate the correct sheet before updating the range of the Named Range.
Therefore, I thought to activate the relevant sheet of the currently named range.
I get an error code.
I would appreciate the help.
Is there another way to do it?

Code:
Sub UpdateRange2()
Dim NamedRange As Name
Dim ColumnOfRange As Long
Dim LastRow As Long
Dim wb As Workbook
Dim ws As Worksheet

i = 1
Set wb = ActiveWorkbook
Debug.Print ActiveWorkbook.Names.Count

For Each nm In ActiveWorkbook.Names

Set NamedRange = wb.Names.Item(i)
//The next line gives me the error
ws = NamedRange.RefersToRange.Parent.Name
    
ColumnOfRange = Range(wb.Names.Item(i)).Column

// The next line refers the ColumnOfRange (which is a number) to the active sheet. 
//This is why I need to be on the sheet of the specific Named Range. 
LastRow = Cells(Rows.Count, ColumnOfRange).End(xlUp).Row

NamedRange.RefersTo = NamedRange.RefersToRange.Resize(LastRow, 1)
    
Debug.Print nm.Name, nm.RefersTo
' MsgBox NamedRange.Name & vbLf & NamedRange.RefersTo & "Range Number "
    
i = i + 1
    
Next nm

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi,
In most cases, you should not need to activate a worksheet for your code to apply to it.

Not tested but see if this update to your code does what you want:

Code:
Sub UpdateRange2()
    Dim NamedRange As Name, nm As Name
    Dim ColumnOfRange As Long
    Dim LastRow As Long
    Dim wb As Workbook
    Dim ws As Worksheet
    
    i = 1
    Set wb = ActiveWorkbook
    Debug.Print ActiveWorkbook.Names.Count
    
    For Each nm In ActiveWorkbook.Names
    
    Set NamedRange = wb.Names.Item(i)
'set object variable to worksheet
    Set ws = NamedRange.RefersToRange.Parent
        
    ColumnOfRange = ws.Range(wb.Names.Item(i)).Column
    
'// The next line refers the ColumnOfRange (which is a number).
    LastRow = ws.Cells(ws.Rows.Count, ColumnOfRange).End(xlUp).Row
    
    NamedRange.RefersTo = NamedRange.RefersToRange.Resize(LastRow, 1)
        
    Debug.Print nm.Name, nm.RefersTo
    'MsgBox NamedRange.Name & vbLf & NamedRange.RefersTo & "Range Number "
        
    i = i + 1
'clear memory
     Set ws = Nothing
     Set NamedRange = Nothing
    Next nm


End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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