gpt4 book ai didi

vba - 使用 ComboBox/UserForms 创建库存系统

转载 作者:行者123 更新时间:2023-12-02 12:02:45 25 4
gpt4 key购买 nike

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

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

当您单击用户表单的“添加零件”时,我不知道如何将零件添加到工单的特定范围内。我一直在关注的所有教程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 或类似的内容)

我还想知道是否有一种方法可以使零件名称和零件编号都依赖于用户表单,具体取决于您输入的哪一个?尽管这是一个较低的优先级。

最佳答案

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

关于vba - 使用 ComboBox/UserForms 创建库存系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14783816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com