返回列表 上一主題 發帖

[發問] 求解!!資料一對多的驗證

本帖最後由 GBKEE 於 2016-6-22 05:00 編輯

回復 3# bsy4life


   
順便請教一下combobox無法用滑鼠滾動是要怎麼改

不是有方向鍵可用

work 表單模組程式碼試試看
TextBoxProductNumber,TextBoxMachineNumber,ComboBoxProcess
皆改為 ComboBox 的控制項 其 BoundColumn 屬性 請指定=1
  1. Dim d As Object, Msg As Boolean  '這表單模組 的私用變數
  2. Private Sub UserForm_Initialize()
  3.      TextBoxWorkOrderNumber = New_TextBoxWorkOrderNumber
  4.      TextBoxProductNumber_MakeList
  5.      TextBoxMachineNumber_MakeList
  6.      Msg = True
  7. End Sub
  8. Private Sub TextBoxProductNumber_Change()
  9.    With ComboBoxProcess  ' 製程 控制項
  10.         .Clear
  11.         If TextBoxProductNumber.ListIndex > -1 Then
  12.             If d(TextBoxProductNumber.Value).Rows.Count = 1 Then
  13.                 .AddItem d(TextBoxProductNumber.Value).Cells(2)  '料號只有一個
  14.             Else
  15.                 .List = d(TextBoxProductNumber.Value).Columns(2).Value '相同料號 不只一個  的製程範圍
  16.             End If
  17.             .ListIndex = 0
  18.         End If
  19.     End With
  20.     xChicked
  21. End Sub
  22. Private Sub ComboBoxProcess_Change()
  23.     LabeExceptTimeShow.Caption = ""
  24.     LabelProcessTimeShow.Caption = ""
  25.     With ComboBoxProcess
  26.         If .ListIndex > -1 Then
  27.             ' ***  d(TextBoxProductNumber.Value) ->Range 物件***
  28.             LabeExceptTimeShow.Caption = d(TextBoxProductNumber.Value).Cells(.ListIndex + 1, "C")
  29.             LabelProcessTimeShow.Caption = d(TextBoxProductNumber.Value).Cells(.ListIndex + 1, "D")
  30.         End If
  31.     End With
  32.     xChicked
  33. End Sub
  34. Private Sub TextBoxMachineNumber_Change() '帶入機器型號的資料
  35.     With TextBoxMachineNumber
  36.         LabelMachineShow.Caption = ""
  37.         If .ListIndex > -1 Then LabelMachineShow.Caption = .List(.ListIndex, 1)
  38.     End With
  39.     xChicked
  40. End Sub
  41. Private Sub TextBoxWorkOrderNumber_Change()
  42.     xChicked
  43. End Sub

  44. Private Sub CommandButtonSend_Click()
  45. Dim Rng As Range, BtnCode As Integer
  46.     If MsgBox("新增派工  - " & TextBoxWorkOrderNumber, vbYesNo + 16 * 2, "資料送出") = vbNo Then Exit Sub
  47.     With Sheets("新增派工")
  48.         Set Rng = .Range("A1").End(xlDown)
  49.         If Rng.Row = Rows.Count Then Set Rng = .Range("A1")
  50.     End With
  51.     With Rng.Offset(1)
  52.         .Cells(1, 1) = TextBoxWorkOrderNumber.Value '工單編號
  53.         .Cells(1, 2) = TextBoxProductNumber.Value '料號
  54.         .Cells(1, 3) = ComboBoxProcess.Text  '製程
  55.         .Cells(1, 4) = TextBoxMachineNumber.Value  '機器編號
  56.         .Cells(1, 5) = LabelMachineShow.Caption  '機器型號
  57.         .Cells(1, 6) = LabeExceptTimeShow.Caption '除外工時
  58.         .Cells(1, 7) = LabelProcessTimeShow.Caption '單次工時
  59.     End With
  60.     TextBoxProductNumber.ListIndex = -1
  61.     TextBoxMachineNumber.ListIndex = -1
  62.     ComboBoxProcess.ListIndex = -1
  63.     BtnCode = CreateObject("WScript.Shell").popup("此檔案已自動存檔", 1, Caption)
  64.     ThisWorkbook.Save
  65.     TextBoxWorkOrderNumber = New_TextBoxWorkOrderNumber
  66.     BtnCode = CreateObject("WScript.Shell").popup("工單編號 " & TextBoxWorkOrderNumber, 2, Caption)
  67.     TextBoxWorkOrderNumber.SetFocus
  68. End Sub
  69. Private Sub CommandButtonExit_Click()
  70.     End
  71. End Sub
  72. Private Sub TextBoxProductNumber_MakeList()  '製程工時:料號資料
  73.     Dim Rng As Range
  74.     Set d = CreateObject("scripting.dictionary")   '字典物件
  75.     Set Rng = Sheets("製程工時").Range("A2")
  76.     Do While Rng <> ""
  77.         If d.EXISTS(Rng.Value) Then
  78.             Set d(Rng.Value) = Union(Rng.Resize(, 4), d(Rng.Value)) '料號, 製程, 除外工時,單次工時
  79.         Else
  80.             Set d(Rng.Value) = Rng.Resize(, 4)      '料號, 製程, 除外工時,單次工時
  81.         End If
  82.         Set Rng = Rng.Offset(1)
  83.     Loop
  84.     With TextBoxProductNumber
  85.         .List = d.KEYS
  86.         .ListIndex = 0
  87.     End With
  88. End Sub
  89. Private Sub TextBoxMachineNumber_MakeList()  'List 包含( 機器編號 , 型號)
  90.         With Sheets("機器型號")
  91.                 TextBoxMachineNumber.List = .Range("A2:B" & .Range("A1").End(xlDown).Row).Value
  92.         End With
  93.         TextBoxMachineNumber.ListIndex = 0
  94. End Sub
  95. Private Function New_TextBoxWorkOrderNumber() As String '新增 工單號碼  格式: XXX-1234567890
  96.     Dim New_No As Variant
  97.     With Sheets("新增派工").Range("A1").End(xlDown)
  98.             If .Row > 1 And .Cells <> "" Then
  99.                 New_No = Split(.Cells, "-")
  100.                 New_No(1) = Format(Val(New_No(1)) + 1, "0000000000")
  101.                 New_TextBoxWorkOrderNumber = New_No(0) & "-" & New_No(1)
  102.             End If
  103.     End With
  104. End Function
  105. Private Sub xChicked()  '防呆程式
  106.     Dim BtnCode As Integer, xOrder, Msg As Boolean
  107.     With CommandButtonSend
  108.         .Enabled = TextBoxProductNumber.ListIndex > -1 And ComboBoxProcess.ListIndex > -1 And TextBoxMachineNumber.ListIndex > -1
  109.         .Enabled = .Enabled And Len(Trim(TextBoxWorkOrderNumber)) = 14
  110.         If Len(Trim(TextBoxWorkOrderNumber)) = 14 Then
  111.              xOrder = Split(TextBoxWorkOrderNumber, "-")
  112.             If UBound(xOrder) = 0 Then Msg = True
  113.             If UBound(xOrder) = 1 Then
  114.                 If Len(xOrder(0)) <> 3 Then Msg = True   '前三碼
  115.                 If Len(xOrder(1)) <> 10 Then Msg = True '後十碼
  116.                 If Len(xOrder(1)) = 10 And IsNumeric(xOrder(1)) = False Then Msg = True '後十碼需為數字
  117.             End If
  118.             If Msg Then BtnCode = CreateObject("WScript.Shell").popup("工單編號 錯誤  " & TextBoxWorkOrderNumber & vbLf & "如 :xxx-1234567890", 2, Caption)
  119.             .Enabled = .Enabled And Msg = False
  120.         End If
  121.     End With
  122. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 虛空有盡.我願無窮,發願容易行願難。
返回列表 上一主題