0

I'm trying to use visual basic for applications to loop through a list of named ranges from an excel sheet as the first step in a process for altering their formulas. Its all part of an Excel plug in I work with using Visual Studo I have a basic loop, which came directly from the MS docs.


        Dim n As Excel.Name

        Dim names As Excel.Names = destStreetEstimates.Names

        Dim test_string As String = destStreetEstimates.Name
        MsgBox(test_string)

        For Each n In names
            MsgBox(Prompt:=n.Name)
        Next n

This doesn't work, it looks like com objects are being put together into a list when I turn on the VS debugger (yay), but the com objects aren't being turned into anything useful I can loop through. (boo).

Am I missing something? I've gone through the Docs several times, nothing I've read there matches with the errors I'm getting. But I'm still pretty green.

https://learn.microsoft.com/en-us/office/vba/excel/concepts/cells-and-ranges/refer-to-named-ranges

I've looked at other guides, most of them seemed to be discussing how to programmatically generate named ranges. I have to start with the ranges I've been given, its about 70, and while I have all their names in a config file there's a good chance the sheet will have additional names I haven't been given in the future and my function has to work in that case too.

1 Answer 1

0

See https://www.thespreadsheetguru.com/blog/the-vba-guide-to-named-ranges

Sub NamedRange_Loop()
'PURPOSE: Named Ranges in the Active Workbook
'SOURCE: www.TheSpreadsheetGuru.com

Dim nm As Name

'Loop through each named range in workbook
  For Each nm In ActiveWorkbook.Names
    Debug.Print nm.Name, nm.RefersTo
  Next nm
  
'Loop through each named range scoped to a specific worksheet
  For Each nm In Worksheets("Sheet1").Names
    Debug.Print nm.Name, nm.RefersTo
  Next nm

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.