2016-08-12 360 views
2

我在Excel中排序了一个非常大的范围,它由多个列组成,而不是所有列都被填充。在某些列中,有时会输入空格,并且单元格的时间为Empty。我想要做的是将空白区域和空单元格移动到范围的底部。目前,excel将顶部的空白值和底部的空白单元格放在一起。或者,我不介意可以通过范围并用Empty替代空白值的消毒功能。我写了一篇,但是我给出的范围非常慢(接近4000行,需要2分钟才能完成)。以及我发布的排序sub我正在使用。Excel VBA排序范围,空白和空值在范围底部

我查看了自定义订单,但它看起来像需要您指定您计划使用的所有值。是否可以使用自定义比较功能,而无需编写全新排序sub

Sub SortCallLog() 
    Application.ScreenUpdating = False 
    Dim longTime As Double 
    longTime = Millis 
    Dim lastRow As Long 
    Dim ws As Worksheet 
    Dim sortOrder As Variant 
    Set ws = Worksheets("CallLog") 
    Call addToIndexesHV(FirstCBSort, SecondCBSort, ThirdCBSort, FourthCBSort) ' These are global variables referring to the sort order 
    sortOrder = getSortOrder(False) 
    lastRow = GetLastRow(ws) 
    With ws.Sort 
     .SortFields.Clear 
     For sortIndex = 0 To getVariantLength(sortOrder) - 1 
      .SortFields.Add Key:=Range(ConvertCBSortToColRow(CStr(sortOrder(sortIndex)))), Order:=xlAscending, SortOn:=xlSortOnValues, DataOption:=xlSortNormal 
     Next sortIndex 
     .Header = xlYes 
     .MatchCase = False 
     .SetRange ws.Range("B1:X" & (lastRow)) 
     .Apply 
    End With 
    Debug.Print (Millis - longTime) 
    Application.ScreenUpdating = True 
Exit Sub 

消毒部

For Each cell In ws.Range("B2:L" & (lastRow)).Rows.Cells 
    cell.value = Trim(cell.value) 
    If "" = cell.value Then 
     cell.value = Empty 
    End If 
Next cell 

回答

1

代替对每个小区使用阵列来净化你的细胞

用您的实际范围替换sheet1.usedrange。这将需要1或2秒

Sub test() 
    Dim arr 
    Dim lctrRow As Long 
    Dim lctrCol As Long 

    arr = Sheet1.UsedRange 

    For lctrRow = LBound(arr, 1) To UBound(arr, 1) 
     For lctrCol = LBound(arr, 2) To UBound(arr, 2) 
      If Trim(arr(lctrRow, lctrCol)) = "" Then 
       arr(lctrRow, lctrCol) = Empty 
      End If 
     Next 
    Next 

    Sheet1.UsedRange.Cells(1, 1).Resize(UBound(arr, 1), UBound(arr, 2)) = arr 
End Sub 

这将减少所有时间。当你开始分类/消毒时,也要确保你的计算模式是manaul。这可能也有帮助。

+1

是的,那好多了,好多了。在我的机器上花了94毫秒。这应该使它更清晰,我认为这对我的应用程序来说已经足够了。谢谢。 – RWA4ARC