Excel VBA: Can I name a range using a formula?

前端 未结 2 427
遇见更好的自我
遇见更好的自我 2021-01-27 10:59

My goal is to name my just-pasted range something unique to I can find it in the future.

The copied and pasted range comes from a drop-down menu, and thus must be modi

2条回答
  •  醉话见心
    2021-01-27 11:50

    I think YowE3K has the right approach. I refactored his code because I don't like Do Loop.

    Sub AddName()
    
        Dim myNameBase As String
        Dim arr() As String
        Dim maxName As Long
        Dim n As Name
        myNameBase = "AddSection_" & Replace(Worksheets("Add Section").Range("D3").Value, " ", "")
    
        For Each n In Names
            If n.Name Like myNameBase & "*" Then
                If n.Name = myNameBase Then
                    maxName = 1
                ElseIf n.Name Like myNameBase & ".*." Then
                    arr = Split(n.Name, ".")
                    If arr(UBound(arr) - 1) >= maxName Then maxName = arr(UBound(arr) - 1) + 1
                End If
    
            End If
        Next
        Selection.Name = myNameBase & IIf(maxName, "." & maxName & ".", "")
    
    End Sub
    

    YowE3K Thanks for the help!

提交回复
热议问题