4

我正在尝试在我的工作中创建一个库存系统,作为我们唯一拥有的 Excel 软件。基本上我们有一个工作订单表,我们输入维修以及使用的零件。我制作了一个代码,可以将插入的零件编号和描述从各个工作订单中提取出来,以跟踪使用的所有内容,但我的老板希望我创建一个系统,允许我们开始输入某物的名称/零件编号,并且让它为我们猜测或填写。因此,组合框出现在哪里。

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

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

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 或类似的东西)

我还想知道是否有一种方法可以根据您输入的内容使部件名称和部件编号都依赖于用户窗体?虽然这是一个较低的优先级。

4

1 回答 1

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 引用中的第一个值,即行索引,而不是第二个数字,即列索引。

于 2013-04-08T02:13:07.867 回答