按字母顺序对组合框值进行排序 [英] Sort combobox values alphabetically

查看:79
本文介绍了按字母顺序对组合框值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户表格中有一个用于Excel的组合框.按字母排序的最简单方法是什么?它的值在vba中进行了硬编码,而新值则被添加到了底部,因此它们已经没有任何顺序了.

I have a combobox in a userform for excel. What is the easiest way to sort it alphabetically? The values for it are hardcoded in vba and new ones are just added to the bottom so they are not in any kind of order already.

当前正在使用用户窗体,以便我们的用户可以将数据库中的数据导入excel.组合框在那里,因此他们可以指定要导入的客户端数据.

The userform is currently being used so that our users can import data from our database into excel. The combobox is there so they can specify which client data to import.

推荐答案

在添加它们时,请将它们与组合框中已有的值进行比较. 如果它们少于您遇到的项目,请更换该项目. 如果它们不小于,则继续前进,直到找到该项目小于的东西. 如果找不到该项目,则将其添加到末尾.

As you are adding them, compare them to the values already in the combobox. If they are less than the item you come across, the replace the item. If they are not less than, then move on until you find something the item is less than. If it cannot find the item, then add it to the end.

For X = 0 To COMBOBOX.ListCount - 1
  COMBOBOX.ListIndex = X
  If NEWVALUE < COMBOBOX.Value Then
     COMBOBOX.AddItem (NEWVALUE), X
     GoTo SKIPHERE
     End If
Next X
        COMBOBOX.AddItem (NEWVALUE)
SKIPHERE:

这篇关于按字母顺序对组合框值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