标题:一个vba宏代码,如何转换成vb语言呢?
只看楼主
shi7361
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2019-7-12
结帖率:60%
已结贴  问题点数:25 回复次数:2 
一个vba宏代码,如何转换成vb语言呢?
原来做个一个复杂判断代码,现在想在vb上重新写一下,这个代码如何转换到vb中呢?
说明一下:

在excel中10-1和10-2中输入数据,以,号相隔,按照国标乱七八糟的判断,在结果中自动计算;代码如下:
Sub 计算()
    Dim d As Object
    Dim m, n, i, j, x, arr, brr, sa, sb, k, t
    Dim ar(1 To 4), br(1 To 5), cr(1 To 4), sr(), ssr(1 To 2, 1 To 4)
    Dim a%, b%, c0%, c1%, s%, p%, p0%, p1%
    Dim l, la, lb, lc
    Set d = CreateObject("scripting.dictionary")
    sa = 10
    sb = 100
    arr = Range("X28:AB30")
    For j = 1 To UBound(arr, 2)
        If Not d.exists(arr(1, j)) Then
            m = 1
            ReDim brr(1 To 4, 1 To m)
        Else
            brr = d(arr(i, 1))
            m = UBound(brr, 2) + 1
            ReDim Preserve brr(1 To 4, 1 To m)
        End If
        brr(1, m) = Split(arr(2, j), ",")(0)
        brr(2, m) = Split(arr(2, j), ",")(1)
        brr(3, m) = Split(arr(3, j), ",")(0)
        brr(4, m) = Split(arr(3, j), ",")(1)
        d(arr(1, j)) = brr
    Next
    k = d.keys
    t = d.items
    For x = 0 To d.Count - 1
        Erase ar
        n = 0: a = 0: b = 0: c0 = 0: c1 = 0: s = 0
        For i = 1 To 4
            n = n + 1
            a = Val(t(x)(i, 1))
            b = b + a      '判断全0
            If a < 30 Then c0 = c0 + 1    '全小于30
            If a > 300 Then c1 = c1 + 1    '全大于300
            If a >= 30 And a <= 300 Then     '判断范围内外
                ar(n) = 1
            Else
                ar(n) = 0
            End If
        Next
        For i = 1 To 4
            s = s + ar(i)
        Next
        If s = 4 Then       '全在范围内--4
            br(x + 1) = (Val(t(x)(1, 1)) + Val(t(x)(2, 1)) + Val(t(x)(3, 1)) + Val(t(x)(4, 1))) / 0.022
        ElseIf s > 0 And s < 4 Then   '只有1-3个在范围内--5
            n = 0
            For i = 1 To 4
                If ar(i) = 1 Then n = i: Exit For
            Next
            If n = 1 Or n = 2 Then
                br(x + 1) = (Val(t(x)(1, 1)) + Val(t(x)(2, 1))) / 2 * sa
            Else
                br(x + 1) = (Val(t(x)(3, 1)) + Val(t(x)(4, 1))) / 2 * sb
            End If
        ElseIf s = 0 Then
            If b = 0 Then     '全0--2
                br(x + 1) = "'<10"
            ElseIf c0 = 4 Then           '全小于30--3
                br(x + 1) = (Val(t(x)(1, 1)) + Val(t(x)(2, 1))) / 2 * sa
            ElseIf c1 = 4 Then           '全大于300--6
                br(x + 1) = (Val(t(x)(3, 1)) + Val(t(x)(4, 1))) / 2 * sb
            Else                     '全不在范围内--1
                Erase sr
                n = 0
                If Abs(300 - (Val(t(x)(1, 1)) + Val(t(x)(2, 1))) / 2) < Abs(300 - (Val(t(x)(3, 1)) + Val(t(x)(4, 1))) / 2 * sa) Then
                        br(x + 1) = (Val(t(x)(1, 1)) + Val(t(x)(2, 1))) / 2 * sa
                        Else
                        br(x + 1) = (Val(t(x)(3, 1)) + Val(t(x)(4, 1))) / 2 * sb
                    End If
            End If
        End If
    Next
    For i = 1 To 5
        If br(i) = "'<10" Then
            br(i) = "'<10"
        ElseIf br(i) = 0 Then
            br(i) = 0
        ElseIf br(i) < 10 Then
            br(i) = Application.WorksheetFunction.RoundDown(br(i), 0)
        ElseIf br(i) < 100 Then
            br(i) = Format(br(i), "0")
        Else
            l = 0: la = 0: lb = 0: lc = 0
            l = Application.WorksheetFunction.RoundDown(br(i), 0)
            la = Left(l, 2)
            lb = Val(Mid(l, 3, 1))
            lc = Val(Len(l)) - 2
            If lb > 4 Then
                br(i) = la + 1 & Application.Rept(0, lc)
            Else
                br(i) = la & Application.Rept(0, lc)
            End If
        End If
    Next
    Range("X32:AB32") = Empty
    [X32].Resize(1, 5) = br
    Set d = Nothing
End Sub

现在在vb’中窗体内插入若干text,代替表格,输入数据后也想自动计算,请老师帮忙!非常感谢!

搜索更多相关主题的帖子: End If vb Then For 
2020-03-19 15:55
shi7361
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2019-7-12
得分:0 
工作簿1.rar (9.17 KB)

可能是我描述的不准确,我把条件拿出来,大家再帮我看看,谢谢
2020-03-20 20:22
HVB6
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:15
帖 子:320
专家分:561
注 册:2013-10-30
得分:18 
VB封装DLL实例讲解
http://club.
(出处: ExcelHome技术论坛)
2020-03-21 19:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-500438-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.069519 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved