Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用VBScript进行排序顺序检查_Vba_Date_Excel_Vbscript_Sorting - Fatal编程技术网

Vba 使用VBScript进行排序顺序检查

Vba 使用VBScript进行排序顺序检查,vba,date,excel,vbscript,sorting,Vba,Date,Excel,Vbscript,Sorting,假设我有一个数组arr1,其中包含日期值,如下所示: Arr1(50)=(“2012年3月9日4:57:02上午”、“2012年3月22日5:57:02上午”、“2012年5月9日8:57:02上午”、“2011年3月9日4:57:02上午”) 编辑 Dim CellCount 仪表板(10) CellCount=0 直到arr(细胞计数)=“和Ubound(arr)>9 如果CStr(arr(CellCount)所以你知道它现在已经分类了 然后转换回一个变量数组 一些代码片段可以帮助您朝这个

假设我有一个数组arr1,其中包含日期值,如下所示:

Arr1(50)=(“2012年3月9日4:57:02上午”、“2012年3月22日5:57:02上午”、“2012年5月9日8:57:02上午”、“2011年3月9日4:57:02上午”)

编辑

Dim CellCount
仪表板(10)
CellCount=0
直到arr(细胞计数)=“和Ubound(arr)>9
如果CStr(arr(CellCount)
现在,有没有不使用任何循环技术的直接方法来确定Arr1()的日期值是否升序


谢谢,

不,因为在第一个反例(可能是最后一个反例)之前,您必须检查元素。

他正在回答您的问题。你问,有没有办法找到没有循环的排序方式? 他说不。你的眼睛,我的眼睛可以看到它是否分类。但是,在不检查集合元素的情况下,您希望Excel/computer如何做到这一点呢

当数据位于数组中时,需要循环遍历其元素。希望一切都清楚

所以我只能说

  • 如果需要,用逗号分隔符将其拆分为变量。但这不是必需的,因为Array()可以将元素放入1D变量中
  • 使用转置将其转储到
    范围内
  • 使用工作表排序方法/功能对整个范围进行排序

    --->所以你知道它现在已经分类了

  • 然后转换回一个变量数组
一些代码片段可以帮助您朝这个方向前进:

Option Explicit

    Sub omgArraySort()
       Dim inputArray As Variant
       Dim outputArray As Variant
       Dim upperB as Long

       inputArray = Array("9/3/2012 4:57:02 AM","22/3/2012 5:57:02 AM", _ 
                          "9/5/2012 8:57:02 AM","9/3/2011 4:57:02 AM")
       '-- sorted array
       outputArray = sortRange(inputArray)

          upperB = UBound(iArray, 1) '-- for 1D array you may also use UBound(iArray)
          If (Err.Number <> 0) Then '-- if there's an error, it's erro code is > 0
            MsgBox "Dates sorted, not empty"
          End If
    End Sub

    '-- dump into sheet and sort in the sheet and dump back into the array
    Function sortRange(ByVal iArray As Variant) As Variant
       Dim rngSort as Range
       Dim i As Long

       Set rngSort = WorkSheets(1).Range("B2")
       i = Ubound(iArray,1)

       With rngSort.Resize(i)
          .Value = WorksheetFunction.Transpose(iArray)
          .Sort rngSort, xlDescending, Header:=xlNo
          sortRange = .Value              
       End With
    End Function
选项显式
子omgArraySort()
作为变量的Dim输入阵列
作为变量的Dim输出阵列
B长得一样
inputArray=Array(“9/3/2012 4:57:02 AM”、“22/3/2012 5:57:02 AM”
“2012年9月5日上午8:57:02”、“2011年9月3日上午4:57:02”)
'--排序数组
outputArray=sortRange(输入阵列)
upperB=UBound(iArray,1)“--对于1D数组,您也可以使用UBound(iArray)
如果(Err.Number 0),则'--如果有错误,则其错误代码大于0
MsgBox“已排序的日期,不为空”
如果结束
端接头
'--转储到工作表中并在工作表中排序,然后再转储回数组中
函数sortRange(ByVal iArray作为变量)作为变量
Dim rngSort as范围
我想我会坚持多久
设置rngSort=工作表(1)。范围(“B2”)
i=Ubound(iArray,1)
使用rngSort.Resize(i)
.Value=工作表函数.Transpose(iArray)
.Sort rngSort,xlDescending,标头:=xlNo
sortRange=.Value
以
端函数

例如,当您处理大量数据时,从工作表到代码的流量可能会降低您的性能


请务必知道,当某人在SO中有可信的答案(例如Ekkehard的答案)时,你必须注意。他们没有正当理由这么说。

有没有更快的比较方法可以完成我要求的任务?请告诉我!使用
Worksheetfunction.Small/Large/Rank/Max/Min
可能有助于跳过一些周期。我可以在Juri上获得一些代码演示吗?Please@J尤里·鲁特,请帮帮我here@TukaiRakshit只要你零零碎碎地问问题……在没有提供适当图片的情况下,你就必须继续创建登录名(请建议更高级的技术。@TukaiRakshit如果这有效,请标记-因此系统已更新,我们都知道您完整的脚本问题列表中还有1个问题已解决;)
i=Ubound(iArray,1)
Ubound(outputArray)-1
这些语句代表什么?例程有时可能什么也不返回-表示一个空数组。因此对其调用UBound()会导致错误#9“下标超出范围”您可以检测错误以确定排序数组为空。
iArray
是1D数组,那么为什么
UBound(iArray,1)
而不是
UBound(iArray)
?两者都是相同的。如果1D,您可以使用其中一个
Option Explicit

    Sub omgArraySort()
       Dim inputArray As Variant
       Dim outputArray As Variant
       Dim upperB as Long

       inputArray = Array("9/3/2012 4:57:02 AM","22/3/2012 5:57:02 AM", _ 
                          "9/5/2012 8:57:02 AM","9/3/2011 4:57:02 AM")
       '-- sorted array
       outputArray = sortRange(inputArray)

          upperB = UBound(iArray, 1) '-- for 1D array you may also use UBound(iArray)
          If (Err.Number <> 0) Then '-- if there's an error, it's erro code is > 0
            MsgBox "Dates sorted, not empty"
          End If
    End Sub

    '-- dump into sheet and sort in the sheet and dump back into the array
    Function sortRange(ByVal iArray As Variant) As Variant
       Dim rngSort as Range
       Dim i As Long

       Set rngSort = WorkSheets(1).Range("B2")
       i = Ubound(iArray,1)

       With rngSort.Resize(i)
          .Value = WorksheetFunction.Transpose(iArray)
          .Sort rngSort, xlDescending, Header:=xlNo
          sortRange = .Value              
       End With
    End Function