该子将与VBA做到这一点:
Sub SortValsToCols()
Dim w1 As Worksheet
Dim w2 As Worksheet
Dim rCell As Range
Dim rRng As Range
Dim rowCounters As New Scripting.Dictionary
Dim i As Long
Set w1 = Sheets("Sheet1") 'Change to name of the worksheet containing data.
Set w2 = Sheets("Sheet2") 'Change to name of a blank worksheet. The sorted data will end up here.
Set rRng = w1.Range("A1:C3") 'Change to range of cells containing data.
'Turn off ScreenUpdating to speed up execution.
Application.ScreenUpdating = False
'Fills a dictionary with keys from A-Z and gives each an initial value of 1.
For i = 65 To 90
rowCounters.Add Key:=Chr(i), Item:=1
Next i
'Loops thru all cells in range specified and adds the value after the last populated _
cell in the relevant column to the target sheet.
For Each rCell In rRng.Cells
w2.Range(Left$(rCell.Value, 1) & rowCounters(Left$(rCell.Value, 1))).FormulaR1C1 = rCell.Value
rowCounters(Left$(rCell.Value, 1)) = rowCounters(Left$(rCell.Value, 1)) + 1
Next rCell
'Turn ScreenUpdating back on.
Application.ScreenUpdating = True
End Sub
你需要改变3个SET
报表,以满足您的工作簿和运行前的VBE通过工具 - 添加“Microsoft脚本运行”>参考。
还假定所有单元格值以?_
开头,其中?
是来自A-Z的单个大写字母。
在下划线之前是否只有一个单个字符,并且它们都在范围A-Z中? – gbavba
它不是单个字符,但可以在下划线之前将其替换为单个字符。 我只是有一个D,所以他们都在A-Z的范围内。 –
列A中的所有值是否都以下划线之前的相同字符开头,还是每行都需要按字母顺序排序? – gbavba