我编写了一个VBA代码,将与办公室相关的特定数量的东西插入到另一个Excel工作表中,该表格将它们放在一起并计算相关的成本。
现在,我想要将我的“办公室号码”从左到右排序为"1.2.30“、"1.1.1130”或"1.3.150“。为了排序,我必须改变它们,问题是如何做到这一点?
此外,在列中还有其他号码,我希望与标题行中的“办公室号码”进行交换。
示例
对此进行排序:
1.2.30 1.1.1130 1.3.150
1 4 7
2 5 8
3 6 9要这样做:
1.1.1130 1.2.30 1.3.150
4 1 7
5 2 8
6 3 9Excel会这样排序: 1.2.30,1.3.150,1.1.1130
我必须找到一种方法将这些数字转换为正常数字(我已经通过排除这些".")并将它们保存为最后一个点后的字符串,并添加尽可能多的"0“,这样我就有了5个数字长度的标准化数字,我猜?
因此,在最后一个点之后,我的所有办公室号码看起来是这样的: 1.2.30 = (1.2.)00030,1.3.150 = (1.3.)150 = 00150和1.1.1130 = (1.1.)01130
到目前为止我尝试过的排序代码:
Sub Table1Sort()
Range("B39:Q39").Select
Selection.ClearContents
Range("B44:Q44").Select
Selection.ClearContents
Range("B9:Q28").Select
ActiveWorkbook.Worksheets("Table1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Table1").Sort.SortFields.Add2 Key:=Range( _
"B10:Q10"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Table1").Sort
.SetRange Range("B9:Q28")
.Header = xlGuess
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
Dim rng As Range
For Each rng In Range("B9:Q9")
rng = rng
Next
End Sub将数字保存为字符串并添加零?或者我的逻辑全错了?
发布于 2019-09-24 00:15:26
下面是建议的解决方案。
Sub Table1Sort()
Dim i As Long
Dim rg As Range, cl As Range
Dim parts As Variant
Dim fmt As String, id As String
fmt = String(4, "0") 'in this case 4 seems to be the max lenght of number parts, adjust as needed
Set rg = Range("A1:C5") 'your range to get sorted - adjust to the correct address
For Each cl In rg.Rows(1).Cells 'Transform numbers into a sort string, unless blank
If cl <> "" Then
id = ""
parts = Split(cl.Text, ".") 'Split into parts by numbers, pad with leading zeroes and concatenate with a separator
For i = 0 To UBound(parts)
id = id & "-" & Format(CInt(parts(i)), fmt)
Next i
id = Mid(id, 2) 'Remove leading separator
cl.Value = id 'Put into cell
End If
Next cl
ActiveWorkbook.Worksheets("Table1").Sort.SortFields.Clear 'Do the sorting
ActiveWorkbook.Worksheets("Table1").Sort.SortFields.Add2 Key:=rg.Rows(1) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Table1").Sort
.SetRange rg
.Header = xlGuess
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
For Each cl In rg.Rows(1).Cells 'Transform sort strings back to original
If cl <> "" Then
id = ""
parts = Split(cl.Text, "-")
For i = 0 To UBound(parts)
id = id & "." & CInt(parts(i))
Next i
id = Mid(id, 2)
cl.Value = id
End If
Next cl
End Sub您还可以考虑将目标范围作为参数传递。
发布于 2019-09-24 22:17:03
这是我想出来的:
Option Explicit
Sub Table1Sort()
Dim i As Integer
Dim iRows As Integer
Dim iLen As Integer
Dim Arr() As Variant
ActiveSheet.Range("d3:e5").Select 'I randomly entered the values to be sorted in a column here.
iRows = Selection.Rows.Count 'Figure out how many items I'm dealing with.
Arr = Range("d3:e5").Value2 'Write them to an array. I include the column next to the data as a placeholder.
For i = 1 To iRows 'Strip periods and fill/overwrite 2nd column of array
Arr(i, 2) = Replace(Arr(i, 1), ".", "")
If Len(Arr(i, 2)) > iLen Then iLen = Len(Arr(i, 2))
Next i
For i = 1 To iRows 'Pad with trailing zeros
Do While Len(Arr(i, 2)) < iLen
Arr(i, 2) = Arr(i, 2) & "0"
Loop
Next i
QuickSortArray Arr, , , 2 'Call the sort found here: https://stackoverflow.com/a/5104206/12000364
For i = 1 To iRows 'Write the results out across columns. I randomly start at column F.
Cells(1, 5 + i) = Arr(i, 1)
Next i
End Sub正如我在代码注释中提到的,我使用了在这里找到的多维数组排序- https://stackoverflow.com/a/5104206/12000364,并在第二维排序。
发布于 2019-09-26 20:29:57
这是我在@Dschuli和@Miles Fett的帮助下完成的代码。
现在它可以正常工作,没有任何问题:)
Sub Table1Sort()
Dim i As Long
Dim rg As Range, cl As Range
Dim parts As Variant
Dim fmt As String, id As String
fmt = String(5, "0")
Set rg = Tabelle1.Range("B9:Q28")
For Each cl In rg.Rows(1).Cells
If cl <> "" Then
id = ""
parts = Split(cl.Text, ".")
For i = 0 To UBound(parts)
id = id & "-" & Format(CInt(parts(i)), fmt)
Next i
id = Mid(id, 2)
cl.Value = id
End If
Next cl
Tabelle1.Sort.SortFields.Clear
Tabelle1.Sort.SortFields.Add Key:=rg.Rows(1) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Tabelle1.Sort
.SetRange rg
.Header = xlGuess
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
Tabelle1.Range("B39:Q39").ClearContents
Tabelle1.Range("B44:Q44").ClearContents
For Each cl In rg.Rows(1).Cells
If cl <> "" Then
id = ""
parts = Split(cl.Text, "-")
For i = 0 To UBound(parts)
id = id & "." & CInt(parts(i))
Next i
id = Mid(id, 2)
cl.Value = id
End If
Next cl
End Subhttps://stackoverflow.com/questions/58064294
复制相似问题