我有一个从任何DB断开的Flexgrid(vb6都不是.Net),它具有一些这样的行:
135,00 4
218,00 4
100,00 10
6,00 4
15,00 22
我无法创建一个周期来获取网格上有多少个不同的组(按增值税),并计算相对数量,以便有3个组(在这种情况下为4/10/22)4 = 359,00 10 = 100 22 = 15,00我不想使用带有select group by的数据库,而是预先计算金额并在n文本框中显示。
这是我尝试过的代码
Dim A, b, c, d
if A = "" Then
A = xNum
ElseIf A <> "" Then
If A <> xNum Then
If b = "" Then
b = xNum
ElseIf b <> "" Then
If b <> xNum Then
If c = "" Then
c = xNum
ElseIf c <> "" Then
If c <> xNum Then
If d = "" Then
d = xNum
ElseIf d <> "" Then [etc...]
谢谢你的帮助。
我假设您使用的是Microsoft FlexGrid控件,其中第一列是数量,第二列是增值税。
以下代码将产生显示在文本框中的结果:
Option Explicit
Private Sub cmdCalc_Click()
On Error Resume Next
Dim c As Collection
Dim i As Integer
Dim g As Group
Dim data As String
'calculate groups
Set c = New Collection
For i = 1 To MSFlexGrid1.Rows - 1
Set g = New Group
g.VAT = CInt(MSFlexGrid1.TextMatrix(i, 2))
g.Qty = CDbl(MSFlexGrid1.TextMatrix(i, 1))
Err.Clear
c.Add g, CStr(g.VAT)
If Err.Number > 0 Then c(CStr(g.VAT)).Qty = c(CStr(g.VAT)).Qty + g.Qty
Next
'and visualize the data
For Each g In c
data = data & g.VAT & vbTab & g.Qty & vbCrLf
Next
Text1.Text = data
End Sub
此代码的基本思想是构建一组组对象,网格中的每一行一组。在将组添加到集合中时,错误陷阱可用于检测重复项并在这些情况下增加数量。
这是Group
上面的代码使用的类:
Option Explicit
Private m_VAT As Integer
Private m_Qty As Double
Public Property Get VAT() As Integer
VAT = m_VAT
End Property
Public Property Let VAT(ByVal Value As Integer)
m_VAT = Value
End Property
Public Property Get Qty() As Double
Qty = m_Qty
End Property
Public Property Let Qty(ByVal Value As Double)
m_Qty = Value
End Property
非常感谢Brian,它绝对完美!您所做的正是我想要完成的,但是我不知道该怎么做。我将从您的代码中汲取灵感,以始终学习一些东西。我已经尝试过了,效果很好。非常感谢你
好吧,那你也应该赞成这个答案:)