Results 1 to 6 of 6

Thread: Add Named Ranges To Multiple Workbooks Using VBA

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    Stalker, this isn't very specific to what you've asked, but I am mostly certain that this can do the trick. If you can get the way this works, it should be a straightforward thing to do. Check the macro. You can modify it to suit your need. If you think it will suit your purpose, you can download the file.

    Code:
    Option Explicit
    
    Sub AddCusNames()
        
        'Adds all listed names from this workbook to other workbooks
        Dim nm As Name
        Dim lng As Long
        Dim lngList As Long
        Dim var As Variant
        var = ThisWorkbook.Sheets(1).ListBoxes("lstWorkBooks").List
        For lngList = LBound(var) To UBound(var)
            If ThisWorkbook.Sheets(1).ListBoxes("lstWorkBooks").Selected(lngList) Then
                With ThisWorkbook.Sheets(1)
                    For lng = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
                        If .Cells(lng, 4).Value = "Worksheet" Then
                            Call Application.Workbooks(var(lngList)).Worksheets(.Cells(lng, 3).Value).Names.Add(.Cells(lng, 1).Value, .Cells(lng, 2).Value)
                        Else
                            Call Application.Workbooks(var(lngList)).Names.Add(.Cells(lng, 1).Value, .Cells(lng, 2).Value)
                        End If
                    Next lng
                End With
            End If
        Next lngList
        
    End Sub
    
    Sub GetWbks()
    
        'Get a list of ALL open workbooks
        Dim wbk As Workbook
        With ThisWorkbook.Sheets(1).ListBoxes("lstWorkBooks")
            .RemoveAllItems
            For Each wbk In Application.Workbooks
                If (wbk.Name <> ThisWorkbook.Name) And (Not wbk.IsAddin) And (wbk.Path <> Application.StartupPath) Then
                    .AddItem wbk.Name
                End If
            Next wbk
        End With
        
    End Sub
        
    Sub GetNms()
    
        'Get the names of all named ranges in any selected workbook
        Dim nm As Name
        Dim wbk As Workbook
        Dim var As Variant
        Dim lng As Long
        
        On Error GoTo eRRh
        Set wbk = Workbooks.Open(Application.GetOpenFilename("*.xl*", , "Select File", , False), False, True)
        ReDim var(1 To ThisWorkbook.Names.Count, 1 To 4)
        For Each nm In ThisWorkbook.Names
            lng = lng + 1
            If InStr(1, nm.Name, "!") Then
                var(lng, 1) = Split(nm.Name, "!")(1)
            Else
                var(lng, 1) = nm.Name
            End If
            var(lng, 2) = "'" & nm.RefersTo
            If nm.Parent.Name = ThisWorkbook.Name Then
                var(lng, 3) = nm.RefersToRange.Parent.Name
                var(lng, 4) = "Workbook"
            Else
                var(lng, 3) = nm.Parent.Name
                var(lng, 4) = "Worksheet"
            End If
        Next nm
        ThisWorkbook.Sheets(1).Cells(Rows.Count, 1).End(xlUp)(2).Resize(UBound(var), 4).Value = var
        wbk.Close 0
        Exit Sub
    eRRh:    MsgBox "Unexpected Error!", vbExclamation, ""
        
    End Sub
    You can even manually update the list of names in the sheet (be sure you are 100% accurate in filling at the four properties of the named range, including taking care of the apostrophes (') )
    Attached Files Attached Files
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Automatically Create Multiple Dynamic Named Ranges Using VBA
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 0
    Last Post: 04-24-2013, 03:49 PM
  2. Consolidate multiple workbooks from a folder into one master file VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 4
    Last Post: 02-26-2013, 09:00 PM
  3. Replies: 2
    Last Post: 12-19-2012, 08:28 AM
  4. Detect Duplicates In Named Ranges With Cross Check
    By Yegarboy in forum Excel Help
    Replies: 3
    Last Post: 10-09-2012, 11:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •