2013-04-22 29 views
0

我怎样才能在一个表中选择某列的最大值,并在同一表单上显示的文本价值?列表框本身由一个依赖于用户输入的查询填充,所以它的值是事先未知的。如何在一个表中显示来自一列的最大值,在文​​本框相同的形式

我可以排序值列表框,选择第一个值,但它已经通过日期另一列排序,用于不同的目的。我想知道的是发生在列2

下一步是要显示或全部4列该日期为空白或N/A之前其occure值是maximimum值的日期。

+0

什么机制选择用于填充您的列表框查询?你说它是基于一个依赖于用户输入的查询,当用户指定查询时,你可以使用DMAX来填充文本框吗? – Simon1979 2013-04-22 21:02:45

回答

0

您可能会发现以下VBA代码很有用。它扫描值.Column数据名为List0列表框,例如内部...

2013-04-18 | 123 
2013-04-17 | 77 
2013-04-16 | 345 
2013-04-15 | 34 

...发现的时间(第一列)对应于最大值在列表框中的第二列中,并将该日期放入名为Text3的文本框中。请注意,CompareNumeric标志控制比较是基于字符串(“77”会赢)还是基于数字(345会赢)。

Private Sub Command2_Click() 
Const DateCol = 0 '' column numbers start with 0 
Const MaxCol = 1 '' second column has column index of 1 
Const CompareNumeric = True '' convert strings to numbers for finding maximum 

Dim RowIdx As Long, MaxItem As Variant, MaxIdx As Long, CurrItem As Variant, NewMaxFound As Boolean 

MaxIdx = -1 
MaxItem = Null 
For RowIdx = 0 To Me.List0.ListCount - 1 
    CurrItem = Me.List0.Column(MaxCol, RowIdx) 
    If CompareNumeric Then 
     CurrItem = Val(CurrItem) 
    End If 

    If IsNull(MaxItem) Then 
     NewMaxFound = True '' first one 
    Else 
     NewMaxFound = (CurrItem > MaxItem) 
    End If 
    If NewMaxFound Then 
     MaxItem = CurrItem 
     MaxIdx = RowIdx 
    End If 
Next 
If MaxIdx >= 0 Then 
    Me.Text3.Value = Me.List0.Column(DateCol, MaxIdx) 
End If 
End Sub 
相关问题