返回列表 上一主題 發帖

[發問] 自動套表

本帖最後由 PJChen 於 2019-11-16 23:17 編輯

回復 2# luhpro

請問大大,
一般我在用VBA時,用Alt + F11就可以看到Module的程式碼,為什麼您寫的程式看不到Module ?
另外因為表格一開始的狀態都是空白的,填入資料時要從第3列開始,但在測試時都會從第6列開始填,可否幫忙改為起始由第3列開始寫入資料?
另外我的程式要放在另一檔案Macro.xlsm中執行,並指定檔名"最新庫存.xlsx"請問程式要怎麼修正?
謝謝您

TOP

回復 2# luhpro
真是太感謝了...

TOP

Dear,
我不知道這個套表問題能否用函數解決,但因為表格中的資料大多用函數套出來的,所以在這裡發問...   
...
PJChen 發表於 2019-11-5 19:02


使用儲存格公式的方式我想不出來,
在此只能使用 Excel VBA 嘗試達成 :
只要點擊 廠缺表 的 "產生明細" 按鈕,
結果就出來了...
程式如下 :
  1. Private Sub CbCreat_Click() ' 產生明細
  2.   Dim iI%, iCol%
  3.   Dim lRow&
  4.   Dim sStr$
  5.   Dim diPro, diTit, vA, vD1, vD2
  6.   Dim wsSou As Worksheet, wsTar As Worksheet
  7.   
  8.   Set wsSou = Sheets("出貨")
  9.   Set wsTar = Sheets("廠缺表")
  10.   Set diPro = CreateObject("Scripting.Dictionary") ' 商品缺數
  11.   Set diTit = CreateObject("Scripting.Dictionary") ' 據點列數
  12.   
  13.   
  14.   With wsSou ' 讀取出貨資料
  15.     iCol = 45 '廠缺起始行
  16.     While .Cells(3, iCol) <> ""
  17.       sStr = .Cells(3, iCol) ' 據點名
  18.       lRow = 4 '商品起始列
  19.       While .Cells(lRow, 8) <> "" ' 商品名稱
  20.         If .Cells(lRow, iCol) > 0 Then
  21.           If diPro.Exists(sStr) Then
  22.             diPro(sStr) = diPro(sStr) & "," & lRow & "-" & .Cells(lRow, iCol)
  23.           Else
  24.             diPro(sStr) = lRow & "-" & .Cells(lRow, iCol)
  25.           End If
  26.         End If
  27.         lRow = lRow + 1
  28.       Wend
  29.       iCol = iCol + 1
  30.     Wend
  31.   End With
  32.   
  33. '12 粗 18
  34. '[d8].Font.ColorIndex = 23


  35.   With wsTar ' 產生廠缺表
  36.     .Range(.[A6], .Cells(Rows.Count, 8)).Delete Shift:=xlShiftUp
  37.     lRow = 6
  38.     For Each vA In diPro
  39.       With .Cells(lRow, 2).Resize(, 6) ' 據點名
  40.         With .Cells(1)
  41.           .Value = vA
  42.           diTit(vA) = lRow
  43.           .HorizontalAlignment = xlCenter
  44.           .VerticalAlignment = xlCenter
  45.           With .Font
  46.             .Size = 22
  47.             .Bold = False
  48.           End With
  49.         End With
  50.         
  51.         With .Interior
  52.             .Pattern = xlPatternLinearGradient
  53.             .Gradient.Degree = 90
  54.             .Gradient.ColorStops.Clear
  55.           With .Gradient.ColorStops.Add(0)
  56.             .ThemeColor = xlThemeColorDark1
  57.             .TintAndShade = 0
  58.           End With
  59.           With .Gradient.ColorStops.Add(1)
  60.             .Color = 118671
  61.             .TintAndShade = 0
  62.           End With
  63.         End With
  64.       End With
  65.       lRow = lRow + 1
  66.       
  67.       sStr = diPro(vA)
  68.       vD1 = Split(sStr, ",")
  69.       For iI = 0 To UBound(vD1)
  70.         vD2 = Split(vD1(iI), "-")
  71.         .Cells(lRow, 1) = wsSou.Cells(vD2(0), 6) ' 料號
  72.         .Cells(lRow, 2) = wsSou.Cells(vD2(0), 8) ' 商品名稱
  73.         .Cells(lRow, 4) = wsSou.Cells(vD2(0), 7) ' 箱入數
  74.         .Cells(lRow, 5) = vD2(1) ' 欠瓶數
  75.         .Cells(lRow, 6) = Int(vD2(1) / .Cells(lRow, 4)) ' 箱
  76.         .Cells(lRow, 7) = vD2(1) Mod .Cells(lRow, 4) ' 瓶
  77.         .Cells(lRow, 8) = wsSou.Cells(vD2(0), 5) ' 膠帶
  78.         lRow = lRow + 1
  79.       Next
  80.     Next
  81.    
  82.     lRow = lRow - 1
  83.     With .Range(.[B6], .Cells(lRow, 7))
  84.       .Borders(xlEdgeLeft).LineStyle = xlContinuous
  85.       .Borders(xlEdgeTop).LineStyle = xlContinuous
  86.       .Borders(xlEdgeBottom).LineStyle = xlContinuous
  87.       .Borders(xlEdgeRight).LineStyle = xlContinuous
  88.       .Borders(xlInsideVertical).LineStyle = xlContinuous
  89.       .Borders(xlInsideHorizontal).LineStyle = xlContinuous
  90.     End With
  91.     For Each vA In diTit
  92.       With .Cells(diTit(vA), 2).Resize(, 6)
  93.         .Borders(xlInsideVertical).LineStyle = xlNone
  94.         .Borders(xlInsideHorizontal).LineStyle = xlNone
  95.       End With
  96.     Next
  97.   End With
  98.   MsgBox "缺料明細已產生完畢..."
  99. End Sub
複製代碼
自動套表-Ans.zip (51.69 KB)

TOP

        靜思自在 : 真正的愛心,是照顧好自己的這顆心。
返回列表 上一主題