使用ComboBox / UserForms创建库存系统

时间:2013-02-09 01:06:07

标签: vba excel-vba combobox userform excel

我正在尝试在我的工作中创建一个库存系统,作为我们唯一的Excel软件。基本上我们有一个工作单表,我们输入维修以及使用的零件。我制作了一个代码,用于拉出插入的部件编号和各个工作订单中的描述,以跟踪所使用的一切,但我的老板希望我创建一个系统,允许我们开始键入某些东西的名称/部件号,让它猜测或填写给我们。因此组合框进来的地方。

我让它达到了一定程度。列表中填充了部件库存(我们有一个主EXTNERAL文件列表)但我的问题是:

当您单击用户表单的“添加部件”时,我无法弄清楚如何在工作订单的某个范围内添加部件。我一直关注herehere的所有教程只设置为按列的顺序添加部分。任何人都可以看看我的(可怕的,对不起)编码,看看他们是否可以提供帮助?

Private Sub UserForm_Initialize()

Dim cPart As Range
Dim cNum As Range
Dim ws As Workbook
'Dim ComboBox1 As Variant


Application.ScreenUpdating = False
Set ws = Workbooks.Open("\\Capserver\iso maintenance\CAPS MASTER PARTS & PRICE LIST 2012.xls")
Windows("CAPS MASTER PARTS & PRICE LIST 2012.xls").Visible = False
'ws.Sheets("CAPS ORDER FORM").Range("Name") = Sheet1.ComboBox1

'ComboBox1.Clear
For Each cPart In ws.Sheets("CAPS ORDER FORM").Range("Name")
    With Me.cboPart
     .AddItem cPart.Value
    End With

Next cPart

For Each cNum In ws.Sheets("CAPS ORDER FORM").Range("Number")
    With Me.cboNum
     .AddItem cNum.Value
     .List(.ListCount - 1, 1) = cNum.Offset(0, 1).Value
    End With

Next cNum

End Sub

Private Sub cmdAdd_Click()
Dim lRow As Range
Dim lPart As Long
Dim ws As Worksheet
Dim something As Variant
Dim box As Object
Set ws = Worksheets("Sheet2")


With Worksheets(1).Range("A1:a500")
Set lRow = .Find(What:="", SearchOrder:=xlRows, SearchDirection:=xlNext, LookIn:=xlValues)
End With

'Set lRow = Range("A1")
'    If VBA.IsEmpty(lRow.Value) Then
'        MsgBox ("POOP!")
'    Else
'        Set box = lRow.End(xlDown)
'    End If

'lRow = Worksheets("Sheet2").Range("A33:A37")
'ws.Cells.Find(What:="*", SearchOrder:=xlRows,  (From tutorial, always returned lRow =     Nothing)
'    SearchDirection:=xlPrevious, LookIn:=xlValues).Row 1

lPart = Me.cboPart.ListIndex

'check for a part number
If Trim(Me.cboPart.Value) = "" Then
  Me.cboPart.SetFocus
  MsgBox "Please enter a part name or number"
  Exit Sub
End If

'copy the data to the database
'use protect and unprotect lines,
'     with your password
'     if worksheet is protected
With ws
'  .Unprotect Password:="password"
  .Cells(lRow, 1).Value = Me.cboPart.Value
  .Cells(lRow, 2).Value = Me.cboPart.List(lPart, 1)
  .Cells(lRow, 3).Value = Me.cboNum.Value
'  .Cells(lRow, 4).Value = Me.txtDate.Value
  .Cells(lRow, 5).Value = Me.txtQty.Value
'  .Protect Password:="password"
End With

'Combobox1.linkedcell=C4

'clear the data
Me.cboPart.Value = ""
Me.cboNum.Value = ""
Me.txtQty.Value = ""
Me.cboPart.SetFocus

End Sub



Private Sub cmdClose_Click()
  Unload Me
End Sub

目标是能够点击“添加零件”按钮并添加多个文件并将其输出到工作单(我认为零件的范围是A33:A55或类似的东西)

我还想知道是否有办法在用户窗体中依赖于您输入的部分名称和部件号?虽然这是一个较低的优先级。

1 个答案:

答案 0 :(得分:1)

我仍然不是100%想要做的事情,你可能会有一些部分评论你可能想要使用(即组合框)。你的1row范围是找到我认为你不想做的下一个空单元格。但至于如何将信息输入范围,您可能需要更改ws With语句:

   1row = 35 'or whatever row number
   For n =0 to CountOfItemsToAdd 'could also be done with a For Each statement
   'You will also need another for statement here to go through your part list
    With ws
     1row = 1row + (n*3)
     '  .Unprotect Password:="password" [you only need this if using passwords]
     .Cells(lRow, 1).Value = Me.cboPart.Value
     .Cells(lRow+1, 1).Value = Me.cboPart.List(lPart, 1)
     .Cells(lRow+2, 1).Value = Me.cboNum.Value
     '  .Cells(lRow, 4).Value = Me.txtDate.Value
     .Cells(lRow+3, 1).Value = Me.txtQty.Value
     '  .Protect Password:="pasword"

但是为了解决您的具体问题,您可以更改.Cells引用中的第一个值,即行索引,而不是第二个数字,即列索引。