简体   繁体   English

如何在给定文本条件下使用VBA对一个表进行排序?

[英]How can I sort a a table with VBA with given text condition?

I am using a software that generates similar outputs in the picture below: 我正在使用在下图中生成类似输出的软件: 在此处输入图片说明

I would like to sort this table according to the numbers after the RetValue. 我想根据RetValue之后的数字对该表进行排序。

Is there an easy way to do this with VBA? 有使用VBA做到这一点的简便方法吗?

我不会为此使用VBA,而是将文本拆分为多个单元格,并在“筛选/排序”中按新创建的“ __Y”列进行排序。

With data in A1 through B4 , in C1 enter: 对于A1B4中的数据,在C1中输入:

=--LEFT(MID(A1,FIND(" ",A1)+1,9999),LEN(MID(A1,FIND(" ",A1)+1,9999))-1)

and then run the Recorded macro: 然后运行Recorded宏:

Sub Macro1()
    Range("A1:C4").Select
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C1:C4"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:C4")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

在此处输入图片说明

I am always a big fan of storing numbers in cells. 我一直是将数字存储在单元格中的忠实粉丝。 Especially in Excel number are worth storing as such in order to allow for calculations. 尤其是在Excel中,这样的数字值得存储,以便进行计算。 So, why don't you put into a cell just the number 36 (for example) with the 因此,为什么不将数字36(例如)与

.NumberFormat = """RetValue ""00""Y"""

Then you still get to see RetValue 36Y but in fact only 36 is the .Value of the cell and you are allowed for calculations. 然后您仍然可以看到RetValue 36Y但实际上只有36是单元格的.Value ,并且可以进行计算。

Still, to sort your above table there is nothing special to be considered. 不过,要对您的上表进行排序,没有什么特别的要考虑的。 Since all cells start with the same string, all cells will be sorted anyway correctly. 由于所有单元格均以相同的字符串开头,因此无论如何都将正确地对所有单元格进行排序。 The only "problem" will be the RetValue 4Y since it is not a two-digit number. 唯一的“问题”将是RetValue 4Y因为它不是两位数。

Once again, I'd try to format it correctly. 再一次,我会尝试正确格式化它。 If that is not an option then I'd go for any of the solutions provided here to extract the number and then sort it as such. 如果这不是一个选择,那么我将使用此处提供的任何解决方案来提取数字,然后对它进行排序。 During this process I would (as described above) only store the number in the cell.Value and wrap the text around it as .NumberFormat to allow for more calculations in the future (if necessary). 在此过程中,我将(如上所述)仅将数字存储在cell.Value并将其周围的文本包装为.NumberFormat以允许将来进行更多计算(如有必要)。

Afterwards, you can sort the table for example with this solution: Sort range without sorting it in a spreadsheet 之后,您可以使用以下解决方案对表格进行排序无需对电子表格进行排序即可对范围进行排序

It gives you the option to sort it as an array in memory before pasting the result to the sheet or to use a UDF (entered as an array formula). 它使您可以选择在将结果粘贴到工作表之前将其作为内存中的数组排序或使用UDF(作为数组公式输入)。

Note for the future: please don't post pictures but rather post formatted text we can copy for testing. 未来的注意事项:请不要发布图片,而是发布可以复制以进行测试的格式化文本。

VBA may not be required for this. 为此,可能不需要VBA。

You can insert the following formula in column C (assuming RetValue in Column A and Value in Column B and your data starts from 3rd row). 您可以在C列中插入以下公式(假设A列中的RetValue和B列中的Value,并且数据从第三行开始)。

=MID(A3, FIND(" ",A3,1)+1, FIND("Y",A3,FIND(" ",A3,1))-10) = MID(A3,FIND(“”,A3,1)+1,FIND(“ Y”,A3,FIND(“”,A3,1))-10)

You can sort on column C. 您可以在C列上排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM