【问题标题】:How can I sort dates in an array in vba?如何在 vba 中对数组中的日期进行排序?
【发布时间】:2014-01-10 08:53:22
【问题描述】:

您好,我是编程新手,刚开始学习 VBA for excel。我有一个关于数组排序的查询。如何对包含日期的数组进行排序?例如,如果我有一个包含日期的数组(“23-jul-13”、“11-jan-10”、“1-may-09”、“3-feb-04”),我该如何对该数组进行排序。我在整个互联网上搜索了答案,但只能找到用于排序数字的代码。我已经为此绞尽脑汁 2 天,但似乎无法得到它。

谢谢

我有下面的代码,它从选定的列中获取日期,但每当我运行它时都会出错。两天来我一直在试图弄清楚它有什么问题。我之前没有提到这段代码,因为我认为它会不必要地增加混乱。 子 GetUniqueAndCount 工作正常,但问题在于排序子,因为它不接受作为参数传递给它的数组。

Sub GetUniqueAndCount()
Dim d As Object, c As Range, k, tmp As String

  Set d = CreateObject("scripting.dictionary")
  'I will select the column of dates
 For Each c In Selection
  tmp = Trim(c.Value)
  If Len(tmp) > 0 Then
  If Year(DateValue(Format(tmp, "dd-mmm-yy"))) = 2013 Then
  d(tmp) = d(tmp) + 1
  End If
  End If
  Next c
  i = 0
  ReDim ThisArray(UBound(d.keys)) As Date
  For Each k In d.keys
  ThisArray(i) = DateValue(Format(k, "dd-mmm-yy"))
  i = i + 1

  Next k
  Sort (ThisArray)
End Sub


Sub Sort(arr() As Date)

  Dim Temp As Date
  Dim i As Long
  Dim j As Long

  For j = 2 To UBound(arr)

  Temp = arr(j)
  For i = j - 1 To 1 Step -1
  If (arr(i) <= Temp) Then GoTo 10
  arr(i + 1) = arr(i)

  Next i
  i = 0
10  arr(i + 1) = Temp


  Next j
  End Sub

【问题讨论】:

  • 您有日期数组还是看起来像日期的字符串数组?如果它们是实际日期,那么它们应该像数字一样可排序(因为 excel 实际上将日期存储为数字)
  • 能否提供格式化前的值示例,即单元格中存在的值?
  • 是的,它们如下:23-jul-13","11-jan-10","1-may-09","3-feb-04

标签: arrays vba sorting excel


【解决方案1】:

您的Sort(arr() As Date) 工作正常。问题出在这一行

Sort (ThisArray)

改成

Sort ThisArray

另外,既然您将Dates 存储在ThisArray 中,我希望您已将其声明为Date?

示例

Sub Sample()
    Dim ThisArray(1 To 5) As Date

    ThisArray(1) = #12/13/2013#
    ThisArray(2) = #12/13/2012#
    ThisArray(3) = #12/13/2015#
    ThisArray(4) = #12/13/2014#
    ThisArray(5) = #12/13/2016#

    SortAr ThisArray

    For i = 1 To 5
        Debug.Print ThisArray(i)
    Next i
End Sub

Sub SortAr(arr() As Date)
    Dim Temp As Date
    Dim i As Long, j As Long

    For j = 2 To UBound(arr)
        Temp = arr(j)
        For i = j - 1 To 1 Step -1
            If (arr(i) <= Temp) Then GoTo 10
                arr(i + 1) = arr(i)
        Next i
        i = 0
10:     arr(i + 1) = Temp
    Next j
End Sub

输出

13/12/2012 
13/12/2013 
13/12/2014 
13/12/2015 
13/12/2016 

【讨论】:

  • 嗨,Siddharth Rout,谢谢我找出问题所在,您的代码帮助我做到了。这是一个非常简单的错误。我认为 Sort 方法是一个预定义的 VBA 函数,因此不接受任何参数。我还将语法从 Sort (ThisArray) 更改为 SortAr ThisArray。代码运行良好。我不知道如何感谢你或给你 +1,因为我需要 15 的代表才能这样做。很抱歉...
  • 我没有注意到您在代码上方的评论。是的,你是对的,问题是你提到的这一行排序(ThisArray)
【解决方案2】:

这可能会有所帮助。有空问一个后续问题。

Sub Sort()
Dim x As Long, y As Long, z As Long
    For x = Application.WorksheetFunction.Min(Columns("M")) To Application.WorksheetFunction.Max(Columns("M"))
        For y = 1 To Worksheets("Users Info").Cells(Rows.Count, 13).End(xlUp).Row
        If Worksheets("Users Info").Cells(y, 13).Value = i Then
            z = z + 1
            Worksheets("Users Info").Cells(z, 14).Value = i
        End If
        Next y
    Next x
End Sub

【讨论】:

  • 欢迎来到 StackOverflow:如果您发布代码、XML 或数据示例,请在文本编辑器中突出显示这些行,然后单击编辑器工具栏上的“代码示例”按钮 ( { } ) 或使用 Ctrl键盘上的 +K 可以很好地格式化和语法突出显示它!
【解决方案3】:

我使用了两种不同的方法来将日期填充到工作表上的下拉列表中,作为唯一的和排序的最旧到最新的值:

  • 第一种方式,使用 Adodb Connection。
  • 第二种方式,使用脚本字典对象和用户定义的函数。

我使用的 Adodb 连接代码:

Dim con, rs As Object, sorgu As String
Set con = CreateObject("adodb.connection")
Sheets("Page1").ComboBox1.Clear
Sheets("Page1").ComboBox2.Clear
    
    #If VBA7 And Win64 Then
con.Open "provider=microsoft.ace.oledb.12.0;data source=" & ThisWorkbook.FullName & ";extended properties=""excel 12.0;hdr=yes"""
    #Else
con.Open "provider=microsoft.jet.oledb.4.0;data source=" & ThisWorkbook.FullName & ";extended properties=""excel 8.0;hdr=yes"""
    #End If
          
Set rs = CreateObject("adodb.recordset")
sorgu = "select Date from [Page1$] group by Date"
rs.Open sorgu, con, 1, 1
While Not rs.EOF
Sheets("Page1").ComboBox1.AddItem Format(rs("Date").Value, "dd.mm.yyyy")
Sheets("Page1").ComboBox2.AddItem Format(rs("Date").Value, "dd.mm.yyyy")
rs.movenext
Wend
rs.Close
con.Close

Source

【讨论】:

    猜你喜欢
    • 2017-03-20
    • 2023-03-04
    • 2010-10-10
    • 2014-07-08
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多