2017-02-22 44 views
1

我有一个excel文件看起来像这样如何使用excel按字排序每一行?

 A   B   C 
1| A_xxxx | B_xxxx | C_xxxx 
2| B_xxxx | A_xxxx | C_xxxx 
3| C_xxxx | B_xxxx | A_xxxx 

..... (每个xxxx是不同的号码)

我想每一行,使表如下排序:

 A   B   C 
1| A_xxxx | B_xxxx | C_xxxx 
2| A_xxxx | B_xxxx | C_xxxx 
3| A_xxxx | B_xxxx | C_xxxx 

....

我该怎么办?

+0

在下划线之前是否只有一个单个字符,并且它们都在范围A-Z中? – gbavba

+0

它不是单个字符,但可以在下划线之前将其替换为单个字符。 我只是有一个D,所以他们都在A-Z的范围内。 –

+0

列A中的所有值是否都以下划线之前的相同字符开头,还是每行都需要按字母顺序排序? – gbavba

回答

0

该子将与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的单个大写字母。

相关问题