【发布时间】:2021-12-27 23:08:42
【问题描述】:
我正在尝试使用 vba 对 Excel 中的选项卡进行排序,但我对编写代码来更改我在网上找到的现有答案还不够熟悉。
我有多个 Excel 文件,每个文件都有不同的编号系统。 其中包括项目 (#ofsheet),如项目 (1)、项目 (8)、项目 (28)。当我尝试现有代码时,它们按第 1、28 和 8 项组织起来,而应该是 1、8、28。
有人可以帮我写代码吗?谢谢。
编辑:抱歉,我最初是在手机上写的。这段代码对我来说可以将项目放入项目 (1)、项目 (11)、项目 (2)、项目 (34) 订单中。
Sub sortAscendinfg()
Dim i, N, k As Double
'Count the number of worksheets and store the number in variable "n"
N = Application.Sheets.Count
'Do the following look for each worksheet again
For i = 1 To N
'Loop through all worksheets until the second last one (later you use the .move after function)
For k = 1 To N - 1
'If the name is larger than the following worksheet, change the sequence of these two worksheets.
'In order to enable a proper comparison, change all characters to lower case (UCase = Upper case works
'the same way.
If LCase(Sheets(k).Name) > LCase(Sheets(k + 1).Name) Then Sheets(k).Move After:=Sheets(k + 1)
Next
Next
End Sub
【问题讨论】:
-
总是有助于显示您正在使用的代码,并解释您在尝试修改它时遇到的确切错误。看起来您正在将工作表名称作为文本进行比较,当您需要将它们排序为数字时,但是如果没有您的代码,就很难就如何修复它提出任何建议。
-
您确实意识到您的工作表被正确排序(第一个数字是第一个区别:
1, 2, 8)并且您更喜欢不同类型的排序。请分享您用来获得不良结果的代码,以便我们在此基础上进行构建。