在VB中粘贴excel得来的数据,并且能够对数据进行运算
Dim strArray1() As String
Dim strArray2() As String
Dim sinTemp As Single
Private Sub Command1_Click()
If Text1.Text = "" Then
MsgBox "Text1 没有数据"
Exit Sub
End If
If Text2.Text = "" Then
MsgBox "Text2 没有数据"
Exit Sub
End If
ExecuteNum
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub Text1_Change()
Dim strArrayTemp() As String
Dim i As Integer
ReDim strArray1(0)
strArrayTemp = Split(Text1.Text, vbCrLf)
For i = LBound(strArrayTemp) To UBound(strArrayTemp)
If Trim(strArrayTemp(i)) <> "" Then
If LBound(strArray1) = 0 And strArray1(0) = "" Then
strArray1(0) = strArrayTemp(i)
Else
ReDim Preserve strArray1(UBound(strArray1) + 1)
strArray1(UBound(strArray1)) = strArrayTemp(i)
End If
End If
Next
End Sub
Private Sub Text2_Change()
Dim strArrayTemp() As String
Dim i As Integer
ReDim strArray2(0)
strArrayTemp = Split(Text2.Text, vbCrLf)
For i = LBound(strArrayTemp) To UBound(strArrayTemp)
If Trim(strArrayTemp(i)) <> "" Then
If LBound(strArray2) = 0 And strArray2(0) = "" Then
strArray2(0) = strArrayTemp(i)
Else
ReDim Preserve strArray2(UBound(strArray2) + 1)
strArray2(UBound(strArray2)) = strArrayTemp(i)
End If
End If
Next
End Sub
Private Sub ExecuteNum()
Dim i As Integer
If UBound(strArray1) <> UBound(strArray2) Then
MsgBox "列的个数不一样"
Exit Sub
End If
sinTemp = 0
For i = LBound(strArray1) To UBound(strArray1)
sinTemp = sinTemp + Val(strArray1(i)) * Val(strArray2(i))
Next
Label1.Caption = sinTemp
End Sub