Dim LdZ, i As Integer, ld As Integer
LdZ = Array(400, 450, 500, 560, 630, 710, 800, 900, 1000, 1120, 1250, 1400, 1600, 1800)ld = Val(Text1.txet)For i = LBound(LdZ) To UBound(LdZ)
If LdZ(i) - ld < LdZ(i + 1) - ld Then
ld = LdZ(i)
Else: ld = LdZ(i + 1)
Next i
MsgBox ld
我想实现的功能是:
在文本框内输入ld的值,让ld与LdZ数组里的数做比较,找到最接近ld的值,将其输出。
可是为什么说“未找到方法和数据成员”,各位大侠帮帮我,要怎么改才行啊

解决方案 »

  1.   

    Private Sub Form_Load()
    Dim LdZ, i As Integer, ld As Integer
    LdZ = Array(400, 450, 500, 560, 630, 710, 800, 900, 1000, 1120, 1250, 1400, 1600, 1800)ld = Val(Text1.Text)ld0 = LdZ(0)
    For i = LBound(LdZ) To UBound(LdZ)
    If Abs(ld0 - ld) > Abs(LdZ(i) - ld) Then
    ld0 = LdZ(i)
    End If
    Next i
    MsgBox ld0
    End Sub
      

  2.   

    放错过程了。。Private Sub Command1_Click()Dim LdZ, i As Integer, ld As Integer
    LdZ = Array(400, 450, 500, 560, 630, 710, 800, 900, 1000, 1120, 1250, 1400, 1600, 1800)ld = Val(Text1.Text)ld0 = LdZ(0)
    For i = LBound(LdZ) To UBound(LdZ)
    If Abs(ld0 - ld) > Abs(LdZ(i) - ld) Then
    ld0 = LdZ(i)
    End If
    Next i
    MsgBox ld0
    End Sub
      

  3.   

    要找接近的值应该用绝对值比较,另外,你的这个要比较的"原值"一直在变,错误之至.
    Private Sub Command1_Click()
        Dim LdZ, i As Integer, ld As Integer
        LdZ = Array(400, 450, 500, 560, 630, 710, 800, 900, 1000, 1120, 1250, 1400, 1600, 1800)
        
        ld = LdZ(0)
        For i = 1 To UBound(LdZ)
            If Abs(LdZ(i) - Val(Text1.Text)) < Abs(ld - Val(Text1.Text)) Then
                ld = LdZ(i)
            End If
        Next
        MsgBox ld
    End Sub
      

  4.   


    Option ExplicitDim LdZ(0 To 13) As Integer
    Dim i As Integer
    Dim ld As Integer
    Dim intMinNo As Integer '记录最小差的那个数据的编号
    Dim intMin As Integer   '最小差值Private Sub Command1_Click()
        ld = Val(Text1.Text)
        intMinNo = 0
        intMin = Abs(LdZ(0) - ld)
        For i = 1 To 13
            If Abs(LdZ(i) - ld) < intMin Then
                intMinNo = i
                intMin = Abs(LdZ(i) - ld)
            End If
        Next i
        MsgBox "最小偏差的数是数组的第:" & CStr(intMinNo) & "个元素,偏差值:" & intMin
    End SubPrivate Sub Form_Load()
    '    LdZ = 400, 450, 500, 560, 630, 710, 800, 900, 1000, 1120, 1250, 1400, 1600, 1800
        LdZ(0) = 400
        LdZ(1) = 450
        LdZ(2) = 500
        LdZ(3) = 560
        LdZ(4) = 630
        LdZ(5) = 710
        LdZ(6) = 800
        LdZ(7) = 900
        LdZ(8) = 1000
        LdZ(9) = 1120
        LdZ(10) = 1250
        LdZ(11) = 1400
        LdZ(12) = 1600
        LdZ(13) = 1800
    End Sub