vba 根据名称管理器引用的单元格排序

Sub SortNamesByRefersTo()
    Dim wb As Workbook
    Set wb = ThisWorkbook ' 或者您的目标工作簿
    
    Dim ws As Worksheet
    Set ws = wb.Worksheets("现金") ' 修改为您的目标工作表
    
    Dim nm As Name
    Dim sortedNames() As String
    Dim i As Integer
    
    ' 遍历名称管理器并将名称存储在数组中
    For Each nm In ws.Names
        ReDim Preserve sortedNames(i)
        sortedNames(i) = nm.Name
        i = i + 1
    Next nm
    
    ' 使用冒泡排序按 RefersTo 地址排序
    Dim temp As String
    For i = LBound(sortedNames) To UBound(sortedNames) - 1
        For j = i + 1 To UBound(sortedNames)
            If wb.Names(sortedNames(i)).RefersTo > wb.Names(sortedNames(j)).RefersTo Then
                temp = sortedNames(i)
                sortedNames(i) = sortedNames(j)
                sortedNames(j) = temp
            End If
        Next j
    Next i
    
    ' 打印排序后的名称管理器
    For i = LBound(sortedNames) To UBound(sortedNames)
        Debug.Print sortedNames(i)
    Next i
End Sub

你可能感兴趣的:(vba)