2017-01-19 1 views
0

나는 다음과 같은 코드를 가지고 :VBA 분할 배열

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 항상 적어도 "0 0"이됩니다 길이가의 AutoCAD의 태그에서 비롯 될 수 있지만 " 0 0 0 "피트, 인치 및 분수 인치 길이입니다. 따라서 가능한 값은 "8 5", "16 11 11/16", "0 5 3/8"등이 될 수 있습니다.

내가 필요한 것은 분수가 없을 때를 확인하는 것입니다.

제안 사항?

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

# :

+0

'Len (yourstring) -len (replace (yourstring, "" ""))> 1'은 세 번째 값이있을 때 true를 반환합니다. –

답변

1

나는 공간에 문자열을 분할하고 결과 배열의 UBound 함수가 있는지 것 2. 그래서이

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

또 다른 옵션과 같이이 Like 운영자입니다 임의의 한 자리 (0-9)와 일치하고 0323 이상의 문자와 일치하는 *과 일치합니다.

하지만 어쨌든 문자열을 분할했기 때문에 변수에 분할 결과를 저장하고 UBound과 같은 항목의 수를 다른 대답과 같이 확인합니다.