大概知道了~
以您提供資料進行分析
所以我寫出2種不同的加總判斷
希望對您有幫助~
資料一(A)為單純數字時分析 「一般大都用這種方式」
把資料一當作陣列位置並把資料三作加總即可
TEMP(DATA1) += DATA3
搜尋時找到陣列位置就可以找到加總值
輸出加總值= TEMP(輸入資料一值)
資料二(B)為字串時分析 「也可以利用二維陣列」
把資料二放到暫存陣列當索引
啟動暫存陣列迴圈
當暫存資料中索引有資料二 就把資料三加總
複製程式
TEMP1(i) = DATA1(i)
For j = 0 To UBound(TEMP1)
If TEMP1(j) = DATA1(i) Then
TEMP2(j) += DATA2(i)
Exit For
End If
Next
搜尋時
啟動暫存陣列迴圈
當暫存資料中索引為輸入值時 輸出加總值
複製程式
For j = 0 To UBound(TEMP1)
If TEMP1(j) = TextBox2.Text Then
Label1.Text = TEMP2(j)
Exit For
End If
Next
附件為執行程式壓縮檔vb2008
程式完整原碼:
複製程式
Public Class Form1
Dim DATA0() As Object = {"111", "222", "333", "111", "444", "222"}
Dim DATA1() As Object = {"AA", "BB", "CC", "DD", "AA", "FF"}
Dim DATA2() As Object = {"6", "1", "2", "4", "5", "3"}
Dim TEMP(999) As Integer
Dim TEMP1(UBound(DATA0)) As String
Dim TEMP2(UBound(DATA0)) As Integer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox1.Text = "資料一" & vbTab & "資料二" & vbTab & "資料三" & vbTab & vbTab & "一 加總 二" & vbNewLine & "=======================" & vbTab & vbTab & "==============" & vbNewLine
For i = 0 To UBound(DATA0)
TEMP(CInt(DATA0(i))) += CInt(DATA2(i))
TEMP1(i) = DATA1(i)
For j = 0 To UBound(TEMP1)
If TEMP1(j) = DATA1(i) Then
TEMP2(j) += DATA2(i)
Exit For
End If
Next
TextBox1.Text &= DATA0(i) & vbTab & DATA1(i) & vbTab & DATA2(i) & vbTab & vbTab & TEMP(CInt(DATA0(i)))
For j = 0 To UBound(TEMP1)
If TEMP1(j) = DATA1(i) Then
TextBox1.Text &= vbTab & TEMP2(j)
Exit For
End If
Next
TextBox1.Text &= vbNewLine
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = ""
Label1.Text = TEMP(CInt(TextBox2.Text))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Label1.Text = ""
For j = 0 To UBound(TEMP1)
If TEMP1(j) = TextBox2.Text Then
Label1.Text = TEMP2(j)
Exit For
End If
Next
End Sub
End Class
個人解法.請各位先進指導~