返回列表 上一主題 發帖

[發問] 查詢產品數量

回復 20# GBKEE


  DEAR 大大:

自動編碼 我找到

可是 查詢 和 刪除整列 不會做

如附件

謝謝指教

庫存輸入計算0911.rar (15.69 KB)

owen

TOP

回復 21# owen9399
  1. Option Explicit
  2. Option Base 1
  3. Dim Ar(), Sh As Worksheet
  4. Private Sub CommandButton1_Click()
  5.     Dim Nrow As Integer
  6.     If 資料檢查 = True Then Exit Sub
  7.     Nrow = 資料數
  8.     If MsgBox("確定新增第 " & Nrow & " 資料", vbYesNo) = vbNo Then Exit Sub
  9.    
  10.     Ar(1).Value = Nrow
  11.     With Sh.Range("a" & Ar(1) + 1)
  12.         .Resize(, UBound(Ar)) = Ar
  13.         .Resize(, UBound(Ar)) = .Resize(, UBound(Ar)).Value
  14.        '.Cells(1, "i") = "=sum(r2c4:rc4)+sum(r2c5:rc5)-(sum(r2c7:rc7)+sum(r2c8:rc8))"  '庫存差額
  15.         '(台北出貨1 + 台北出貨2)  -  業務員的 (進貨數量1+進貨數量2)   = 庫存差額   ** 怪怪的 **

  16.         '***************************************************************************************
  17.         ' 庫存差額 : 應該是業務員的 (進貨數量1+進貨數量2) - (台北出貨1 + 台北出貨2)
  18.         .Cells(1, "i") = "=sum(r2c7:rc7)+sum(r2c8:rc8)-(sum(r2c4:rc4)+sum(r2c5:rc5))"  '庫存差額
  19.         '***************************************************************************************
  20.         .Cells(1, "i").Value = .Cells(1, "i")     '轉化公式 = 計算後的數值
  21.     End With
  22. End Sub
  23. Private Sub CommandButton2_Click()
  24.     Dim I As Integer
  25.     For I = 1 To UBound(Ar)
  26.       Ar(I).Value = ""
  27.     Next
  28. End Sub
  29. Private Sub CommandButton3_Click()
  30.     Dim I As Integer, Rng As Range
  31.     With Sh
  32.         .AutoFilterMode = False
  33.         For I = 1 To UBound(Ar)
  34.             If Ar(I) <> "" Then .Range("A1").AutoFilter I, Ar(I)
  35.         Next
  36.         .Range("A:i").SpecialCells(xlCellTypeVisible).Copy .Range("AA1")
  37.         .AutoFilterMode = False
  38.         Set Rng = .Range("AA1").CurrentRegion.Offset(1)
  39.     End With
  40.    ListBox1.RowSource = Rng.Address
  41. End Sub
  42. Private Sub CommandButton4_Click()
  43.     Dim s, E As Range, I As Integer
  44.     With ListBox1
  45.         If .ListIndex = -1 Then MsgBox "沒有選擇!!": Exit Sub
  46.         s = Application.Index(Application.Transpose(Application.Transpose(.List)), IIf(.ListCount = 1, 0, 1))
  47.         If Join(s, "") = "" Then MsgBox "沒有資料!!": Exit Sub
  48.         s = Application.Index(Application.Transpose(Application.Transpose(.List)), IIf(.ListCount = 1, 0, .ListIndex + 1))
  49.     End With
  50.     s = Join(s, ",")   'S:  結合控制項的字串 [ 自動編號序號公司產品名稱台北出貨1台北出貨2業務員進貨數量1進貨數量2庫存差額 ]
  51.     With Sh
  52.         For Each E In .Range("A1", .Range("A1").End(xlDown)).Resize(, 9).Rows '整列:[ 自動編號......庫存差額 ]
  53.             If s = Join(Application.Transpose(Application.Transpose(E)), ",") Then
  54.                 If MsgBox(Join(Application.Transpose(Application.Transpose(E.Value)), ","), vbYesNo, "刪除列") = vbYes Then
  55.                     處裡刪除整列 E
  56.                 End If
  57.             End If
  58.         Next
  59.     End With
  60.     CommandButton3_Click   '重新查詢
  61. End Sub
  62. Private Sub CommandButton5_Click()
  63.     End
  64. End Sub
  65. Private Sub UserForm_Initialize()
  66.     Ar = Array(TextBox1, ComboBox1, ComboBox2, TextBox2, TextBox3, ComboBox3, TextBox4, TextBox5)
  67.     Set Sh = Worksheets("sheet1")
  68.     With Sh
  69.         ComboBox1.RowSource = Sh.Range("L2:L5").Address
  70.         ComboBox2.RowSource = Sh.Range("N2:N6").Address
  71.         ComboBox3.RowSource = Sh.Range("M2:M4").Address
  72.     End With
  73.     With ListBox1
  74.         .ColumnHeads = True
  75.         .ColumnCount = 9
  76.     End With
  77. End Sub
  78. Private Sub 處裡刪除整列(Rng As Range)
  79.     Dim I As Integer
  80.     Rng.Delete xlUp
  81.     I = 資料數
  82.     If I > 1 Then
  83.         With Sh
  84.             With .Range("a2:a" & I)
  85.             .Value = "=row()-1"
  86.             .Value = .Value
  87.         End With
  88.         With .Range("i2:i" & I)
  89.             .Value = "=sum(r2c7:rc7)+sum(r2c8:rc8)-(sum(r2c4:rc4)+sum(r2c5:rc5))"  '庫存差額
  90.             .Value = .Value
  91.         End With
  92.      End With
  93.     End If
  94.      
  95. End Sub
  96. Private Function 資料數() As Integer
  97.     資料數 = Application.CountA(Sh.Range("A:A"))     '自動編號
  98. End Function
  99. Private Function 資料檢查() As Boolean
  100.     Dim s As String, E As Range, I As Integer, ii
  101.     With Sh
  102.     For I = 2 To UBound(Ar)
  103.         ii = 10 - Len(Sh.Cells(1, I))
  104.         If I = 2 Or I = 3 Or I = 6 Then
  105.             If Ar(I).ListIndex = -1 Then s = s & IIf(s = "", "", vbLf) & Sh.Cells(1, I) + Space(ii) & vbTab & Ar(I)
  106.         Else
  107.             If Not IsNumeric(Ar(I)) And Ar(I) <> "" Then s = s & IIf(s = "", "", vbLf) & Sh.Cells(1, I) + Space(ii) & vbTab & Ar(I)
  108.         
  109.         End If
  110.     Next
  111.     If s <> "" Then
  112.     資料檢查 = True: MsgBox s, , "資料有誤!!": Exit Function
  113.     ElseIf s = "" And Ar(4) & Ar(5) & Ar(7) & Ar(8) = "" Then
  114.         資料檢查 = True: MsgBox "出貨 進貨 沒有數量", , "資料有誤!!": Exit Function
  115.     End If
  116.     s = "," & Join(Ar, "")
  117.     s = Replace(s, "," & Ar(1), "")  'S:  結合控制項的字串 [ 序號公司產品名稱台北出貨1台北出貨2業務員進貨數量1進貨數量2 ]
  118.         For Each E In .Range("B1", .Range("B1").End(xlDown)).Resize(, 7).Rows
  119.             If s = Join(Application.Transpose(Application.Transpose(E.Value)), "") Then
  120.                 MsgBox Replace(Join(Ar, ","), Ar(1) & ",", "") & vbLf & "已存在為 第" & E.Row - 1 & " 筆 資料不可新增"
  121.                 資料檢查 = True
  122.                 Exit Function
  123.             End If
  124.         Next
  125.     End With
  126. End Function
