m=c(0).b for i=0 to ubound(c) if c(i).a="aaa" and c(i).b<m then m=b j=i end if next'c(j) 不知是不是这个意思
同意采用leswang107(leswang107) 的方法,最快,而且容易处理大数据量
Option ExplicitPrivate Type sss a As String b As Integer End Type Dim c() As sssPrivate Sub Command1_Click() '大致两步走,第一步,先把所有的c( ).a=a的元素取出, '放入相同类型的数组d中,同时用数组index记录对应的下标 '第二步,对d( ).b排序,这里算法没有优化,你可以用其他算法优化一下 On Error GoTo myerr Dim minnum As Integer Dim d() As sss Dim index() As Integer Dim i As Integer, j As Integer Dim a As String Dim minindex As Integer a = "str0" j = 1 For i = 1 To 100 If c(i).a = a Then ReDim Preserve d(1 To j) ReDim Preserve index(1 To j) d(j).a = c(i).a d(j).b = c(i).b index(j) = i j = j + 1 End If Next MsgBox UBound(d) minnum = d(1).b minindex = index(1) For i = 2 To UBound(d) If minnum > d(i).b Then minnum = d(i).b minindex = index(i) End If Next MsgBox "对应要查找的c().a=" + a + "中,相应的b值最小的元素为c(" + CStr(minindex) + "),其对应的b值为" + CStr(minnum) Exit Sub myerr: Select Case Err.Number Case 9 MsgBox "没有找到符合要求的数据" End Select End SubPrivate Sub Form_Load() 'init_c() ReDim c(1 To 100) Dim i As Integer For i = 1 To 100 c(i).a = "str" + CStr(i Mod 7) Randomize c(i).b = i + CInt(100 * Rnd()) Next End Sub
for i=0 to ubound(c)
if c(i).a="aaa" and c(i).b<m then
m=b
j=i
end if
next'c(j)
不知是不是这个意思
a As String
b As Integer
End Type
Dim c() As sssPrivate Sub Command1_Click()
'大致两步走,第一步,先把所有的c( ).a=a的元素取出,
'放入相同类型的数组d中,同时用数组index记录对应的下标
'第二步,对d( ).b排序,这里算法没有优化,你可以用其他算法优化一下
On Error GoTo myerr
Dim minnum As Integer
Dim d() As sss
Dim index() As Integer
Dim i As Integer, j As Integer
Dim a As String
Dim minindex As Integer
a = "str0"
j = 1
For i = 1 To 100
If c(i).a = a Then
ReDim Preserve d(1 To j)
ReDim Preserve index(1 To j)
d(j).a = c(i).a
d(j).b = c(i).b
index(j) = i
j = j + 1
End If
Next
MsgBox UBound(d)
minnum = d(1).b
minindex = index(1)
For i = 2 To UBound(d)
If minnum > d(i).b Then
minnum = d(i).b
minindex = index(i)
End If
Next
MsgBox "对应要查找的c().a=" + a + "中,相应的b值最小的元素为c(" + CStr(minindex) + "),其对应的b值为" + CStr(minnum)
Exit Sub
myerr:
Select Case Err.Number
Case 9
MsgBox "没有找到符合要求的数据"
End Select
End SubPrivate Sub Form_Load()
'init_c()
ReDim c(1 To 100)
Dim i As Integer
For i = 1 To 100
c(i).a = "str" + CStr(i Mod 7)
Randomize
c(i).b = i + CInt(100 * Rnd())
Next
End Sub