標題:
[發問]
該如何某些條件必須符合,某一條件可模糊比對?
[打印本頁]
作者:
tsoo811024
時間:
2020-8-19 14:44
標題:
該如何某些條件必須符合,某一條件可模糊比對?
本帖最後由 tsoo811024 於 2020-8-19 14:45 編輯
小弟碰到一個問題,該如何區、里、路(街)、弄、巷同時符合,門牌號盡量符合地去抓取已知座標值?
例如:烏日區光明里光明路152號,因只有烏日區光明里光明路149號、烏日區光明里光明路153號之已知座標值
能否比對後抓取烏日區光明里光明路153號(最相近之門牌)當作152號之座標值,並標註為模糊比對...
因論壇附件上限1MB,沒辦法上傳,故用google雲端上傳~有勞大神賜教...
https://drive.google.com/file/d/1cUDCHfHE_Pkv9r3bHd2-u6g7Dgc7O_nt/view?usp=sharing
作者:
ikboy
時間:
2020-8-22 14:36
感覺蠻複雜:
Sub zz()
Dim a, b, c(), n&, d As Object, re As Object, aa(), xt As Boolean
Dim dd As Object, s$, p, z(1)
Set re = CreateObject("vbscript.regexp")
Set d = CreateObject("scripting.dictionary")
Set dd = CreateObject("scripting.dictionary")
With Sheets(2)
a = .Range("b2:i" & .[b1048576].End(3).Row)
End With
Application.StatusBar = "Program processing... please wait"
With re
.Pattern = "(\d+-?\d*)"
.Global = True
For i = 1 To UBound(a)
d(a(i, 1)) = i
If .test(a(i, 1)) Then
k = Split(.Replace(a(i, 1), "|$1|"), "|")
n = Val(k(UBound(k) - 1))
s = ""
For j = 0 To UBound(k) - 2 Step 2
s = s & k(j)
Next
If dd.exists(s) Then
p = dd(s)
ReDim Preserve p(UBound(p) + 1)
p(UBound(p)) = n & "|" & i
dd(s) = p
Else
dd(s) = Array(n & "|" & i)
End If
End If
Next
End With
For Each k In dd.keys
p = dd(k)
ReDim b(UBound(p), 1)
For j = 0 To UBound(p)
t = Split(p(j), "|")
b(j, 0) = Val(t(0))
b(j, 1) = Val(t(1))
Next
For i = 0 To UBound(b) - 1
t = b(i, 0)
For j = i + 1 To UBound(b)
If b(j, 0) < t Then t = b(j, 0): jj = j: xt = True
Next
If xt Then
xt = False
For j = 0 To 1
z(j) = b(i, j)
b(i, j) = b(jj, j)
b(jj, j) = z(j)
Next
End If
Next
dd(k) = b
Next
With Sheets(1)
b = .Range("b2:b" & .[b1048576].End(3).Row)
.Cells(2, "h").Resize(UBound(b), 3).Clear
ReDim c(1 To UBound(b), 1 To 3)
For i = 1 To UBound(b)
Application.StatusBar = "Finised " & i & " of " & UBound(b)
n = d(b(i, 1))
If n Then
c(i, 1) = a(n, 7): c(i, 2) = a(n, 8)
Else
c(i, 3) = "模糊比對"
GoSub FC
If dd.exists(s) Then
p = dd(s)
If n Then
If UBound(p) Then
If n < p(0, 0) Then n = p(0, 0)
n = Application.VLookup(n, dd(s), 2, 1)
Else
n = dd(s)(0, 1)
End If
c(i, 1) = a(n, 7): c(i, 2) = a(n, 8)
End If
End If
End If
Next
Cells(2, "H").Resize(i - 1, 3) = c
End With
Debug.Print dd.Count
Application.StatusBar = False
End
FC:
With re
If .test(b(i, 1)) Then
k = Split(.Replace(b(i, 1), "|$1|"), "|")
n = Val(k(UBound(k) - 1))
s = ""
For j = 0 To UBound(k) - 2 Step 2
s = s & k(j)
Next
End If
End With
Return
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)