複製代碼
如圖 表單中新加一 ListBox1

感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 22# GBKEE


    Dear 大大:

   真的很感謝你

    我的庫存差額 是指 每一筆 的差值
   並非 全部筆數 累計 的差額
  
   如何修正

   謝謝

庫存輸入計算0911(已輸入程式).rar (23 KB)

owen

TOP

回復 23# owen9399
  1. Private Sub CommandButton1_Click()
  2.   Dim Nrow As Integer
  3.     If 資料檢查 = True Then Exit Sub
  4.     Nrow = 資料數
  5.     If MsgBox("確定新增第 " & Nrow & " 資料", vbYesNo) = vbNo Then Exit Sub
  6.     ar(1).Value = Nrow
  7.     With Sh.Range("a" & ar(1) + 1)
  8.         .Resize(, UBound(ar)) = ar
  9.         .Resize(, UBound(ar)) = .Resize(, UBound(ar)).Value
  10.         '.Cells(1, "i") = "=sum(r2c7:rc7)+sum(r2c8:rc8)-(sum(r2c4:rc4)+sum(r2c5:rc5))"  '庫存差額
  11.         '.Cells(1, "i") = "=sum(r2c4:rc4)+sum(r2c5:rc5)-(sum(r2c7:rc7)+sum(r2c8:rc8))"  '庫存差額
  12.         .Cells(1, "i") = "=(rc4+rc5)-(rc7+rc8)"  '庫存差額    公式
  13.       '  .Cells(1, "i").Value = .Cells(1, "i")     '轉化公式 = 計算後的數值
  14.     End With
  15. End Sub
複製代碼
  1. Private Sub 處裡刪除整列(Rng As Range)
  2.     Dim I As Integer
  3.     Rng.Delete xlUp
  4.     I = 資料數
  5.     If I > 1 Then
  6.         With Sh
  7.             With .Range("a2:a" & I)
  8.                 .Value = "=row()-1"
  9.                 .Value = .Value
  10.             End With
  11.             With .Range("i2:i" & I)
  12.                 '.Value = "=sum(r2c7:rc7)+sum(r2c8:rc8)-(sum(r2c4:rc4)+sum(r2c5:rc5))"  '庫存差額
  13.                 .Value = "=(rc4+rc5)-(rc7+rc8)" '庫存差額  公式
  14.               '  .Value = .Value                      ''庫存差額  數值

  15.             End With
  16.         End With
  17.     End If
  18. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 24# GBKEE


    Dear 大大:

   謝謝

  程式ok 非常感恩
owen

TOP

本帖最後由 owen9399 於 2013-9-11 17:59 編輯

回復 24# GBKEE


    Dear 大大:

我要 再 延伸 新的工作表 如
1.秀出 輸入數值 的表單
如果 可以 整合 用 sheet1的 下拉式 就比較好
sheet1 的 部份數值 會和 輸入數值 結合 在分到 3位總清單
目前
輸入  不會改
刪除整列   不會改

2.到時後要整合 分別 帶到 3位業務員總清單

附件


謝謝指導

庫存輸入計算0911(新增項目)1.rar (31.76 KB)

owen

TOP

回復 26# owen9399
沒說明sheet1部份數值,輸入數值的關係, 如何結合到3位總清單
配額,筆數 與 sheet1數值的關連如何??
輸入數值的表單,新增程式沒弄好
  1. '*** UserForm2 表單模組 *********
  2. Option Base 1
  3. Dim ar(), Sh(1 To 2) As Worksheet
  4. Private Sub UserForm_Initialize()
  5.      ar = Array(TextBox1, ComboBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, ComboBox2)     '這裡有修改控制項,事正確的
  6.      Set Sh(1) = Worksheets("Sheet1")
  7.      Set Sh(2) = Worksheets("輸入數值")
  8.        With Sh(2)
  9.         ComboBox1.RowSource = Sh(1).Range("L2:L5").Address  ' 整合用sheet1的下拉式
  10.         ComboBox2.RowSource = Sh(1).Range("M2:M4").Address
  11.      End With
  12.      With ListBox1
  13.         .ColumnHeads = True
  14.         .ColumnCount = 8
  15.      End With
  16. End Sub
  17. Private Function 資料檢查() As Boolean
  18.         Dim s As String, E As Range, I As Integer, ii
  19.         With Sh
  20.         For I = 2 To UBound(ar)
  21.             ii = 10 - Len(Sh.Cells(1, I))
  22.             If I = 2 Or I = 3 Or I = 6 Then   'I= ??  ComboBox 及 ListBox 才有.ListIndex的屬性
  23.             '*** 這裡沒改:TextBox1 沒有.ListIndex的屬性  ****
  24.                If ar(I).ListIndex = -1 Then s = s & IIf(s = "", "", vbLf) & Sh.Cells(1, I) + Space(ii) & vbTab & ar(I)
  25.                
  26.            Else
  27.                 If Not IsNumeric(ar(I)) And ar(I) <> "" Then s = s & IIf(s = "", "", vbLf) & Sh.Cells(1, I) + Space(ii) & vbTab & ar(I)
  28.                 '*** 這裡也要改  ***********
  29.             End If
  30.             Next
  31.         If s <> "" Then
  32.         資料檢查 = True: MsgBox s, , "資料有誤!!": Exit Function
  33.         ElseIf s = "" And ar(4) & ar(5) & ar(7) & ar(8) = "" Then  '*** 這裡也要改  ***********
  34.             資料檢查 = True: MsgBox "出貨 進貨 沒有數量", , "資料有誤!!": Exit Function
  35.        End If
  36.         s = "," & Join(ar, "")
  37.         s = Replace(s, "," & ar(1), "")  'S:  結合控制項的字串 [ 自動編號序號公司配額筆數數值應付已付(進貨數量1+進貨數量2)業務員 ]
  38.             For Each E In .Range("B1", .Range("B1").End(xlDown)).Resize(, 7).Rows
  39.                 If s = Join(Application.Transpose(Application.Transpose(E.Value)), "") Then
  40.                     MsgBox Replace(Join(ar, ","), ar(1) & ",", "") & vbLf & "已存在為 第" & E.Row - 1 & " 筆 資料不可新增"
  41.                     資料檢查 = True
  42.                     Exit Function
  43.                 End If
  44.             Next
  45.         End With
  46.         
  47.   End Function
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 27# GBKEE


    Dear 大大:
   我有修改
   可是 刪除整列 無法用

   不知如何改

   謝謝

庫存輸入計算0912(新增項目)2.rar (31.9 KB)

owen

TOP

回復 28# owen9399
  1. Private Sub CommandButton3_Click() '查詢
  2.     Dim I As Integer, Rng As Range
  3.      With Sh(2)
  4.          .AutoFilterMode = False
  5.          For I = 1 To UBound(ar)
  6.             If ar(I) <> "" Then .Range("A1").AutoFilter I, ar(I)
  7.          Next
  8.         '.Range("A:i").SpecialCells(xlCellTypeVisible).Copy .Range("AA1") ->'Sh(2)的 AA1->AI欗
  9.          '*******   .ColumnCount = 8    'ListBox1設定8欗     所以改成如下********
  10.         .Range("A:H").SpecialCells(xlCellTypeVisible).Copy .Range("AA1")
  11.          .AutoFilterMode = False
  12.          Set Rng = .Range("AA1").CurrentRegion.Offset(1)   'Sh(2)的 AA1->AH :AI欗的資料,*****AI欗須先刪掉(手動)*****
  13.      End With
  14.   ListBox1.RowSource = Rng.Address
  15. End Sub
  16. Private Sub CommandButton4_Click() '刪除整列
  17.     Dim s, E As Range, I As Integer
  18.     With ListBox1
  19.         If .ListIndex = -1 Then MsgBox "沒有選擇!!": Exit Sub
  20.         s = Application.Index(Application.Transpose(Application.Transpose(.List)), IIf(.ListCount = 1, 0, 1))
  21.         If Join(s, "") = "" Then MsgBox "沒有資料!!": Exit Sub
  22.         s = Application.Index(Application.Transpose(Application.Transpose(.List)), IIf(.ListCount = 1, 0, .ListIndex + 1))
  23.         'S= ListIndex的選擇.ListIndex 那一列的資料,會比 Resize(, 8)多一欗.
  24.         '當
  25.         '->.Range("A:i").SpecialCells(xlCellTypeVisible).Copy .Range("AA1") ->'Sh(2)的 AA1->AI欗
  26.         '-> For Each E In .Range("A1", .Range("A1").End(xlDown)).Resize(, 8).Rows '整列:[ 自動編號......業務員 ]
  27.     End With
  28.     s = Join(s, ",")   'S:  結合控制項的字串 [ 自動編號序號公司配額筆數數值應付已付(進貨數量1+進貨數量2)業務員 ]
  29.     With Sh(2)
  30.          For Each E In .Range("A1", .Range("A1").End(xlDown)).Resize(, 8).Rows '整列:[ 自動編號......業務員 ]
  31.             Debug.Print s
  32.             Debug.Print Join(Application.Transpose(Application.Transpose(E)), ",")
  33.             If s = Join(Application.Transpose(Application.Transpose(E)), ",") Then
  34.                
  35.                 If MsgBox(Join(Application.Transpose(Application.Transpose(E.Value)), ","), vbYesNo, "刪除列") = vbYes Then
  36.                      處裡刪除整列 E
  37.                 End If
  38.             End If
  39.         Next
  40.     End With
  41.     CommandButton3_Click   '重新查詢
  42. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 29# GBKEE


    DEAR 大大:

   我試不出來 刪除整列 的問題

   無法執行
owen

TOP

        靜思自在 : 能付出愛心就是福,能消除煩惱就是慧。
返回列表 上一主題