返回列表 上一主題 發帖

[發問] 比較一堆資料中規則問題,能否判讀

回復 8# boblovejoyce
資料那麼多筆要先講,上傳的檔案才兩筆就沒有優化了
  1. Sub Test()   
  2.     '== Read File ==
  3.     Dim fn As Integer, allText As String
  4.     Dim fname
  5.     fname = Application.GetOpenFilename()
  6.     If TypeName(fname) = "Boolean" Then Exit Sub
  7.     fn = FreeFile()
  8.     Open fname For Input As #fn
  9.     allText = Input$(LOF(1), 1)
  10.     Close #fn
  11.         
  12.     '== Parse Element ==
  13.     Dim oRegex As Object: Set oRegex = CreateObject("vbscript.regexp")
  14.     Dim oDic As Object: Set oDic = CreateObject("scripting.dictionary")
  15.     Dim oMch As Object, oMch2 As Object, x, i As Long, j As Long, endIndex As Long
  16.     Dim hasVDD As Boolean, hasGND As Boolean
  17.     With oRegex
  18.         .Global = True
  19.         .Pattern = "\(net\s+([^\s()]*)"     '(net 開頭,捕捉下一個非空白或()的字組
  20.         Set oMch = .Execute(allText)
  21.         
  22.         .Pattern = "\(portRef\s+([^\s()]*)"     '(portRef 開頭,捕捉下一個非空白或()的字組
  23.         i = 1
  24.         For Each x In oMch
  25.             endIndex = FindCloseBracket(allText, x.firstIndex)
  26.             If endIndex = 0 Then MsgBox "Fail to parse: " & vbNewLine & Mid(allText, x.firstIndex, 50) & "...": Exit Sub
  27.             Set oMch2 = .Execute(Mid(allText, x.firstIndex, endIndex - x.firstIndex + 1))
  28.             
  29.             hasVDD = False: hasGND = False
  30.             For Each y In oMch2
  31.                 If InStr(1, y.subMatches(0), "VDD", vbTextCompare) Then hasVDD = True
  32.                 If InStr(1, y.subMatches(0), "GND", vbTextCompare) Then hasGND = True
  33.             Next
  34.             
  35.             If oMch2.Count < 2 Or (hasVDD And hasGND) Then  'portRef低於兩個或同時含VDD及GND字串
  36.                 oDic.Add i, x.subMatches(0)
  37.                 i = i + 1
  38.             End If
  39.         Next
  40.     End With
  41.    
  42.     '== Output result ==
  43.     Dim ar
  44.     If oDic.Count = 0 Then MsgBox "All pass": Exit Sub
  45.     ar = oDic.items
  46.     With Sheets.Add
  47.         Application.ScreenUpdating = False
  48.         .[a1].Resize(UBound(ar) - LBound(ar) + 1) = OneDtoTwoD(ar)
  49.         Application.ScreenUpdating = True
  50.     End With
  51. End Sub

  52. '   s: input string
  53. '   i: index of begin bracket "("
  54. Function FindCloseBracket(ByRef s As String, ByVal i As Long) As Long
  55.     Dim isInString As Boolean   'ex : 預防雙引號字串內的 () 誤判
  56.     i = i + 1   '從下一個字元開始
  57.     Do While i <= Len(s)
  58.         Select Case Mid(s, i, 1)
  59.         Case "("
  60.             If Not isInString Then
  61.                 i = FindCloseBracket(s, i)
  62.                 If i = 0 Then Exit Do
  63.             End If
  64.         Case ")"
  65.             If Not isInString Then
  66.                 FindCloseBracket = i
  67.                 Exit Function
  68.             End If
  69.         Case """"
  70.             isInString = Not isInString
  71.         Case Else
  72.         End Select
  73.         i = i + 1
  74.     Loop
  75.     FindCloseBracket = 0
  76. End Function

  77. Function OneDtoTwoD(ar, Optional base As Integer) As Variant    'transepose array of elements > 65535
  78.     Dim i, retn()
  79.     ReDim retn(base To base + UBound(ar) - LBound(ar), base To base)  ' transe to base
  80.     For i = LBound(ar) To UBound(ar)
  81.         retn(i - LBound(ar) + base, base) = ar(i)
  82.     Next
  83.     OneDtoTwoD = retn
  84. End Function
複製代碼
表達不清、題意不明確、沒附檔案格式、沒有討論問題的態度~~~~~~以上愛莫能助。

TOP

回復 11# stillfish00

謝謝大大的回覆~
目前不解的是,為何跑完後,會有這幾個項次?
附上我的讀取檔案
test0311.rar (34.99 KB)
&10104
&10306
&10208
&10205
&10108
&10314
&10414
&10102
&10311
&10204
&10115

test0311.rar (34.99 KB)

TOP

本帖最後由 stillfish00 於 2016-3-11 19:42 編輯

回復 12# boblovejoyce
恩,firstIndex 這邊我錯了,match到第一個字時 firstIndex = 0,和VBA不一樣,難怪跑那麼久,修改如下

然後有些rename的我不知要取哪個名稱,都寫到第一欄
  1. Sub Test()
  2.     '== Read File ==
  3.     Dim fn As Integer, allText As String
  4.     Dim fname
  5.     fname = Application.GetOpenFilename()
  6.     If TypeName(fname) = "Boolean" Then Exit Sub
  7.     fn = FreeFile()
  8.     Open fname For Input As #fn
  9.     allText = Input$(LOF(1), 1)
  10.     Close #fn
  11.         
  12.     '== Parse Element ==
  13.     Dim oRegex As Object: Set oRegex = CreateObject("vbscript.regexp")
  14.     Dim oDic As Object: Set oDic = CreateObject("scripting.dictionary")
  15.     Dim omch As Object, oMch2 As Object, x, i As Long, j As Long, endIndex As Long
  16.     Dim hasVDD As Boolean, hasGND As Boolean
  17.     With oRegex
  18.         .Global = True
  19.         .Pattern = "\(net\s+(\(rename.*\)|[^\s()]*)"     '(net 開頭,捕捉下一個非空白或()的字組或rename...
  20.         Set omch = .Execute(allText)
  21.         
  22.         .Pattern = "\(portRef\s+([^\s()]*)"     '(portRef 開頭,捕捉下一個非空白或()的字組
  23.         i = 1
  24.         For Each x In omch
  25.             endIndex = FindCloseBracket(allText, x.firstIndex + 1)
  26.             If endIndex = 0 Then MsgBox "Fail to parse: " & vbNewLine & Mid(allText, (x.firstIndex + 1), 50) & "...": Exit Sub
  27.             Set oMch2 = .Execute(Mid(allText, x.firstIndex + 1, endIndex - (x.firstIndex + 1) + 1))
  28.             
  29.             hasVDD = False: hasGND = False
  30.             For Each y In oMch2
  31.                 If InStr(1, y.subMatches(0), "VDD", vbTextCompare) Then hasVDD = True
  32.                 If InStr(1, y.subMatches(0), "GND", vbTextCompare) Then hasGND = True
  33.             Next
  34.             
  35.             If oMch2.Count < 2 Or (hasVDD And hasGND) Then  'portRef低於兩個或同時含VDD及GND字串
  36.                 oDic.Add i, x.subMatches(0)
  37.                 i = i + 1
  38.             End If
  39.         Next
  40.     End With
  41.    
  42.     '== Output result ==
  43.     Dim ar
  44.     If oDic.Count = 0 Then MsgBox "All pass": Exit Sub
  45.     ar = oDic.items
  46.     With Sheets.Add
  47.         Application.ScreenUpdating = False
  48.         .[a1].Resize(UBound(ar) - LBound(ar) + 1) = OneDtoTwoD(ar)
  49.         Application.ScreenUpdating = True
  50.     End With
  51. End Sub

  52. '   s: input string
  53. '   i: index of begin bracket "("
  54. Function FindCloseBracket(ByRef s As String, ByVal i As Long) As Long
  55.     Dim isInString As Boolean   'ex : 預防雙引號字串內的 () 誤判
  56.     i = i + 1   '從下一個字元開始
  57.     Do While i <= Len(s)
  58.         Select Case Mid(s, i, 1)
  59.         Case "("
  60.             If Not isInString Then
  61.                 i = FindCloseBracket(s, i)
  62.                 If i = 0 Then Exit Do
  63.             End If
  64.         Case ")"
  65.             If Not isInString Then
  66.                 FindCloseBracket = i
  67.                 Exit Function
  68.             End If
  69.         Case """"
  70.             isInString = Not isInString
  71.         Case Else
  72.         End Select
  73.         i = i + 1
  74.     Loop
  75.     FindCloseBracket = 0
  76. End Function

  77. Function OneDtoTwoD(ar, Optional base As Integer) As Variant    'transepose array of elements > 65535
  78.     Dim i, retn()
  79.     ReDim retn(base To base + UBound(ar) - LBound(ar), base To base)  ' transe to base
  80.     For i = LBound(ar) To UBound(ar)
  81.         retn(i - LBound(ar) + base, base) = ar(i)
  82.     Next
  83.     OneDtoTwoD = retn
  84. End Function
複製代碼
表達不清、題意不明確、沒附檔案格式、沒有討論問題的態度~~~~~~以上愛莫能助。

TOP

回復 13# stillfish00
謝謝大大~調整
測試後,正常又快速~:)

TOP

        靜思自在 : 世上有兩件事不能等:一、孝順 二、行善。
返回列表 上一主題