是否可以使用VBA从左到右对整个工作表进行排序

时间:2016-04-06 12:21:06

标签: excel vba excel-vba

我有一张包含以下信息的大型工作表:

1: B T B J S

2: A

3: T S S P E 

4: E O R P W

这些数字都属于A列。我想在VBA脚本中包含一行来安排整个工作表按字母顺序对所有字母进行排序,同时将数字保持在同一位置:

1: B B J S T

2: A

3: E P S S T

4: E O P R W 

当然,这可以一次一行地执行,但是可以安排这样的整个工作表吗?我知道从左到右按字母顺序排序的每一行都会将数字保持在相同的位置,所以没有必要考虑这一点。我有一个简单的解决方案吗?

2 个答案:

答案 0 :(得分:1)

对于我修改过的字母排序和brettdj,http://www.thespreadsheetguru.com感谢VBA Exclude special characters and numbers but keep spaces from string:{{3}}

Sub sortcells(StartRange As Range)

    Dim strArrCell() As String
    Dim intTemp As Integer

    Do While rngStart.Value <> ""
        intTemp = Split(StartRange.Value, ":")(0)
        strArrCell = Split(StripNonAlpha(rngStart.Value), " ")
        strArrCell = Alphabetically_SortArray(strArrCell)
        StartRange.Value = intTemp & ": " & Join$(strArrCell, " ")
        Set StartRange = StartRange.Offset(1, 0)
    Loop

End Sub

Function Alphabetically_SortArray(myArray() As String) As String()

    Dim x As Long, y As Long
    Dim TempTxt1 As String
    Dim TempTxt2 As String

    For x = LBound(myArray) To UBound(myArray)
      For y = x To UBound(myArray)
        If UCase(myArray(y)) < UCase(myArray(x)) Then
          TempTxt1 = myArray(x)
          TempTxt2 = myArray(y)
          myArray(x) = TempTxt2
          myArray(y) = TempTxt1
        End If
       Next y
    Next x

    Alphabetically_SortArray = myArray

End Function

Function StripNonAlpha(TextToReplace As String) As String
    Dim ObjRegex As Object
    Set ObjRegex = CreateObject("vbscript.regexp")
    With ObjRegex
        .Global = True
        .Pattern = "[^a-zA-Z\s]+"
        StripNonAlpha = .Replace(Replace(TextToReplace, "-", Chr(32)), vbNullString)
    End With
End Function

答案 1 :(得分:1)

希望下面的代码能满足

  for(i=1;i<=shippingDays;i++){

      var result = new Date();
      result.setTime( result.getTime() + i * 86400000 );
      console.log(result);
      console.log(result.getMonth()+'-'+result.getDate()+'-'+result.getFullYear());
      newDate = new Date(result.getMonth()+'-'+result.getDate()+'-'+result.getFullYear());
     console.log(newDate);
 };