主题:VB编程对EXCEL数据处理
Private Sub Commandbutton1_Click()
Dim C1, C2, SX, SX2, SX3, SX4, SY, SY2, SYX, SYX2, b0, b1, b2, result1, result2, result3 As Single
Dim DEN, T2, T3, T4 As Single
Dim tempN1, tempN2 As Single
Dim ai, A, A2, P, B, W As Single
Dim i, k, j As Integer
Dim bRet As Boolean
Rem i为总的输入个数
Rem k为希望观察的变量
Rem j用作普通循环计数
k = [F3]
i = [F4]
P = [H3]
B = [H4]
W = [H5]
j = 0
bRet = False
SX = 0
SX2 = 0
SX3 = 0
SX4 = 0
SY = 0
SY2 = 0
SYX = 0
SYX2 = 0
b0 = 0
b1 = 0
b2 = 0
result1 = 0
If i <= 0 Then
MsgBox ("i<=0")
Call CleanResult
Exit Sub
ElseIf k <= 0 Then
MsgBox ("k<=0")
Call CleanResult
Exit Sub
ElseIf k > i Then
MsgBox ("k>i")
Call CleanResult
Exit Sub
ElseIf (k < 4 Or k > (i - 3)) Then
MsgBox ("k不在适合i的范围内!")
Call CleanResult
Exit Sub
Else
tempN1 = Cells(k + 3 + 2, 2)
tempN2 = Cells(k - 3 + 2, 2)
C1 = 0.5 * (tempN1 + tempN2)
C2 = 0.5 * (tempN1 - tempN2)
If C2 = 0 Then
MsgBox "N" & (k + 3 + 2) & "=N" & (k - 3 + 2) & ",引起C2=0参数错误!"
Call CleanResult
Exit Sub
Else
End If
Call JudgeNi(bRet, C1, C2)
If bRet Then
Call CleanResult
Exit Sub
Else
End If
j = k - 3
Rem Do Until j>i
Do Until j > k + 3
SX = SX + ((Cells(j + 2, 2) - C1) / C2)
SX2 = SX2 + ((Cells(j + 2, 2) - C1) / C2) ^ 2
SX3 = SX3 + ((Cells(j + 2, 2) - C1) / C2) ^ 3
SX4 = SX4 + ((Cells(j + 2, 2) - C1) / C2) ^ 4
SY = SY + (Cells(j + 2, 3))
SY2 = SY2 + (Cells(j + 2, 3)) ^ 2
SYX = SYX + ((Cells(j + 2, 2) - C1) / C2) * (Cells(j + 2, 3))
SYX2 = SYX2 + ((Cells(j + 2, 2) - C1) / C2) ^ 2 * (Cells(j + 2, 3))
j = j + 1
Loop
j = j - 4
DEN = 7 * (SX2 * SX4 - SX3 ^ 2) - SX * (SX * SX4 - SX2 * SX3) + SX2 * (SX * SX3 - SX2 ^ 2)
T2 = SY * (SX2 * SX4 - SX3 ^ 2) - SYX * (SX * SX4 - SX2 * SX3) + SYX2 * (SX * SX3 - SX2 ^ 2)
T3 = 7 * (SYX * SX4 - SYX2 * SX3) - SX * (SY * SX4 - SYX2 * SX2) + SX2 * (SY * SX3 - SYX * SX2)
T4 = 7 * (SYX2 * SX2 - SYX * SX3) - SX * (SYX2 * SX - SY * SX3) + SX2 * (SYX * SX - SY * SX2)
b0 = T2 / DEN
b1 = T3 / DEN
b2 = T4 / DEN
result1 = b1 / C2 + 2 * b2 * (Cells(j + 2, 2) - C1) / (C2 ^ 2)
ai = b0 + b1 * (Cells(j + 2, 2) - C1) / C2 + b2 * ((Cells(j + 2, 2) - C1) / C2) ^ 2
A = ai / W
A2 = 2 * A
result2 = P * (2 + A) * (0.886 + 4.64 * A - 13.32 * A ^ 2 + 14.72 * A ^ 3 - 5.6 * A ^ 4) / (B * (W ^ 0.5) * (1 - A) ^ 1.5)
result3 = P * (3.14 * A2 / (Cos(3.14 * A2 / 2) * 2 * W)) ^ 0.5 / B
[F11] = C1
[F12] = C2
[F14] = SX
[F15] = SX2
[F16] = SX3
[F17] = SX4
[F18] = SY
[F19] = SY2
[F20] = SYX
[F21] = SYX2
[F23] = DEN
[F24] = T2
[F25] = T3
[F26] = T4
[F27] = b0
[F28] = b1
[F29] = b2
[F29] = b2
[F30] = ai
[F33] = result1
[F34] = result2
[F35] = result3
End If
End Sub
Private Sub JudgeNi_Click()
Dim i, j As Integer
Dim temp As Single
i = [F4]
k = [F3]
j = k
temp = 0
bRet = False
temp = Cells(j + 2, 2)
If (((temp - C1) / C2) <= -1) Or (((temp - C1) / C2) >= 1) Then
MsgBox ("N" & "j" & "输入不符合范围")
bRet = True
End If
End Sub
Private Sub CleanResult()
[F11] = ""
[F12] = ""
[F14] = ""
[F15] = ""
[F16] = ""
[F17] = ""
[F18] = ""
[F19] = ""
[F20] = ""
[F21] = ""
[F23] = ""
[F24] = ""
[F25] = ""
[F26] = ""
[F27] = ""
[F28] = ""
[F29] = ""
[F33] = ""
[F34] = ""
[F35] = ""
End Sub
Private Sub CommandButton2_Click()
Range("A2:C2").Select
End Sub
Private Sub Worksheet_Activate()
Range("A2:C2").Select
End Sub
这几个源代码是对应下图的控件的。哪位高手帮小弟看看,是不是控件选错了啊。为什么运行不了、还有最后一个控件在VB中怎么找到啊。谢谢大家了