2017-01-19 10 views
0

У меня есть следующий код:VBA Split массив

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 

Переменная фракция/fracVal приходит из тега в AutoCAD, которая является длиной, которая всегда будет по крайней мере, «0 0», но может быть " 0 0 0 "это длина в футах, дюймах и дробных дюймах. Таким образом, некоторые возможные значения могут быть «8 5», «16 11 11/16», «0 5 3/8» и т. Д.

Мне нужна проверка, когда фракции там нет.

Любые предложения?

+0

'Len (yourstring) -len (заменить (yourstring," "," "))> 1' вернет true, если есть третье значение. –

ответ

1

Я бы разделить строку на место и посмотреть, если UBound результирующего массива 2. Так что-то вроде этого

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

Другим вариантом является Like Оператор:

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

# соответствует любой отдельной цифре (0-9) и * соответствует ноль или более символов.

но так как вы все равно разделили строку, я бы сохранил результат разделения в переменной и проверил количество элементов в нем с UBound, как показано в другом ответе.