笨方法
N欄要留空白
Sub test()
Columns("O:AA").ClearContents
For r = 1 To 10 '到第10列
cc = 1
ccc = 0
For c = 2 To 14 '最後一欄+1
If Cells(r, c) - Cells(r, c - 1) <> 1 Then
ccc = ccc + 1
For i = cc To c - 1
Cells(r, ccc + 14) = Cells(r, ccc + 14) & "," & Cells(r, i) '資料從最後一欄+2開始放
Next
cc = c
End If
Next
Next
End Sub作者: quickfixer 時間: 2021-3-29 15:46
[attach]33156[/attach]
修正一下,只列出連續數字
Sub test2()
Columns("O:AA").ClearContents
For r = 1 To 10
cc = 1
ccc = 0
For c = 2 To 14
If Cells(r, c) - Cells(r, c - 1) <> 1 Then
s = ""
For i = cc To c - 1
s = s & "," & Cells(r, i)
Next
If UBound(Split(s, ",")) <> 1 Then
ccc = ccc + 1
Cells(r, ccc + 14) = s
End If
cc = c
End If
Next
Next
End Sub作者: ML089 時間: 2021-3-30 14:21
試試看
Sub ex()
Dim Arr As Variant, C As Variant, X%, Y%
[A10].CurrentRegion.ClearContents '資料放置位置,清除資料(請自行調整)
Arr = [a1].CurrentRegion
Set C = Nothing
For X = 1 To UBound(Arr)
For Y = 1 To UBound(Arr, 2) - 1
If Cells(X, Y) - Cells(X, Y + 1) = -1 Then '判斷是否為連續數值
If C Is Nothing Then
Set C = Cells(X, Y).Resize(, 2)
Else
Set C = Union(C, Cells(X, Y).Resize(, 2))
End If
End If
Next
C.Copy [A10].Offset(X).Resize(, C.Count) '資料放置位置(請自行調整)
Set C = Nothing
Next
End Sub作者: samwang 時間: 2021-3-31 08:20
不知道是否為樓主需求,請測試看看,謝謝。
Sub tt()
Dim Arr, Brr(), T%, T1%, L%, M%, i&, j&, C%
Columns("O:AA").ClearContents
Arr = [a1].CurrentRegion
ReDim Brr(1 To UBound(Arr), 1 To UBound(Arr, 2))
For i = 1 To UBound(Arr)
M = 0: L = 0: C = 0
For j = 1 To UBound(Arr, 2)
If j + 1 > UBound(Arr, 2) Then
If T1 = T + 1 Then Brr(i, M) = Mid(Brr(i, M) & "," & Arr(i, j), 2)
Exit For
End If
T = Arr(i, j): T1 = Arr(i, j + 1)
If T1 = T + 1 Then
If C = 0 Then M = M + 1
Brr(i, M) = Brr(i, M) & "," & Arr(i, j): C = 1
Else
If T > L + 1 Then GoTo 99
Brr(i, M) = Mid(Brr(i, M) & "," & Arr(i, j), 2): C = 0
End If
L = T
99: Next
Next
Range("o1").Resize(UBound(Brr), UBound(Brr, 2)) = Brr
End Sub作者: f00l01 時間: 2021-3-31 10:07
連續數字單獨個別放在單一儲存格,請測試看看,謝謝。
Sub tt1()
Dim Arr, Brr(), T%, T1%, L%, M%, i&, j&
Columns("O:AA").ClearContents
Arr = [a1].CurrentRegion
ReDim Brr(1 To UBound(Arr), 1 To UBound(Arr, 2))
For i = 1 To UBound(Arr)
M = 0: L = 0
For j = 1 To UBound(Arr, 2)
If j + 1 > UBound(Arr, 2) Then
If T1 = T + 1 Then M = M + 1: Brr(i, M) = Arr(i, j)
Exit For
End If
T = Arr(i, j): T1 = Arr(i, j + 1)
If T1 = T + 1 Then
M = M + 1: Brr(i, M) = Arr(i, j)
Else
If T > L + 1 Then GoTo 99
M = M + 1: Brr(i, M) = Arr(i, j)
End If
L = T
99: Next
Next
Range("o1").Resize(UBound(Brr), UBound(Brr, 2)) = Brr
End Sub作者: 准提部林 時間: 2021-3-31 18:22
Sub TEST()
Dim Arr, V&, U1&, U2&, i&, j%, T$
Arr = Range([A1], [m65536].End(xlUp))
For i = 1 To UBound(Arr)
V = -9 ^ 9
For j = 1 To UBound(Arr, 2)
U1 = Arr(i, j): U2 = U1
If j < UBound(Arr, 2) Then U2 = Arr(i, j + 1)
If U1 - V = 1 Or U2 - U1 = 1 Then T = T & "," & U1: V = U1
Next j
Arr(i, 1) = Mid(T, 2): T = ""
Next i
[O1].Resize(UBound(Arr)) = Arr
End Sub作者: samwang 時間: 2021-4-1 09:52
全部連續值顯示在同一格儲存格如照片,請測試看看,謝謝。
Sub tt2()
Dim Arr, TT, T%, T1%, L%, i&, j&
[o1].CurrentRegion = ""
Arr = [a1].CurrentRegion
For i = 1 To UBound(Arr)
L = 0
For j = 1 To UBound(Arr, 2)
If j < UBound(Arr, 2) Then T1 = Arr(i, j + 1)
T = Arr(i, j)
If T1 - T = 1 Or T - L = 1 Then
TT = TT & "," & Arr(i, j): L = T
End If
Next
Arr(i, 1) = Mid(TT, 2): TT = ""
Next
Range("o1").Resize(UBound(Arr)) = Arr
End Sub作者: 准提部林 時間: 2021-4-1 13:39
Sub tt3()
Dim Arr, TT, T%, T1%, L&, i&, j&
[o1].CurrentRegion = ""
Arr = [a1].CurrentRegion
For i = 1 To UBound(Arr)
L = 9 ^ 9
For j = 1 To UBound(Arr, 2)
If j < UBound(Arr, 2) Then T1 = Arr(i, j + 1)
T = Arr(i, j)
If T1 - T = 1 Or T - L = 1 Then
TT = TT & "," & T: L = T
End If
Next
Arr(i, 1) = Mid(TT, 2): TT = ""
Next
Range("o1").Resize(UBound(Arr)) = Arr
End Sub作者: f00l01 時間: 2021-4-22 18:01
Option Explicit
Sub TEST_2()
Dim Arr, i&, j&, T, V, Y, U1, U2, S
S = Timer
Set Y = CreateObject("Scripting.Dictionary")
Arr = Range([A1], [m65536].End(xlUp))
[O:O].ClearContents
For i = 1 To UBound(Arr)
V = -9 ^ 9
For j = 1 To UBound(Arr, 2)
If j < UBound(Arr, 2) Then
U1 = Arr(i, j): U2 = Arr(i, j + 1)
If U1 * U2 >= 0 Then
Y(U1) = IIf(Y(U1) <> 1, Abs((U2) - (U1)), Y(U1))
Y(U2) = IIf(Y(U2) <> 1, Abs((U2) - (U1)), Y(U1))
End If
End If
Next
For Each T In Y.KEYS
If Y(T) <> 1 Then
Y.Remove (T)
End If
Next
Arr(i, 1) = Join(Application.Transpose(Application.Transpose(Y.KEYS)), ",")
Y.RemoveAll
Next
[O1].Resize(UBound(Arr)) = Arr
MsgBox Timer - S & "秒"
End Sub作者: Andy2483 時間: 2022-10-31 13:59
字典key轉置的方式改成迴圈直接寫入陣列少!稍好!
[attach]35436[/attach]
Option Explicit
Sub TEST_3()
Dim Arr, i&, j&, T, V, Y, U1, U2, S
S = Timer
Set Y = CreateObject("Scripting.Dictionary")
Arr = Range([A1], [m65536].End(xlUp))
[O:O].ClearContents
For i = 1 To UBound(Arr)
V = -9 ^ 9
For j = 1 To UBound(Arr, 2)
If j < UBound(Arr, 2) Then
U1 = Arr(i, j): U2 = Arr(i, j + 1)
If U1 * U2 >= 0 Then
Y(U1) = IIf(Y(U1) <> 1, Abs((U2) - (U1)), Y(U1))
Y(U2) = IIf(Y(U2) <> 1, Abs((U2) - (U1)), Y(U1))
End If
End If
Next
Arr(i, 1) = ""
For Each T In Y.KEYS
If Y(T) <> 1 Then
Y.Remove (T)
Else
Arr(i, 1) = Arr(i, 1) & "," & T
End If
Next
Arr(i, 1) = Mid(Arr(i, 1), 2)
Y.RemoveAll
Next
[O1].Resize(UBound(Arr)) = Arr
MsgBox Timer - S & "秒"
End Sub作者: Andy2483 時間: 2022-10-31 16:30
深入研究耗時: 單就判斷與建立字典庫!就耗時0.6秒
Option Explicit
Sub TEST_4()
Dim Arr, i&, j&, T, V, Y, U1, U2, S, P$
S = Timer
Set Y = CreateObject("Scripting.Dictionary")
Arr = Range([A1], [m65536].End(xlUp))
[O:O].ClearContents
For i = 1 To UBound(Arr)
V = -9 ^ 9
For j = 1 To UBound(Arr, 2)
If j < UBound(Arr, 2) Then
U1 = Arr(i, j): U2 = Arr(i, j + 1)
If U1 * U2 >= 0 Then
Y(U1) = IIf(Y(U1) <> 1, Abs((U2) - (U1)), Y(U1))
Y(U2) = IIf(Y(U2) <> 1, Abs((U2) - (U1)), Y(U1))
End If
End If
Next
Y.RemoveAll
Next
MsgBox Timer - S & "秒"
End Sub
去除IIF判斷! 0.3秒!
Option Explicit
Sub TEST_4()
Dim Arr, i&, j&, T, V, Y, U1, U2, S, P$
S = Timer
Set Y = CreateObject("Scripting.Dictionary")
Arr = Range([A1], [m65536].End(xlUp))
[O:O].ClearContents
For i = 1 To UBound(Arr)
V = -9 ^ 9
For j = 1 To UBound(Arr, 2)
If j < UBound(Arr, 2) Then
U1 = Arr(i, j): U2 = Arr(i, j + 1)
If U1 * U2 >= 0 Then
Y(U1) = ""
Y(U2) = ""
End If
End If
Next
Y.RemoveAll
Next
MsgBox Timer - S & "秒"
End Sub
光是反覆字典建立/清空!就要 0.25秒!
Option Explicit
Sub TEST_4()
Dim Arr, i&, j&, T, V, Y, U1, U2, S, P$
S = Timer
Set Y = CreateObject("Scripting.Dictionary")
Arr = Range([A1], [m65536].End(xlUp))
[O:O].ClearContents
For i = 1 To UBound(Arr)
V = -9 ^ 9
For j = 1 To UBound(Arr, 2)
If j < UBound(Arr, 2) Then
U1 = Arr(i, j)
Y(U1) = ""
End If
Next
Y.RemoveAll
Next
MsgBox Timer - S & "秒"
End Sub