Dear 大大
我用一個很笨的方式來處理當DATA無資料時,所產生的問題
不知道大大是否有其他方式
這是我用大大的程式碼修改 (紅色為我新增的地方)
Dim Ay()
Set d = CreateObject("Scripting.Dictionary")
With Sheet2
ar = .Range(.[B5], .[B5].End(xlDown).Offset(, 2))
If UBound(ar, 1) = 0 Or UBound(ar, 1) = 65536 - 4 Then
For i = 1 To 1
mystr1 = Join(Application.Index(ar, i))
d(mystr1) = d.Count
Next
Else For i = 1 To UBound(ar, 1)
mystr1 = Join(Application.Index(ar, i))
d(mystr1) = d.Count
Next
End If With Sheet1
ar = .Range(.[B5], .[B5].End(xlDown).Offset(, 6))
For i = 1 To UBound(ar, 1)
mystr1 = Join(Array(ar(i, 1), ar(i, 2), ar(i, 6)))
If d.exists(mystr1) = False Then
ReDim Preserve Ay(s)
Ay(s) = Array(ar(i, 1), ar(i, 2), ar(i, 6), ar(i, 7))
s = s + 1
End If
Next
End With
If s > 0 Then .[B65536].End(xlUp).Offset(1, 0).Resize(s, 4) = Application.Transpose(Application.Transpose(Ay))
End With作者: Hsieh 時間: 2011-1-4 20:03
Option Explicit
Sub TEST()
Dim Ay(), d, d1, ar, i, a, s, mystr1
'↑宣告變數:Ay是陣列,其餘為通用型變數
Set d = CreateObject("Scripting.Dictionary")
'↑令d是 字典
Set d1 = CreateObject("Scripting.Dictionary")
'↑令d1也是 字典
With Sheet2
'↑以下是關於Sheet2工作表的程序 (Data表)
.Unprotect "1234"
'↑令以"1234"密碼取消保護工作表
ar = .Range(.[B5], .[B65536].End(xlUp).Offset(, 2))
'↑令ar變數是二維陣列,以[B5]到(B欄最後一個有內容儲存格,
'再向右偏移2欄的儲存格),此範圍儲存格值帶入ar陣列中
For i = 1 To UBound(ar, 1)
'↑設順迴圈!i從1到 ar縱向最大索引列號
mystr1 = Join(Array(ar(i, 1), ar(i, 2), ar(i, 3)))
'↑令mystr1變數是以 空白字元連結陣列子值的新字串
'陣列子值:i迴圈數的(1,2,3)欄ar陣列值
d(mystr1) = d.Count
'↑令以mystr1變數當key,item是 d字典key數量(PS:起始值是0),納入d字典
Next
With Sheet1
'↑以下是關於Sheet1工作表的程序 (輸入表)
ar = .Range(.[B5], .[B65536].End(xlUp).Offset(, 6))
'↑令ar變數裝新資料:
'令ar是二維陣列,以[B5]到(B欄最後一個有內容儲存格,
'再向右偏移6欄的儲存格),此範圍儲存格值帶入ar陣列中
For i = 1 To UBound(ar, 1)
'↑設順迴圈!i從1到 ar縱向最大索引列號
mystr1 = Join(Array(ar(i, 1), ar(i, 2), ar(i, 6)))
'↑令mystr1變數是以 空白字元連結陣列子值的新字串
'陣列子值:i迴圈數的(1,2,6)欄ar陣列值
If d.exists(mystr1) = False Then
'↑如果查d字典裡沒有 mystr1變數 key
ReDim Preserve Ay(s)
'↑令Ay陣列擴充列數到索引號s(PS:s起始值是0)
Ay(s) = Array(ar(i, 1), ar(i, 2), ar(i, 6), ar(i, 7))
'↑令s變索引號Ay陣列值是一維陣列,以i迴圈列ar陣列子值(1,2,6,7)帶入
s = s + 1
'↑令s變數累加 1
Else
d1(mystr1) = ar(i, 7)
'↑否則令以mystr1變數為key,item是i迴圈列第7欄ar陣列值,納入d1字典中
End If
Next
End With
For Each a In .Range(.[B5], .[B65536].End(xlUp))
'↑設逐項迴圈!令a是 (Data表[B5]到 B欄最後一個有內容儲存格)這範圍儲存格之一
mystr1 = Join(Array(a, a.Offset(, 1), a.Offset(, 2)))
'↑令mystr1變數是以 空白字元連結陣列子值的新字串
'陣列子值:a變數值,a變數往右偏移1格的儲存格值,a變數往右偏移2格的儲存格值
a.Offset(, 3) = d1(mystr1)
'↑令a變數往右偏移3格的儲存格值是 以mystr1變數查d1字典的item值
Next
If s > 0 Then
'↑如果s變數大於 0?
.[B65536].End(xlUp).Offset(1, 0).Resize(s, 4) = _
Application.Transpose(Application.Transpose(Ay))
'↑令Data表B欄第1空白格擴展向下s1變數列,向右擴展4欄,
'這擴展範圍儲存格值以Ay陣列轉置兩次的值帶入
End If
.Protect "1234"
'↑令以"1234"密碼保護Data表
End With
End Sub作者: Andy2483 時間: 2023-3-16 10:33
Option Explicit
Sub TEST_1()
Dim A, B, V, Y, Z, C%, R&, i&, N&, T$, xR
Set Y = CreateObject("Scripting.Dictionary")
[Data!F:F].ClearContents
Set xR = Range([Data!F5], [Data!B65536].End(3))
A = xR: B = Range([輸入!H5], [輸入!B65536].End(3))
ReDim V(UBound(B), 4): Z = Array(1, 2, 6, 7)
For i = 1 To UBound(A)
T = Join(Array(A(i, 1), A(i, 2), A(i, 3)), "|")
Y(T) = i: Y(T & "/c4") = A(i, 4)
Next
For i = 1 To UBound(B)
T = Join(Array(B(i, 1), B(i, 2), B(i, 6)), "|")
If Y.Exists(T) Then
N = Y(T)
If B(i, 7) <> Y(T & "/c4") And N <= UBound(A) Then
A(N, 5) = Date & "_" & A(N, 4) & "_修改為_" & B(i, 7)
A(N, 4) = B(i, 7)
End If
Else
For C = 0 To 3: V(R, C) = B(i, Z(C)): Next
V(R, 4) = "新增"
R = R + 1: Y(T) = i: Y(T & "/c4") = B(i, 7)
End If
Next
xR.Value = A
If R > 0 Then xR.Item(xR.Count + 1).Resize(R, 5) = V
Application.Goto [Data!A1]
Set Y = Nothing: Erase A, B, V
End Sub作者: Andy2483 時間: 2023-3-17 09:16
Option Explicit
Sub TEST_1()
If [輸入!B65536].End(3).Row <= 5 Then Exit Sub
'↑如果輸入表B欄最後一個有內容儲存格列號<=5!就結束程式執行
Dim A, B, V, Y, Z, C%, R&, i&, N&, T$, xR As Range
'↑宣告變數:(A,B,V,Y,Z)是通用型變數,C是短整數變數,
'(R,i,N)是長整數變數,T是字串變數,xR是儲存格變數
Set Y = CreateObject("Scripting.Dictionary")
'↑令Y這通用型變數是 字典
[Data!F:F].ClearContents
'↑令Data表F欄清除內容
Set xR = Range([Data!F4], [Data!B65536].End(3))
'↑令xR這儲存格變數是 Data表[F4]到B欄最後一個有內容儲存格
A = xR: B = Range([輸入!H5], [輸入!B65536].End(3))
'↑令A這通用型變數是 二維陣列,以xR變數值(儲存格值)帶入,
'令B這通用型變數是 二維陣列,以輸入表[H5]到B欄最後有內容儲存格,
'這範圍儲存格值帶入B陣列中
ReDim V(UBound(B), 4): Z = Array(1, 2, 6, 7)
'↑宣告V這通用型變數是 二維陣列,縱向範圍從0到 B陣列縱向最大列號,
'橫向範圍從0到 4
'令Z這通用型變數是 一維陣列,以數字(1,2,6,7)為其陣列值
For i = 2 To UBound(A)
'↑設順迴圈!i從2到 A陣列縱向最大索引列號
T = Join(Array(A(i, 1), A(i, 2), A(i, 3)), "|")
'↑令T這字串變數是 以"|"連結A陣列值的新字串,
'A陣列值:i迴圈列(1,2,3)欄A陣列值
Y(T) = i: Y(T & "/c4") = A(i, 4)
'↑令以T變數為key,item是 i迴圈數,納入Y字典裡
'令以T變數連結"/c4"後的新字串為key,item是 i迴圈列第4欄A陣列值,
'納入Y字典裡
Next
For i = 1 To UBound(B)
'↑設順迴圈!i從2到 B陣列縱向最大索引列號
T = Join(Array(B(i, 1), B(i, 2), B(i, 6)), "|")
'↑令T這字串變數是 以"|"連結B陣列值的新字串,
'B陣列值:i迴圈列(1,2,6)欄B陣列值
If Y.Exists(T) Then
'↑如果Y字典裡有 T變數這key?
N = Y(T)
'↑令N這長整數變數是 以T變數查Y字典回傳的item值
If B(i, 7) <> Y(T & "/c4") And N <= UBound(A) Then
'↑如果i迴圈列第7欄陣列值不等於 以T變數連結"/c4"的Y item值
'而且N變數<= A陣列縱向最大索引列號(目的:隔離新增)
A(N, 5) = Date & "_" & A(N, 4) & "_修改為_" & B(i, 7)
'↑令N變數列第5欄A陣列值是 今天日期連接"_",
'再連接N變數列第4欄A陣列值,續連接"_修改為_",
'最後連接i迴圈列第7欄B陣列值
A(N, 4) = B(i, 7)
'↑令N變數列第4欄A陣列值是 i迴圈列第7欄B陣列值
End If
Else
For C = 0 To 3: V(R, C) = B(i, Z(C)): Next
'↑設順迴圈C從0到 3
'令R這長整數變數列第C變數欄V陣列值是
'i迴圈列第(C變數索引號Z陣列值)欄的B陣列值
'R變數的初始值是0,V縱向索引號起始值也是0,要搭配才對得準
V(R, 4) = "新增"
'↑令R變數列第4欄V陣列值是 "新增"字串
R = R + 1: Y(T) = i: Y(T & "/c4") = B(i, 7)
'↑令R變數累加1:令以T變數當key,item是 i迴圈數,納入Y字典裡,
'令以T變數連結"/c4"後的新字串為key,item是 i迴圈列第7欄B陣列值,
'納入Y字典裡
End If
Next
xR = A
'↑令xR變數(儲存格)值 以A陣列值帶入
If R > 0 Then xR.Item(xR.Count + 1).Resize(R, 5) = V
'↑如果R變數>0 !
'就令xR變數(儲存格)的下一個儲存格 擴展向下R變數列,向右擴展5欄,
'這範圍儲存格值以V陣列值帶入
Application.Goto [Data!A1]
'↑令儲存格游標跳到 [Data!A1]位置
Set Y = Nothing: Erase A, B, V, Z
'↑令釋放變數
End Sub