Private Sub MSHFlexGrid1_Compare(ByVal Row1 As Long, ByVal Row2 As Long, cmp As Integer)
Dim x1 As Double, x2 As Double, n1 As Long, n2 As Long
If MSHFlexGrid1.TextMatrix(Row1, 1) <> "" Then
x1 = CVar(MSHFlexGrid1.TextMatrix(Row1, 1))
x2 = CVar(MSHFlexGrid1.TextMatrix(Row2, 1))
If x1 < x2 Then
cmp = -1
ElseIf x1 > x2 Then
cmp = 1
Else '第一列相等的情况下,按第i列排序
Dim i%
For i = 2 To 50
n1 = CVar(MSHFlexGrid1.TextMatrix(Row1, i))
n2 = CVar(MSHFlexGrid1.TextMatrix(Row2, i))
If n1 < n2 Then
cmp = -1
ElseIf n1 > n2 Then
cmp = 1
Else
cmp = 0
End If
Next i
End If
cmp = cmp * SortType
End If
End Sub
这几天在做一个类似于excel的数据分析统计软件,遇到问题,像上面这段代码在排序的时候只能识别数字也就是说排的时候只要遇到文字就报错,想了半天想不出来,像以下这种情况就不可以排啦就报错:
高等数学 大学英语 大学语文 大学物理 历史 政治
67 76 78 98 99 100
76 65 60 70 80 98
67 78 89 79 70 78
60 69 90 78 87 87
67 89 99 88 77 66
65 67 89 87 79 99
Dim x1 As Double, x2 As Double, n1 As Long, n2 As Long
If MSHFlexGrid1.TextMatrix(Row1, 1) <> "" Then
x1 = CVar(MSHFlexGrid1.TextMatrix(Row1, 1))
x2 = CVar(MSHFlexGrid1.TextMatrix(Row2, 1))
If x1 < x2 Then
cmp = -1
ElseIf x1 > x2 Then
cmp = 1
Else '第一列相等的情况下,按第i列排序
Dim i%
For i = 2 To 50
n1 = CVar(MSHFlexGrid1.TextMatrix(Row1, i))
n2 = CVar(MSHFlexGrid1.TextMatrix(Row2, i))
If n1 < n2 Then
cmp = -1
ElseIf n1 > n2 Then
cmp = 1
Else
cmp = 0
End If
Next i
End If
cmp = cmp * SortType
End If
End Sub
这几天在做一个类似于excel的数据分析统计软件,遇到问题,像上面这段代码在排序的时候只能识别数字也就是说排的时候只要遇到文字就报错,想了半天想不出来,像以下这种情况就不可以排啦就报错:
高等数学 大学英语 大学语文 大学物理 历史 政治
67 76 78 98 99 100
76 65 60 70 80 98
67 78 89 79 70 78
60 69 90 78 87 87
67 89 99 88 77 66
65 67 89 87 79 99
------------------------------------------------------------------以前回贴的代码:'记录集排序法(字符,数字通用)
Sub RecordsetSort(anyInput As Variant, Optional Sort As String = "ASC") Dim rs As New Recordset
Dim i As Long
With rs
.Fields.Append "FSort", adVarChar, 50
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.Open
For i = LBound(anyInput) To UBound(anyInput)
.AddNew
!FSort = anyInput(i)
.Update
Next
'.UpdateBatch
.Sort = "FSort " & Sort
For i = LBound(anyInput) To UBound(anyInput)
anyInput(i) = !FSort
.MoveNext
Next
End With
Set rs = Nothing
End Sub
这个方法要注意的是字段的数据类型一定要正确
但我的意思大概是,比如有个MSHFlexGrid,里面的数据只是手工输入的,现在要把它弄进RecordSet里,是只能把数据存进数据库里再查出来,还是有别的巧妙的方法直接把数据导入RecordSet里?
但我的意思大概是,比如有个MSHFlexGrid,里面的数据只是手工输入的,现在要把它弄进RecordSet里,是只能把数据存进数据库里再查出来,还是有别的巧妙的方法直接把数据导入RecordSet里?
-------------------------------------------------------------
那段代码就是啊,声明一个Recordset,然后用Append方法添加字段,示例中只添加了一个字段:FSort,你可以任意添加多个字段。同样的的用Open方法打开这个Recordset后用AddNow方法向字段中添加内容,示例中是数组,比如你用MSHFlexGrid控件,这儿你只要循环给每个字段赋值,再用Update更新就将数据添加到这个Recordset中去了。排序可以用Recordset的Sort方法,可以实现多个字段的顺序和逆序......
MSHFlexGrid用的不多,不知道怎么直接解决的方法
用代码给MSHFlexGrid排序试了下可行的,代码:Function SortRecordset(ByVal Col As Integer, _
Optional Sort As String = "ASC") As Recordset
'col要排序的列 Sort排序方式默认ASC Dim rs As New Recordset
Dim i As Long, j As Long
With rs
'添加字段
.Fields.Append "C1", adVarChar, 10
.Fields.Append "C2", adInteger
.Fields.Append "C3", adInteger
.Fields.Append "C4", adInteger
.Fields.Append "C5", adInteger
'打开记录集
.Open
'添加fg数据
For i = 1 To fg.Rows - 1
.AddNew
For j = 1 To fg.Cols - 1
.Fields(j - 1) = fg.TextMatrix(i, j)
Next j
.Update
Next i
'按指定列、指定方式排序
.Sort = "C" & Col & " " & Sort
End With
'排序后的记录集
Set SortRecordset = rs
Set rs = NothingEnd FunctionPrivate Sub fg_Click()
If fg.MouseRow = 0 And fg.MouseCol <> 0 Then
Set fg.DataSource = SortRecordset(fg.Col, "ASC")
End If
End SubPrivate Sub Form_Load()
Dim i As Integer, j As Integer
fg.Rows = 20
fg.Cols = 6
Randomize
For i = 1 To fg.Rows - 1
fg.TextMatrix(i, 1) = Chr(CInt(Rnd * 26) + 65)
For j = 2 To fg.Cols - 1
fg.TextMatrix(i, j) = i * 10 + j * 21
Next j
Next i
End Sub
你的代码我试了,过不去,提示有错误
User-defined type not defined
选择“工程\引用”命令,引用“Microsoft AetiveX Date Objects 2.7 Library”