【问题标题】:Sorting numbers and text at one time creates issues一次对数字和文本进行排序会产生问题
【发布时间】:2016-04-21 14:02:30
【问题描述】:

我有一个问题。我正在从一个大的产品表(大约 32'000 行)中过滤我需要的产品,并首先按产品名称排序,然后按交货编号排序。

我的问题是,如果我想过滤和排序数字作为产品名称(这也是可能的),它不会正确排序(见图)。

Picture of the filtered and sorted table

我的代码是这个:

'*************************************************************
'Filter Product Code
Set myrange = Range("A1:A" & lastrow)

For Each c In myrange
    If Len(c.Value) <> 0 Then
                ThisWorkbook.Worksheets(Worksheets.Count).Columns("A:D").AdvancedFilter xlFilterCopy, _
            Sheet1.Range("A1:A" & lastrow), Sheet1.Range("G1"), False
    End If
Next

'Sheet1.Range("51:100").RemoveDuplicates Columns:=3, Header:=xlYes

'*******************************************************************
'Sort the filtered list first by Product Code then by the Delivery number
Dim lngRow As Long
Dim lngRowMax As Long
Dim wsf As WorksheetFunction

With Sheet1

lastrow = Cells(Rows.Count, 8).End(xlUp).Row

Range("G2:J" & lastrow).Sort Key1:=Range("G2:G" & lastrow), _
 Order1:=xlAscending, Key2:=Range("I2:I" & lastrow), _
 Order1:=xlAscending, Header:=xlNo

Set wsf = Application.WorksheetFunction
lngRowMax = .UsedRange.Rows.Count

End With

如何才能对其进行正确排序。首先是相同的产品代码,然后是交货编号(升序),因为我想先销售最旧的产品。

感谢您的回复。

【问题讨论】:

  • 第4行和第5行数据的单元格格式有区别吗?只需使用数据功能区上的排序功能,它就会按照您对我的期望进行排序。
  • 必须用宏来完成。 G 列格式化为文本,I 列格式化为数字。我认为这应该不是问题,对吧?还是不行。 @PeterT, @user3598756 有什么想法吗?

标签: vba excel sorting macros


【解决方案1】:

现在我找到了解决方案。只有一个小的调整。现在排序的代码如下所示:

Dim lngRow As Long
Dim lngRowMax As Long
Dim wsf As WorksheetFunction

 With Sheet1

lastrow = Cells(Rows.Count, 8).End(xlUp).Row

Range("G1:J" & lastrow).Sort Key1:=Range("G1:G" & lastrow), _
   Order1:=xlAscending, Key2:=Range("I1:I" & lastrow), _
   Order2:=xlAscending, Header:=xlYes, DataOption1:=xlSortTextAsNumbers


Set wsf = Application.WorksheetFunction
lngRowMax = .UsedRange.Rows.Count

End With

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2012-12-31
    • 2016-11-28
    • 2014-02-03
    • 2015-06-06
    • 2021-05-03
    相关资源
    最近更新 更多