Sub test()
Dim d, R%
d = Sheets("VBA").[Af2]
R = [北區!a65536].End(3).Row
With [北區!k3].Resize(R - 2)
.Formula = "=K2+G3-F3-H3-I3+J3"
'.Value = .Value '轉為值
End With
End Sub作者: PJChen 時間: 2021-9-23 19:34
Sub test()
Dim Arr, d, R%
d = Sheets("VBA").[Af2]
R = [北區!b65536].End(3).Row
With Sheets("北區").Range("a1:k" & R)
Arr = .Value
For i = 3 To UBound(Arr)
.Cells(i, "a").Formula = "=YEAR(RC[1]) & "".."" & MONTH(RC[1])"
If Arr(i, 2) >= d Then
.Cells(i, "k").Formula = "=R[-1]C+RC[-4]-RC[-5]-RC[-3]-RC[-2]+RC[-1] "
End If
Next
'.Value = .Value '轉為值
End With
End Sub作者: samwang 時間: 2021-9-24 10:31
請測試看看,謝謝
Sub 刪除列()
Dim Arr, i&, j%, n%
With Sheets("北區")
With Range(.[k3], .[a65536].End(3))
Arr = .Value
For i = 1 To UBound(Arr)
If Arr(i, 3) <> "美" Then GoTo 99
n = n + 1
For j = 1 To UBound(Arr, 2)
Arr(n, j) = Arr(i, j)
Next
99: Next
.Clear
End With
With Range("a3").Resize(n, UBound(Arr, 2))
.Value = Arr
.Borders(xlBottom).Weight = xlHairline
End With
End With
End Sub作者: samwang 時間: 2021-9-24 10:45
Sub 刪除列2()
Dim xR As Range, xU As Range
For Each xR In Range("c3:c" & [c65536].End(3).Row).Rows
If IsError(Application.Match("美", xR, 0)) Then
If xU Is Nothing Then Set xU = xR Else Set xU = Union(xR, xU)
End If
Next
If Not xU Is Nothing Then xU.EntireRow.Delete
End Sub作者: PJChen 時間: 2021-9-24 20:32
Sub test()
Dim Arr, d, R%
d = Sheets("VBA").[Af2]
R = [北區!b65536].End(3).Row
With Sheets("北區").Range("a1:k" & R)
With [a3].Resize(R - 2)
.Formula = "=YEAR(B3) & "".."" & MONTH(B3)"
End With
.Value = .Value '轉為值
Arr = .Value
For i = 3 To UBound(Arr)
If Arr(i, 2) >= d Then
.Cells(i, "k").Formula = "=k" & i - 1 & "+G" & i & "-F" & i & "-H" & i & "-I" & i & "+j" & i
End If
Next
'.Value = .Value '轉為值
End With
End Sub作者: samwang 時間: 2021-9-25 16:14
回復 11#samwang
您是指K3=K2+G3-F3-H3-I3+J3的公式嗎?
這是為了讓回覆者知道要如何計算
如果是指這個的話,因為我不會其他寫法,用了一段時間後,又要想學其他方式,
For Each xR In Range([B3], [b65535].End(3))
If xR >= d Then 'k+g-f-h-i+j
xR.Offset(, 9) = xR.Offset(-1, 9) + xR.Offset(, 5) - xR.Offset(, 4) - xR.Offset(, 6) - xR.Offset(, 7) + xR.Offset(, 8)
End If
Next
我最想學的是陣列方式,所以求教會寫的人
我常看准大的程式,但我資質不好,一直沒學會,
現在這個檔,每一欄計算,我都使用單獨的程式,
#10
.Formula = "=YEAR(B3) & "".."" & MONTH(B3)"
這段程式,無法截取B欄資料,將YYYY..M填入A欄
>> With [a3].Resize(R - 2)
.Formula = "=YEAR(B3) & "".."" & MONTH(B3)"
End With
.Value = .Value '轉為值
這功能正常,這是將B欄資料沒經過條件轉為 "2021..6 "
如過您的需求是要經過日期比較,請自行將 .Cells(i, "a").Formula = "=YEAR(RC[1]) & "".."" & MONTH(RC[1])"
移到if 底下
Sub test()
Dim Arr, d, R%
d = Sheets("VBA").[Af2]
R = [北區!b65536].End(3).Row
With Sheets("北區").Range("a1:k" & R)
' With [a3].Resize(R - 2)
' .Formula = "=YEAR(B3) & "".."" & MONTH(B3)"
' End With
' .Value = .Value '轉為值
Arr = .Value
For i = 3 To UBound(Arr)
If Arr(i, 2) >= d Then
.Cells(i, "a").Formula = "=YEAR(RC[1]) & "".."" & MONTH(RC[1])"
.Cells(i, "k").Formula = "=k" & i - 1 & "+G" & i & "-F" & i & "-H" & i & "-I" & i & "+j" & i
End If
Next
'.Value = .Value '轉為值
End With
End Sub
請在測試看看,謝謝作者: samwang 時間: 2021-9-26 06:38
#10
.Formula = "=YEAR(B3) & "".."" & MONTH(B3)"
這段程式,無法截取B欄資料,將YYYY..M填入A欄
>> With [a3].Resize(R - 2)
.Formula = "=YEAR(B3) & "".."" & MONTH(B3)"
End With
.Value = .Value '轉為值
這功能正常,這是將B欄資料沒經過條件轉為 "2021..6 "
如過您的需求是要經過日期比較,請自行將 .Cells(i, "a").Formula = "=YEAR(RC[1]) & "".."" & MONTH(RC[1])"
移到if 底下如下
Sub test()
Dim Arr, d, R%
d = Sheets("VBA").[Af2]
R = [北區!b65536].End(3).Row
With Sheets("北區").Range("a1:k" & R)
' With [a3].Resize(R - 2)
' .Formula = "=YEAR(B3) & "".."" & MONTH(B3)"
' End With
' .Value = .Value '轉為值
Arr = .Value
For i = 3 To UBound(Arr)
If Arr(i, 2) >= d Then
.Cells(i, "a").Formula = "=YEAR(RC[1]) & "".."" & MONTH(RC[1])"
.Cells(i, "k").Formula = "=k" & i - 1 & "+G" & i & "-F" & i & "-H" & i & "-I" & i & "+j" & i
End If
Next
'.Value = .Value '轉為值
End With
End Sub
請在測試看看,謝謝作者: PJChen 時間: 2021-9-26 19:40
另外一個U欄程式,紅字這一段,日期+1的寫法不能用,請問如何讓日期+1
例如:u3=8/3,則+1=8/4
For i = 3 To UBound(Arr)
If Arr(i, 2) >= d And Arr(i, 4) <> "中和" Or Arr(i, 4) <> "內湖" And Arr(i, 4) <> "汐止" Then 'i=列
.Cells(i, "u").Formula = "=Arr(i, 2)" + 1
ElseIf Arr(i, 2) >= d And Arr(i, 4) = "中和" Or Arr(i, 4) = "內湖" And Arr(i, 4) = "汐止" Then
.Cells(i, "u").Formula = "Arr(i, 2)"
End If
Next作者: samwang 時間: 2021-9-27 07:45
E欄要填入單號(連結)T&S&R
但R欄=""時,則E欄="無交貨"
請問要如何改這段程式?
>> 依據您的程式做修改如下
If Arr(i, 2) >= d And Arr(i, 18) = "" Then 'Arr(i, 2),第2欄是日期,R欄無單號
.Cells(i, "e") = "無交貨"
Else
.Cells(i, "e").Formula = "=T" & i & " & S" & i & "& R" & i & ""
End If
但是依據您的描述,後學覺得條件可以拆成2段if 比較好,謝謝
If Arr(i, 2) >= d And Arr(i, 18) = "" Then 'Arr(i, 2),第2欄是日期,R欄無單號
.Cells(i, "e") = "無交貨"
end if
If Arr(i, 2) >= d And Arr(i, 18) <> "" Then
.Cells(i, "e").Formula = "=T" & i & " & S" & i & "& R" & i & ""
end if 作者: samwang 時間: 2021-9-27 08:05
另外一個U欄程式,紅字這一段,日期+1的寫法不能用,請問如何讓日期+1
If Arr(i, 2) >= d And Arr(i, 4) <> "中和" Or Arr(i, 4) <> "內湖" And Arr(i, 4) <> "汐止" Then 'i=列
.Cells(i, "u").Formula = "=B" & i & " +1 "
ElseIf Arr(i, 2) >= d And Arr(i, 4) = "中和" Or Arr(i, 4) = "內湖" And Arr(i, 4) = "汐止" Then
.Cells(i, "u").Formula = "=B" & i & ""
End If
H欄要統計D欄每個月最後一個日期的月統計數量
>> 如下,請測試看看,謝謝
Sub test()
Dim Arr, Brr, xD, i&, T$, T1$
Arr = Sheets("中區").Range("a3:k" & [中區!a65536].End(3).Row + 1)
ReDim Brr(1 To UBound(Arr), 1 To 1)
Set xD = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(Arr)
If Not IsDate(Arr(i, 1)) Then GoTo 98
T = Month(Arr(i, 1)): T1 = Month(Arr(i + 1, 1))
If xD.Exists(T) Then
If T <> T1 Then
xD(Arr(i, 1) & "_" & Arr(i, 3)) = Val(xD(T)) + Val(Arr(i, 4))
Else
xD(T) = Val(xD(T)) + Val(Arr(i, 4))
End If
Else
xD(T) = Val(Arr(i, 4))
End If
98: Next
For Each ky In xD.keys
For i = 1 To UBound(Arr)
Brr(i, 1) = xD(Arr(i, 1) & "_" & Arr(i, 3))
Next
Next
Sheets("中區").[h3].Resize(UBound(Brr)) = Brr
End Sub 作者: PJChen 時間: 2021-9-28 19:48
您好,
執行很快速,不過程式對我來說,複雜很多!能否幫我註解一下紅字用法?
另外這段程式"_",它代表什麼?
xD(Arr(i, 1) & "_" & Arr(i, 3)) = Val(xD(T)) + Val(Arr(i, 4))
Sub test()
Dim Arr, Brr, xD, i&, T$, T1$
Arr = Sheets("中區").Range("a3:k" & [中區!a65536].End(3).Row + 1)
ReDim Brr(1 To UBound(Arr), 1 To 1)
Set xD = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(Arr)
If Not IsDate(Arr(i, 1)) Then GoTo 98
T = Month(Arr(i, 1)): T1 = Month(Arr(i + 1, 1))
If xD.Exists(T) Then
If T <> T1 Then
xD(Arr(i, 1) & "_" & Arr(i, 3)) = Val(xD(T)) + Val(Arr(i, 4))
Else
xD(T) = Val(xD(T)) + Val(Arr(i, 4))
End If
Else
xD(T) = Val(Arr(i, 4))
End If
98: Next
For Each ky In xD.keys
For i = 1 To UBound(Arr)
Brr(i, 1) = xD(Arr(i, 1) & "_" & Arr(i, 3))
Next
Next
Sheets("中區").[h3].Resize(UBound(Brr)) = Brr
End Sub作者: samwang 時間: 2021-9-29 07:58
Sub test()
Dim Arr, Brr, xD, i&, T$, T1$
Arr = Sheets("中區").Range("a3:k" & [中區!a65536].End(3).Row + 1) '資料裝入陣列
ReDim Brr(1 To UBound(Arr), 1 To 1) '創空的陣列用來裝答案
Set xD = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(Arr)
If Not IsDate(Arr(i, 1)) Then GoTo 98 '不是日期離開
T = Month(Arr(i, 1)): T1 = Month(Arr(i + 1, 1))
If xD.Exists(T) Then '是否A欄月份有在字典
If T <> T1 Then '自己與下一列日期的月份比較
'紀錄當月最後一筆的交板數量累加,key:最後日期_採購單號,避免最後一天有多筆
xD(Arr(i, 1) & "_" & Arr(i, 3)) = Val(xD(T)) + Val(Arr(i, 4))
Else
xD(T) = Val(xD(T)) + Val(Arr(i, 4)) '同月份交板數量累加
End If
Else
xD(T) = Val(Arr(i, 4)) '交板數量裝入字典
End If
98: Next
For Each ky In xD.keys '字典keys循環
For i = 1 To UBound(Arr)
Brr(i, 1) = xD(Arr(i, 1) & "_" & Arr(i, 3)) '字典key:最後日期_採購單號,裝入答案陣列
Next
Next
Sheets("中區").[h3].Resize(UBound(Brr)) = Brr '顯示答案
End Sub作者: PJChen 時間: 2021-10-4 21:37
多餘的統計問題
>> 更新如下紅字,請再測試看看,謝謝。
For Each ky In xD.keys
For i = 1 To UBound(Arr)
If i < UBound(Arr) Then
If Arr(i, 1) & "_" & Arr(i, 3) = Arr(i, 1) & "_" & Arr(i + 1, 3) Then GoTo 99
End If
Brr(i, 1) = xD(Arr(i, 1) & "_" & Arr(i, 3))
99: Next
Next 作者: PJChen 時間: 2021-10-6 13:55
我將程式改為以下,可否幫我看下,這樣會有bug嗎?
Dim Arr, Brr, xD, i&, T$, T1$
Arr = Sheets("中區").Range("a3:k" & [中區!a65536].End(3).Row + 1)
ReDim Brr(1 To UBound(Arr), 1 To 1)
Set xD = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(Arr)
If Not IsDate(Arr(i, 1)) Then GoTo 98
T = Year(Arr(i, 1)) & "|" & Month(Arr(i, 1)): T1 = Year(Arr(i, 1)) & "|" & Month(Arr(i + 1, 1))
If xD.Exists(T) Then
If T <> T1 Then
xD(Arr(i, 1)) = Val(xD(T)) + Val(Arr(i, 4))
Else
xD(T) = Val(xD(T)) + Val(Arr(i, 4))
End If
Else
xD(T) = Val(Arr(i, 4))
End If
98: Next
'--------
For Each ky In xD.keys
For i = 1 To UBound(Arr)
If i < UBound(Arr) Then
If Arr(i, 1) = Arr(i + 1, 1) Then GoTo 99
End If
Brr(i, 1) = xD(Arr(i, 1))
99: Next
Next
Sheets("中區").[h3].Resize(UBound(Brr)) = Brr
End Sub作者: samwang 時間: 2021-10-7 07:44
1) A欄每月第一筆日期,不一定1日開始
2) A欄每月最後一筆也不一定是月底日,只要是每月的最後出現的日期就進行統計
3) 要把年度也考量進去,因為資料會不斷增加,不會只有一個年度
4) C欄的單號,會有同一日期,同一單號重複數次,我想程式中應該不用加上C欄的判別,因為單號與統計無關
>> 您真的很用心把所有規則條件列出,35#有小問題我修改後如下,請再測試看看,謝謝
Sub test2()
Dim Arr, Brr, xD, i&, T$, T1$
Arr = Sheets("中區").Range("a3:k" & [中區!a65536].End(3).Row + 1)
ReDim Brr(1 To UBound(Arr), 1 To 1)
Set xD = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(Arr)
If Not IsDate(Arr(i, 1)) Then GoTo 98
T = Year(Arr(i, 1)) & "|" & Month(Arr(i, 1)): T1 = Year(Arr(i, 1)) & "|" & Month(Arr(i + 1, 1))
If xD.Exists(T) Then
If T <> T1 Then
xD(Arr(i, 1)) = Val(xD(T)) + Val(Arr(i, 4))
Else
xD(T) = Val(xD(T)) + Val(Arr(i, 4))
End If
Else
xD(T) = Val(Arr(i, 4))
End If
98: Next
For Each ky In xD.keys
For i = 1 To UBound(Arr)
If Not IsDate(Arr(i, 1)) Then GoTo 99
T = Year(Arr(i, 1)) & "|" & Month(Arr(i, 1)): T1 = Year(Arr(i, 1)) & "|" & Month(Arr(i + 1, 1))
If T <> T1 Then Brr(i, 1) = xD(Arr(i, 1))
99: Next
Next
Sheets("中區").[h3].Resize(UBound(Brr)) = Brr
End Sub 作者: PJChen 時間: 2021-11-2 22:08
Sub 刪rows()
f = Columns("A:G").Find("*", , -4163, , 1, 2).Row
dt = [AF1]
Dim x1 As Range, x As Range
For i = 3 To f
If DateValue(Cells(i, 2)) >= DateValue(dt) And Cells(i, 3) <> "美" Then
Set x = Rows(i)
If x1 Is Nothing Then Set x1 = x
If Not x1 Is Nothing Then Set x1 = Union(x1, x)
End If
Next
If Not x1 Is Nothing Then x1.Delete
End Sub
Sub 刪除列()
Dim xR As Range, xU As Range
For Each xR In Range("c3:c" & [c65536].End(3).Row).Rows
If xR = "美" Then GoTo 99
If xR.Offset(, -1) < [AF1] Then GoTo 99
Set xC = xR
If xU Is Nothing Then Set xU = xR Else Set xU = Union(xR, xU)
99: Next
If Not xU Is Nothing Then xU.EntireRow.Delete
End Sub作者: PJChen 時間: 2021-11-3 19:53
If xR.Offset(, -1) > [AF1] And xR.Offset(, -1) < [AF2] Then GoTo 99作者: 准提部林 時間: 2021-11-7 09:52
做個較完整+防呆的方法:
Sub 刪除列()
Dim D(2) As Date, K%, MS$, xR As Range, xU As Range, N&
If IsDate([AF1]) Then D(1) = [AF1]: K = 1
If IsDate([AF2]) Then D(2) = [AF2]: K = K + 2
If K = 0 Then MsgBox "※未指定刪除日期! ": Exit Sub
If K = 1 Then D(2) = D(1): MS = D(1) & " 之後的資料"
If K = 2 Then D(1) = D(2): MS = D(2) & " 之前的資料"
If K = 3 Then
If D(2) < D(1) Then D(0) = D(1): D(1) = D(2): D(2) = D(0)
MS = D(1) & " 至 " & D(2) & " 之間的資料"
If D(1) = D(2) Then MS = D(1) & " 當天的資料"
End If
If MsgBox("※確定要刪除 " & MS & "? ", 1 + 32 + 256) = vbCancel Then Exit Sub
'---------------------------------------------
For Each xR In Range([c3], [c65536].End(3))
If xR = "美" Or IsDate(xR(1, 0)) = False Then GoTo 99
D(0) = xR(1, 0)
If D(0) < D(1) Or D(0) > D(2) Then GoTo 99
N = N + 1
If N = 1 Then Set xU = xR Else Set xU = Union(xR, xU)
99: Next
If N = 0 Then MsgBox "※執行完畢! 找不到符合的資料! ": Exit Sub
xU.Select
If MsgBox("※執行完畢! 共找到 " & N & " 筆符合資料,是否要刪除? ", 4 + 32 + 256) = vbYes Then xU.EntireRow.Delete
End Sub
Sub test()
Dim Arr, Brr, xD, i&, T$, T1$
Arr = Sheets("南區").Range("a3:k" & [南區!a65536].End(3).Row)
ReDim Brr(1 To UBound(Arr), 1 To 1)
Set xD = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(Arr)
If Not IsDate(Arr(i, 1)) Then GoTo 98
T = Year(Arr(i, 1)) & "|" & Month(Arr(i, 1))
If i < UBound(Arr) Then T1 = Year(Arr(i, 1)) & "|" & Month(Arr(i + 1, 1)) Else T1 = 0
If xD.Exists(T) Then
If T <> T1 Then
xD(Arr(i, 1)) = Val(xD(T)) + Val(Arr(i, 4))
Brr(i, 1) = xD(Arr(i, 1))
Else
xD(T) = Val(xD(T)) + Val(Arr(i, 4))
End If
Else
xD(T) = Val(Arr(i, 4))
End If
98: Next
Sheets("南區").[i3].Resize(UBound(Brr)) = Brr
End Sub