Is it possible to have a named range attached to “activesheet,” rather than a specific sheet?

Is it possible to name a range without it being slaved to a single sheet? Like if I select “rnge1” on sheet 1, it will select range e2:e7 on sheet 1, and then I can go to sheet 2 and select “rnge1” and it will select range e2:e7 on sheet 2.

I’d like to use the named ranges I have in a single macro attached to a button. I have 1 button on each of 10 sheets. The ranges (rnge1, rnge2, rnge3) encompass e2:e7, e213:e184, e281:e284. I like using the named ranges because they automatically update when a row is added in the sheet, versus going into VBA to update it every time. I’d prefer to not have to make named ranges for each page.

When I try to remove the sheet name (=’MON (1)’!$E$2:$E$7) so that it looks like this: =$E$2:$E$7, it just reverts back to the one with the sheet name. The issue with this is that when I click the button on sheet 2, it runs the macro on sheet 1. I’ve also tried “=activesheet.range(e2:e7)”, which I found on another post, but it just causes an error.

here is a copy of the code for reference:

Sub DELETE_E()
 Dim ws As Worksheet, rng As Range
 Range("RNGE3").ClearContents
 Set ws = ActiveSheet: Set rng = ws.Range("RNGE2")
 With rng
    .Formula = Evaluate("IF(LEFT(FORMULATEXT(" & rng.Address & "),8)=""=Left($B"","""",FORMULATEXT(" & rng.Address & "))")
    .Formula = Evaluate("IF(LEFT(FORMULATEXT(" & rng.Address & "),7)=""=MISC.!"","""",FORMULATEXT(" & rng.Address & "))")
    .Formula = Evaluate("IF(LEFT(FORMULATEXT(" & rng.Address & "),3)=""=$B"","""",FORMULATEXT(" & rng.Address & "))")
    .Formula = Evaluate("IF(LEFT(FORMULATEXT(" & rng.Address & "),7)=""=MID($B"","""",FORMULATEXT(" & rng.Address & "))")
    On Error Resume Next
     .SpecialCells(xlCellTypeConstants).ClearContents
    On Error GoTo 0
  End With
   Set ws = ActiveSheet: Set rng = ws.Range("RNGE1")
   With rng
    On Error Resume Next
     .SpecialCells(xlCellTypeConstants).ClearContents
    On Error GoTo 0
    End With
 End Sub

8

Having a name (“MyName”) with “Sheet1” as scope, you can use its address in the active sheet in the next way:

 Dim rng as Range
 Set rng = Range(Range("MyName").address)
 rng.Select

It will select in the active sheet the range defined for “MyName”, even referring “Sheet1” as scope.

1

So this is going to be a bit of a dump… there are lots of ways to reference ranges in VBA, or just define ranges; e.g.:

dim currentNamedRange as range
set currentNamedRange = ThisWorkbook.Name("namedRange").RefersToRange 'uses a named range in the whole workbook and finds its location
set currentNamedRange = ThisWorkbook.Sheets(1).Range("E2:E7") 'uses a specified range on a sheet
set currentNamedRange = Range("E2:E7") 'a range with no qualifications, meaning it is the active sheet

Similar to setting up the named ranges by selecting them and labeling, you could also define each range in VBA and use other events, per sheet, to interact with the reference, e.g.:

Public currentNamedRange as range

Private Sub Worksheet_BeforeDoubleClick(Target As Range, Cancel As Boolean)
    Select Case Target
        Case Cells(1,"R")             
            Set currentNamedRange = Range("E2:E7")
        Case Cells(2,"R")             
            Set currentNamedRange = Range("E213:E184")
        Case Cells(3,"R")             
            Set currentNamedRange = Range("E281:E284")
        Case Else
            Exit Sub
    End Select
End Sub

3

We can using INDIRECT to have a Range reference that is not scoped to any particular worksheet. The advantage of the approach over Range(Range("rnge1").Address) is that they will work no matter what worksheet is deleted.

In this example, I added 3 named ranges to the Workbook Names collection. They will work regardless of which worksheet they are qualified to.

Sub AddAndTestNamedRanges()
    Rem Add the Named Ranges to the Workbook
    Rem So they can work across multiple sheets.
    With ThisWorkbook.Names
        .Add Name:="rnge1", RefersToR1C1:="=INDIRECT(""E2:E7"")"
        .Add Name:="rnge2", RefersToR1C1:="=INDIRECT(""E213:E184"")"
        .Add Name:="rnge3", RefersToR1C1:="=INDIRECT(""E281:E284"")"
    End With

    Rem Test the Named Ranges
    Debug.Print Sheet1.Name, Sheet1.Range("rnge1").Address(External:=True)
    Debug.Print Sheet2.Name, Sheet2.Range("rnge2").Address(External:=True)
    Debug.Print Sheet3.Name, Sheet3.Range("rnge3").Address(External:=True)
End Sub

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật