将预定义的值放在 USERFORM VB 查询中

Posted

技术标签:

【中文标题】将预定义的值放在 USERFORM VB 查询中【英文标题】:Put predefined values, inside a USERFORM VB query 【发布时间】:2017-10-14 17:45:53 【问题描述】:

我是宏的新手,但对它的工作原理有一些基本了解,或者能够编写小的 VBA 代码。

我创建了一个示例用户表单,但我想知道如何将值放入代码本身,这样我就不想维护一个单独的查找表来保持我的下拉选项的所有这些值在我创建的用户表单下可用。

请找出我使用的代码。

Private Sub cmdAdd_Click()
Dim lRow As Long
Dim lPart As Long
Dim ws As Worksheet
Set ws = Worksheets("PartsData")

'find first empty row in database
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    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 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.cboLocation.Value
  .Cells(lRow, 4).Value = Me.txtDate.Value
  .Cells(lRow, 5).Value = Me.txtQty.Value
'  .Protect Password:="password"
End With

'clear the data
Me.cboPart.Value = ""
Me.cboLocation.Value = ""
Me.txtDate.Value = Format(Date, "Medium Date")
Me.txtQty.Value = 1
Me.cboPart.SetFocus

End Sub

Private Sub cmdClose_Click()
  Unload Me
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

  If CloseMode = vbFormControlMenu Then
    Cancel = True
    MsgBox "Please use the Close Form button!"
  End If
End Sub

Private Sub UserForm_Initialize()
Dim cPart As Range
Dim cLoc As Range
Dim ws As Worksheet
Set ws = Worksheets("LookupLists")

For Each cPart In ws.Range("PartIDList")
  With Me.cboPart
    .AddItem cPart.Value
    .List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value
  End With
Next cPart

**For Each cLoc In ws.Range("LocationList")
  With Me.cboLocation
    .AddItem cLoc.Value
  End With
Next cLoc**

Me.txtDate.Value = Format(Date, "Medium Date")
Me.txtQty.Value = 1
Me.cboPart.SetFocus

End Sub

I learn how to set this userfrom from a web site and you can use this link to download the sample excel file (posted on that website)

请帮我解决这个问题。

提前致谢

【问题讨论】:

你已经用这行Me.cboPart.Value = ""设置了一个控制值....只需使用相同类型的代码设置一个不为空的值 【参考方案1】:

假设您想在UserForm_Initialize sub 中添加一些预定义的位置,而不是写...

For Each cLoc In ws.Range("LocationList")
  With Me.cboLocation
    .AddItem cLoc.Value
  End With
Next cLoc

...改成

With Me.cboLocation
    .AddItem "Location 1"
    .AddItem "Location 2"
    .AddItem "Location 3"
    'Keep going as many as you like
End With

【讨论】:

谢谢Rosetta,它运行良好,你能帮我一件事吗,是否可以使用这些值来保存我拥有的excel文件。如果你能在这方面帮助我,那就太好了。 当然。要将组合框中的选定位置放入 Excel 电子表格范围,例如单元格 A1,为 Range("A1") = me.cboLocation.value 谢谢,但是我们使用用户表单捕获的值,如何在我们创建的模块上获取这些值,例如,我从我创建的 UserFomr 中选择“cboLocation”下的值并添加一个按钮, 如果用户点击它调用 MODULE 1 代码为SUB Try1 () Dim location Location = cbolocation MsgBox Location END SUB。我试过了,但我在 msgbox 下得到了 null 值。 我认为Location = cboLocation.value 会解决这个问题吗? fyi .text.value 对于组合框有区别,请参阅 ***.com/questions/2844193/…

以上是关于将预定义的值放在 USERFORM VB 查询中的主要内容,如果未能解决你的问题,请参考以下文章

vb.net引用Dll的问题

如何加载和卸载Userform

使用非连续范围的值通过RowSource属性填充UserForm Combobox

Excel 单元格公式可以引用 UserForm 控件吗?

在 .NET 应用程序中预分配(保证)内存

如何根据先前的用户选择在 Django 中预填充(多个)ChoiceField