0

I have the following code:

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

The variable fraction / fracVal comes from a tag in AutoCAD that is a length, that will always be at least "0 0", but may be "0 0 0" it is a length in feet, inches, and fractional inches. So some possible values could be "8 5", "16 11 11/16", "0 5 3/8" etc.

What I need is a check for when the fraction is not there.

Any suggestions?

1
  • Len(yourstring)-len(replace(yourstring," ",""))>1 will return true when there is a third value. Commented Jan 19, 2017 at 21:26

2 Answers 2

1

I would split the string on the space and see if the ubound of the resulting array is 2. So something like this

If Ubound(Split(thisString, " ")) = 2 then
    'fractional part is present
End If
Sign up to request clarification or add additional context in comments.

Comments

0

Another option is the Like Operator:

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

# matches any single digit (0–9) and * matches zero or more characters.

but since you split the string anyway, I would store the result of the split in a variable and check the number of items in it with UBound as shown in the other answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.