excel vba从多到少重复排序

excelVBA

1个回答

写回答

岚岚子

2025-11-02 19:36

+ 关注

VBA
VBA

Sub xx()Dim d As Object, c As Range Set d = CreateObject("scripting.Dictionary") Set c = Range(b5:u20) n = c.Count For i = 1 To n x = c.Item(i) d(x) = d(x) + 1 Next k = d.Keys t = d.Items ReDim a(0 To UBound(k)) For i = 0 To UBound(a) a(i) = i Next For i = 1 To UBound(a) For j = UBound(a) To i Step -1 If t(a(j)) > t(a(j - 1)) Then tmp = a(j) a(j) = a(j - 1) a(j - 1) = tmp End If NextEnd SubThe above code is a VBA macro written in Microsoft excel that performs the following tasks:1. Creates a dictionary object and a range object.2. Sets the two objects to specific values.3. The dictionary is initialized with the number of elements in the range as its capacity.4. A loop is used to iterate through all elements in the range and add them to the dictionary, incrementing their count by 1.5. The keys and values of the dictionary are stored in Arrays called k and t respectively.6. The values in the range are used to populate the array t, and the index array k is created based on the values in t.7. For loop is used to sort the elements in the array t in ascending order.8. Another for loop is used to compare consecutive elements in the sorted array t and swap them if necessary.9. The sorted array t is used as a lookup table for the values in a.10. Finally, a loop is used to loop through each element in a, and for each element it loops through all elements from its current position until the beginning of the array (step size -1) to find the next element greater than its current position.This code performs a range of operations including sorting and rearrangement of elements in an array based on their values. It can be useful in situations where specific arrangements or orderings need to be mAIntAIned within an array.

举报有用(0分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号