Excel listing named range in a worksheet and get the value

后端 未结 2 352
臣服心动
臣服心动 2021-01-20 22:14

How to obtain a list of named range exist in a specific worksheet that start with particular string (for example all named range that start with total) and grab the value? I

2条回答
  •  别那么骄傲
    2021-01-20 22:48

    The following function will output all the names and their totals in your Workbook.

    I think it is the basic block you need to get your code running.

    Sub btnTotal()
    
        For Each N In ActiveWorkbook.Names
               MsgBox N.Name + " " + CStr(Application.WorksheetFunction.Sum(Range(N)))
        Next N
    End Sub
    

    Edit

    Answering your comment:

    Define your names in this way:

    alt text

    Then (and only then) the following code works:

    Sub btnTotal()
    
      For Each N In ActiveSheet.Names
         If (InStr(N.Name, "!Total") <> 0) Then
             MsgBox N.Name + " " + CStr(Application.WorksheetFunction.Sum(Range(N)))
         End If
      Next N
    End Sub
    

    If you do not define the scope of the names correctly you need a lot of extra work in your code.

    Edit As you forgot to mention that you are still working with Excel 2003, here you will find an addin to manage name scoping in that version. See screen cap below

    alt text

    HTH

提交回复
热议问题