Dim sStr$, sPath$, sFlName$
...
Set wsSou = ThisWorkbook.Sheets("出貨")
Set wsTar = Sheets("廠缺表") <=刪掉這行
Set diPro = CreateObject("Scripting.Dictionary") ' 商品缺數
sPath = ThisWorkbook.Path ' 如果要指定目錄, 只要改成該目錄即可, 如 sPath = "D:"
sFlName = "最新庫存.xlsx"
bMatch = False ' 檢查 '最新庫存.xlsx' 檔案是否已開啟
For iI = 1 To Workbooks.Count
If Workbooks(iI).Name = sFlName Then
bMatch = True
Exit For
End If
Next iI
If bMatch Then
Set wsTar = Workbooks(sFlName).Sheets("廠缺表")
wsTar.Activate
Else
Set wsTar = Workbooks.Open(Filename:=sPath & "\" & sFlName).Sheets("廠缺表")
End If
最後, 原先的檔案中,
字形大小與箱入數顏色忘了改,
就一併處理了 :
With .Range(.[B3], .Cells(lRow, 7))
.Font.Size = 18
...
End With
For Each vA In diTit
...
Next
With .Range(.[D3], .Cells(lRow, 3))
.Font.ColorIndex = 23
End With
修改後完整程式如下 :
Private Sub CbCreat_Click() ' 產生明細
Dim iI%, iCol%
Dim lRow&
Dim sStr$, sPath$, sFlName$
Dim bMatch As Boolean
Dim diPro, diTit, vA, vD1, vD2
Dim wsSou As Worksheet, wsTar As Worksheet
Set wsSou = ThisWorkbook.Sheets("出貨")
Set diPro = CreateObject("Scripting.Dictionary") ' 商品缺數
Set diTit = CreateObject("Scripting.Dictionary") ' 據點列數
sPath = ThisWorkbook.Path ' 如果要指定目錄, 只要改成該目錄即可, 如 sPath = "D:"
sFlName = "最新庫存.xlsx"
bMatch = False ' 檢查 '最新庫存.xlsx' 檔案是否已開啟
For iI = 1 To Workbooks.Count
If Workbooks(iI).Name = sFlName Then
bMatch = True
Exit For
End If
Next iI
If bMatch Then
Set wsTar = Workbooks(sFlName).Sheets("廠缺表")
wsTar.Activate
Else
Set wsTar = Workbooks.Open(Filename:=sPath & "\" & sFlName).Sheets("廠缺表")
End If
With wsSou ' 讀取出貨資料
iCol = 45 '廠缺起始行
While .Cells(3, iCol) <> ""
sStr = .Cells(3, iCol) ' 據點名
lRow = 4 '商品起始列
While .Cells(lRow, 8) <> "" ' 商品名稱
If .Cells(lRow, iCol) > 0 Then
If diPro.Exists(sStr) Then
diPro(sStr) = diPro(sStr) & "," & lRow & "-" & .Cells(lRow, iCol)
Else
diPro(sStr) = lRow & "-" & .Cells(lRow, iCol)
End If
End If
lRow = lRow + 1
Wend
iCol = iCol + 1
Wend
End With
With wsTar ' 產生廠缺表
.Range(.[A6], .Cells(Rows.Count, 8)).Delete Shift:=xlShiftUp
lRow = 3
For Each vA In diPro
With .Cells(lRow, 2).Resize(, 6) ' 據點名
With .Cells(1)
.Value = vA
diTit(vA) = lRow
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
With .Font
.Size = 22
.Bold = False
End With
End With
With .Interior
.Pattern = xlPatternLinearGradient
.Gradient.Degree = 90
.Gradient.ColorStops.Clear
With .Gradient.ColorStops.Add(0)
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
With .Gradient.ColorStops.Add(1)
.Color = 118671
.TintAndShade = 0
End With
End With
End With
lRow = lRow + 1
lRow = lRow - 1
With .Range(.[B3], .Cells(lRow, 7))
.Font.Size = 18
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlInsideVertical).LineStyle = xlContinuous
.Borders(xlInsideHorizontal).LineStyle = xlContinuous
End With
For Each vA In diTit
With .Cells(diTit(vA), 2).Resize(, 6)
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
Next
With .Range(.[D3], .Cells(lRow, 3))
.Font.ColorIndex = 23
End With
End With
MsgBox "缺料明細已產生完畢..."
End Sub
[attach]31419[/attach]作者: PJChen 時間: 2019-11-17 12:41
不過這個仍然要改 :
sFlName = "最新庫存.xlsx"
Set wsSou = Workbooks(sFlName).Sheets("飛比")
Set diPro = CreateObject("Scripting.Dictionary") ' 商品缺數
紅色這一行要移到這裡 :
End If
Set wsSou = Workbooks(sFlName).Sheets("飛比")
With wsSou ' 讀取飛比資料
要加上據點,
只要在 "劃單合計" (即 BI 欄)左方插入整欄並賦與資料即可.作者: 准提部林 時間: 2019-11-23 11:50
Sub 載入()
Dim S1 As Worksheet, S2 As Worksheet, Rng1 As Range, Rng2 As Range
Dim Arr, R&, C&, Ck%, N&, xR As Range
Set S1 = Sheets("廠缺表"): Set S2 = Sheets("出貨")
Set Rng1 = S1.[B3:G3]: Set Rng2 = S1.[B4:H4]: Set xR = S1.[B3]
Application.ScreenUpdating = False
Call 清除
Arr = Range(S2.[a1], S2.UsedRange)
For C = 45 To UBound(Arr, 2)
Ck = 0
For R = 4 To UBound(Arr)
If Val(Arr(R, C)) <= 0 Then GoTo 101
If Ck = 0 Then
Rng1.Copy xR
xR.Resize(1, 6).VerticalAlignment = xlCenter '跨欄置中
xR = Arr(3, C) '廠缺名稱
Set xR = xR(2): Ck = 1
End If
'----------------------------
Rng2.Copy xR
xR.Resize(1, 4) = Array(Arr(R, 8), "", Arr(R, 7), Arr(R, C))
xR(1, 7) = Arr(R, 5)
Set xR = xR(2): N = N + 1
101: Next R
Next C
If N = 0 Then Exit Sub
Rng2.Copy xR(2)
xR(2).Resize(1, 7).ClearContents
xR(2).Resize(1, 6).Interior.ColorIndex = 37
xR(2, 4).Resize(1, 3) = "=SUM(R[-" & xR.Row - 3 & "]C:R[-1]C)"
End Sub
Sub 清除()
With Sheets("廠缺表")
.UsedRange.Offset(4, 0).EntireRow.Delete
.[B3] = ""
.[B4:G4].ClearContents
.[F4] = "=IF(MIN(D4:E4)=0,"""",INT(E4/D4))"
.[G4] = "=IF(MIN(D4:E4)=0,"""",MOD(E4,D4))"
.[H3:H4].ClearContents
End With
End Sub
看不懂你的問題,
1)若要資料放在"自動廠缺表",
將 Sheets("廠缺表") 改成 Sheets("自動廠缺表")
2)廠缺欄位數若是固定的:
For C = 45 To UBound(Arr, 2)
改成 For C = 45 To 60
或
For C = 45 To UBound(Arr, 2)
Ck = 0
If Arr(3, C) = "劃單合計" Then Exit For '加入這一行, 以[劃單合計]判斷廠缺欄位的結束點作者: PJChen 時間: 2020-2-24 20:29
回復 15#准提部林
這陣子因為作業內容有變動,所以表格也大幅修改,請幫忙看下....
1) For C = 45 To UBound(Arr, 2)
改成 For C = 45 To 60
這個方法測試後OK,
在執行廠缺表後會出現自動加總(目前在第8列)
我想加個"合計",請問在程式哪個地方可以加入?
2) 另外這個方式,我還試不出來,請問我加入的地方是否不對?
For C = 45 To UBound(Arr, 2)
Ck = 0
If Arr(3, C) = "劃單合計" Then Exit For '加入這一行, 以[劃單合計]判斷廠缺欄位的結束點
[attach]31749[/attach]作者: PJChen 時間: 2020-2-24 23:51
Sub 廠缺匯總_匯入()
Dim Arr, R&, 廠缺數&, 入數&, N&
Call 廠缺匯總_清除
Arr = Range([飛比!A1], [飛比!BI65536].End(xlUp))
For R = 4 To UBound(Arr)
廠缺數 = Val(Arr(R, UBound(Arr, 2)))
入數 = Val(Arr(R, 7))
If 廠缺數 * 入數 = 0 Then GoTo 101
N = N + 1
Arr(N, 1) = Arr(R, 6)
Arr(N, 2) = Arr(R, 5)
Arr(N, 3) = Arr(R, 8)
Arr(N, 4) = 入數
Arr(N, 5) = 廠缺數 Mod 入數
Arr(N, 6) = Int(廠缺數 / 入數)
101: Next R
If N = 0 Then Exit Sub
With [廠缺匯總!A3:F3].Resize(N)
.Rows(1).Copy .Cells
.Value = Arr
End With
End Sub
Sub 廠缺匯總_清除()
With Sheets("廠缺匯總")
.UsedRange.Offset(3, 0).EntireRow.Delete
.[A3:F3].ClearContents
End With
End Sub
Sub 最後效期()
Dim Arr, Brr, Crr, R&, i&, N&, BK As Workbook
Set BK = Workbooks("最新庫存.xlsx")
BK.Sheets("最後效期").Activate
R = [飛比!HE65536].End(xlUp).Row
Arr = Sheets("飛比").Range("F1:F" & R)
Brr = Sheets("飛比").Range("HD1:HE" & R)
ReDim Crr(1 To R, 1 To 2)
For i = 4 To R
If Val(Brr(i, 1)) + Val(Brr(i, 2)) = 0 Then GoTo 101
N = N + 1
If Brr(i, 1) > 0 Then Crr(N, 1) = Arr(i, 1)
If Brr(i, 2) > 0 Then Crr(N, 2) = Arr(i, 1)
101: Next i
If N = 0 Then Exit Sub
With Sheets("最後效期")
.[J4:K4].Resize(N) = Crr
If N > 1 Then
.[L4:AB4].Copy .[L5:AB5].Resize(N - 1)
.[A4:H4].Copy .[A5:H5].Resize(N - 1)
End If
End With
End Sub作者: PJChen 時間: 2020-6-16 16:53
Sub 最後效期()
Dim Arr, Brr, Crr, R&, i&, j%, N&(1 To 2), NN&, BK As Workbook
'Set BK = Workbooks("最新庫存.xlsx")
'BK.Sheets("最後效期").Activate
R = [飛比!HE65536].End(xlUp).Row
Arr = Sheets("飛比").Range("F1:F" & R)
Brr = Sheets("飛比").Range("HD1:HE" & R)
ReDim Crr(1 To R, 1 To 2)
For i = 4 To R
For j = 1 To 2
If Val(Brr(i, j)) > 0 Then
N(j) = N(j) + 1: Crr(N(j), j) = Arr(i, 1)
If N(j) > NN Then NN = N(j)
End If
Next j
Next i
If NN = 0 Then Exit Sub
With Sheets("最後效期")
.[J4:K4].Resize(NN) = Crr
If NN <= 1 Then Exit Sub
.[L4:AB4].Copy .[L5:AB5].Resize(NN - 1)
.[A4:H4].Copy .[A5:H5].Resize(NN - 1)
End With
End Sub作者: Andy2483 時間: 2022-10-27 10:43
回復 31#准提部林
'謝謝前輩
'後學在此帖學習到
'1.沒有深入學習得不到精華
'2.習得批次宣告變數且批次數與迴圈搭配使用!初開始只以為少宣告幾個變數
'3.習得 If N(j) > NN Then 取最大數的方法
'4.習得 [J4:K4].Resize(NN) = Crr,以前只會 [J4].Resize(NN,2)
'5.周到的防錯需要再累積經驗!才能辦到
以下心得註解請在指教! 謝謝前輩
Sub 最後效期()
Dim Arr, Brr, Crr, R&, i&, j%, N&(1 To 2), NN&, BK As Workbook
'↑宣告變數
R = [飛比!HE65536].End(xlUp).Row
'↑令R是 HE欄儲存格有內容的最後一列數
Arr = Sheets("飛比").Range("F1:F" & R)
'↑令Arr是陣列 倒入飛比表 的F1到
'F欄的(HE欄儲存格有內容的最後一列數) 的值
Brr = Sheets("飛比").Range("HD1:HE" & R)
'↑令Brr是陣列 倒入飛比表 的HD1到
'HE欄的(HE欄儲存格有內容的最後一列數) 的值
ReDim Crr(1 To R, 1 To 2)
'↑宣告Crr陣列的大小 綜方向 1到 HE欄儲存格有內容的最後一列數
'橫方向 1 到 2
For i = 4 To R
'↑設外順迴圈 從4 到 R
For j = 1 To 2
'↑設內順迴圈 從 1 到 2
If Val(Brr(i, j)) > 0 Then
'↑如果 惠 統 這兩欄裡的值大於0
N(j) = N(j) + 1
'↑N是這兩欄符合If條件次數的變數!初始值是0
Crr(N(j), j) = Arr(i, 1)
'↑Crr陣列從第一列開始放入符合條件 的膠帶顏色
If N(j) > NN Then
'↑如果大於 NN
'↑當N(j)=1時,NN的初始值是0 !條件成立
NN = N(j)
'↑條件成立!就讓NN =符合If條件次數
'↑當N(j)=1時 條件成立! NN=1
'↑後續如果 N(1) N(2)不相等!NN會裝入最大數
End If
End If
Next j
Next i
If NN = 0 Then
'↑如果N()的最大數NN 是0!完全沒有符合條件的資料
Exit Sub
'↑結束程序
End If
With Sheets("最後效期")
'↑接下來關於 最後效期表的相關程序(前面有 空白+"."符號的")
.[J4:K4].Resize(NN) = Crr
'↑由 最後效期表 的[J4:K4](含)開始向下擴展 NN列的範圍貼入Crr的值
'雖然 ReDim Crr(1 To R, 1 To 2)宣告的範圍比 最後結果範圍大!
'但是精準計算!有效Resize擴展結果範圍,就不會影響其他儲存格
If NN <= 1 Then Exit Sub
'↑如果N()的最大數NN=1,就 結束程序
.[L4:AB4].Copy .[L5:AB5].Resize(NN - 1)
'↑將 最後效期表[L4:AB4]儲存格 複製到
'最後效期表[L5:AB5](含)開始向下擴展 NN-1列
.[A4:H4].Copy .[A5:H5].Resize(NN - 1)
'↑將 最後效期表[A4:H4]儲存格 複製到
'最後效期表[A5:H5](含)開始向下擴展 NN-1列
End With
End Sub作者: Andy2483 時間: 2022-10-28 15:12
Option Explicit
Sub TEST_20221028()
Dim Arr, Brr, i&, j&, X, Y, C, R
R = [飛比!HE65536].End(xlUp).Row
Arr = Sheets("飛比").Range("F1:F" & R)
Brr = Sheets("飛比").Range("HD1:HE" & R)
Set X = CreateObject("Scripting.Dictionary")
Set Y = CreateObject("Scripting.Dictionary")
For i = 4 To R
If Val(Brr(i, 1)) > 0 Then
X(Brr(i, 1) & "|" & i) = Arr(i, 1)
End If
If Val(Brr(i, 2)) > 0 Then
Y(Brr(i, 2) & "|" & i) = Arr(i, 1)
End If
Next
With Sheets("最後效期")
.[J4:K4].Resize(R).ClearContents
.[L5:AB5].Resize(R).ClearContents
.[A5:H5].Resize(R).ClearContents
If X.Count > 0 Then
.[J4].Resize(X.Count, 1) = Application.Transpose(X.items)
End If
If Y.Count > 0 Then
.[K4].Resize(Y.Count, 1) = Application.Transpose(Y.items)
End If
C = IIf(X.Count >= Y.Count, X.Count, Y.Count)
If C <= 1 Then Exit Sub
.[L4:AB4].Copy .[L5:AB5].Resize(C - 1)
.[A4:H4].Copy .[A5:H5].Resize(C - 1)
End With
End Sub
Sub 清除()
With Sheets("最後效期")
.[J4:K4].Resize(100).ClearContents
.[L5:AB5].Resize(100).ClearContents
.[A5:H5].Resize(100).ClearContents
End With
End Sub