Excel 工作表按名称排序 帮忙解释下每一行Vba代码
01.SubSort_Sheets()02.DimsCountAsInteger,IAsInteger,RAsInteger03.ReDimNa(0)AsString04...
01. Sub Sort_Sheets()
02. Dim sCount As Integer, I As Integer, R As Integer
03. ReDim Na(0) As String
04. sCount = Sheets.Count
05.
06. For I = 1 To sCount
07. ReDim Preserve Na(I) As String
08. Na(I) = Sheets(I).Name
09. Next
10.
11.
12. For I = 1 To sCount - 1
13. For R = I + 1 To sCount
14. If Na(R) < Na(I) Then
15. JH = Na(I)
16. Na(I) = Na(R)
17. Na(R) = JH
18. End If
19. Next
20. Next
21.
22.
23. For I = 1 To sCount
24. Sheets(Na(I)).Move After:=Sheets(I)
25. Next
26. End Sub 展开
02. Dim sCount As Integer, I As Integer, R As Integer
03. ReDim Na(0) As String
04. sCount = Sheets.Count
05.
06. For I = 1 To sCount
07. ReDim Preserve Na(I) As String
08. Na(I) = Sheets(I).Name
09. Next
10.
11.
12. For I = 1 To sCount - 1
13. For R = I + 1 To sCount
14. If Na(R) < Na(I) Then
15. JH = Na(I)
16. Na(I) = Na(R)
17. Na(R) = JH
18. End If
19. Next
20. Next
21.
22.
23. For I = 1 To sCount
24. Sheets(Na(I)).Move After:=Sheets(I)
25. Next
26. End Sub 展开
1个回答
展开全部
Sub Sort_Sheets()
Dim sCount As Integer, I As Integer, R As Integer
ReDim Na(0) As String '声明数组Na
sCount = Sheets.Count '统计工作表数量。
'------------收集所有工作表名称代码段--------------
For I = 1 To sCount
ReDim Preserve Na(I) As String 'Na被声明为动态数组,数组元素个数随I值发化变化
Na(I) = Sheets(I).Name '将第I个工作表名称存入数组
Next
'------------在数组中对名称进行排序-----------------
For I = 1 To sCount - 1 '从1到工作表数-1
For R = I + 1 To sCount '从i+1到工作表数
If Na(R) < Na(I) Then '若Na(R)<Na(i),将相邻两个数组元素(工作表名称)比较大小。文本也可比大小!
JH = Na(I) '本句和接下来两句,将Na(I)与Na(R)内容互换
Na(I) = Na(R)
Na(R) = JH
End If
Next
Next
'------------根据数组中排好的顺序,将相应工作表移到最后-------完成排序
For I = 1 To sCount '
Sheets(Na(I)).Move After:=Sheets(I)
Next
End Sub
追问
谢谢你啊 很详细
再问下这个 ReDim Na(0) As String 声明了数组 为什么里面是0?
ReDim Preserve Na(I) As String 这句的Preserve 是啥意思?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询