VBA Split数组

时间:2017-01-19 21:24:02

标签: vba autocad

我有以下代码:

Sub UpdateBlock()

'Define empty variables for each attribute
Dim ent As AcadEntity
Dim oBkRef As AcadBlockReference
Dim Insertpoints As Variant
Dim A As Double
Dim tag As String
Dim material As String
Dim actualLength As String
Dim cutOff As Double
Dim cutLengths As Double
Dim totalLengths As Double
Dim weight As Double
Dim purchaseLength As Double
Dim decimalLength As Double
Dim lengthWeight As Double
Dim totalLengthWeight As Double
Dim cutLengthWeight As Double
Dim cutWeight As Double
Dim order As Double
Dim feet As Double
Dim inches As Double
Dim fraction As Double
Dim fracVal As Variant

'First we go over every object in the modelspace
For Each ent In ThisDrawing.ModelSpace
    'Check if the object is a block
    If ent.ObjectName = "AcDbBlockReference" Then
        Set oBkRef = ent
        'If the object is a block then check if its the block we are looking for
        If oBkRef.EffectiveName = "AUTOTAG-MATERIAL" Then
            A = A + 1
            'Get Current Attributes
            attlist = oBkRef.GetAttributes
            For i = LBound(attlist) To UBound(attlist)
                Select Case attlist(i).TagString
                    Case "ACTUAL-LENGTH"
                        actualLength = attlist(i).TextString
                    Case "PURCHASE-LENGTH"
                        purchaseLength = attlist(i).TextString
                    Case "CUT-OFF"
                        cutOff = Frac2Num(attlist(i).TextString)
                    Case "DECIMAL-LENGTH"
                        feet = Split(actualLength)(0)
                        inches = Split(actualLength)(1)
                        fracVal = Split(actualLength)(2)

                        If Not IsNull(Split(actualLength)(2)) Then
                            fraction = Frac2Num(fracVal)
                        Else
                            fraction = 0
                        End If

                        decimalLength = Round((((feet * 12) + (inches + fraction)) / 12) - cutOff, 2)
                        attlist(i).TextString = decimalLength
                    Case "WEIGHT"
                        weight = attlist(i).TextString
                    Case "CUT-WEIGHT"
                        cutWeight = weight * decimalLength
                        attlist(i).TextString = cutWeight
                    Case "LENGTH-WEIGHT"
                        lengthWeight = weight * purchaseLength
                        attlist(i).TextString = lengthWeight
                    Case "TOTAL-LENGTHS"
                        totalLengths = attlist(i).TextString
                    Case "CUT-LENGTHS"
                        cutLength = attlist(i).TextString
                    Case "TOTAL-LENGTH-WEIGHT"
                        totalLengthWeight = lengthWeight * totalLengths
                        attlist(i).TextString = totalLengthWeight
                    Case "CUT-LENGTH-WEIGHT"
                        totalCutWeight = lengthWeight * cutLength
                        attlist(i).TextString = totalCutWeight
                End Select
            Next
        End If
    End If
Next ent
End Sub
Function Frac2Num(ByVal X As String) As Double
  Dim P As Integer, N As Double, Num As Double, Den As Double
     X = Trim$(X)
     P = InStr(X, "/")
     If P = 0 Then
        N = Val(X)
     Else
        Den = Val(Mid$(X, P + 1))
        If Den = 0 Then Error 11    ' Divide by zero
        X = Trim$(Left$(X, P - 1))
        P = InStr(X, " ")
        If P = 0 Then
           Num = Val(X)
        Else
           Num = Val(Mid$(X, P + 1))
           N = Val(Left$(X, P - 1))
        End If
     End If
     If Den <> 0 Then
        N = N + Num / Den
     End If
     Frac2Num = N
  End Function

变量fraction / fracVal来自AutoCAD中的标签,该标签的长度始终至少为“0 0”,但可能为“0 0 0”,它是以英尺,英寸和分数英寸为单位的长度。所以一些可能的值可能是“8 5”,“16 11 11/16”,“0 5 3/8”等。

我需要的是检查分数何时不存在。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

我会在空格上拆分字符串,看看生成的数组的ubound是否为2.所以这样的事情

If Ubound(Split(thisString, " ")) = 2 then
    'fractional part is present
End If

答案 1 :(得分:0)

另一个选项是Like运营商:

If thisString Like "#* #* #*/#*" Then

#匹配任何单个数字(0-9),*匹配零个或多个字符。

但是既然你要分割字符串,我会将分割的结果存储在一个变量中,并用UBound检查其中的项目数,如另一个答案所示。