英文:
Use input from a ListBox in a different module
问题
我想将来自ListBox的用户输入集成到我的宏中。
例如,提前期为4,如果用户输入不同的值,则应该采用该值。
对于UserForm,我有以下内容:
Public Lead_time As Double
Private Sub UserForm_Initialize()
ListBox1.AddItem "1"
ListBox1.AddItem "2"
ListBox1.AddItem "3"
ListBox1.AddItem "4"
ListBox1.AddItem "5"
ListBox1.AddItem "6"
ListBox1.AddItem "7"
ListBox1.AddItem "8"
ListBox1.AddItem "9"
ListBox1.AddItem "10"
End Sub
Private Sub Cmd_OK_Click()
Dim Lead_time As Double
Dim i As Integer
For i = 0 To Me.ListBox1.ListCount
If Me.ListBox1.Selected(i) = True Then
Exit For
End If
Next i
If i = Me.ListBox1.ListCount + 1 Then
MsgBox "选择提前期的数字"
Unload Days_lead_time
End If
Lead_time = Days_lead_time.ListBox1.Value
Unload Days_lead_time
End Sub
在我的另一个模块中,我加载了UserForm,但遗憾的是输入没有被接受:
Lead_time = 4
Load Days_lead_time
Days_lead_time.Show
MsgBox CStr(Lead_time) '在这里,我测试了值是否从UserForm中被接受,但它仍然保持为4。即使我删除了默认的4,也不会显示任何内容。
这是我第一次使用UserForms,所以我有点困惑。
英文:
I want to integrate the user input from a ListBox into my macro.
The lead time would be 4 for example and if a user inputs a different value, this value should be taken instead.
For the Userfrom I have the following:
Public Lead_time As Double
Private Sub UserForm_Initialize()
ListBox1.AddItem "1"
ListBox1.AddItem "2"
ListBox1.AddItem "3"
ListBox1.AddItem "4"
ListBox1.AddItem "5"
ListBox1.AddItem "6"
ListBox1.AddItem "7"
ListBox1.AddItem "8"
ListBox1.AddItem "9"
ListBox1.AddItem "10"
End Sub
Private Sub Cmd_OK_Click()
Dim Lead_time As Double
Dim i As Integer
For i = 0 To Me.ListBox1.ListCount
If Me.ListBox1.Selected(i) = True Then
Exit For
End If
Next i
If i = Me.ListBox1.ListCount + 1 Then
MsgBox "Select the numbers of lead time"
Unload Days_lead_time
End If
Lead_time = Days_lead_time.ListBox1.value
Unload Days_lead_time
End Sub
In my other module I load the UserForm but the input isn't being taken over sadly:
Lead_time = 4
Load Days_lead_time
Days_lead_time.Show
MsgBox CStr(Lead_time) 'here I was testing to see if the value was taken over from the UserFrom but it remains at 4. Even if I delete the standard 4 it doesn't show anything
This is the first time that I use UserFroms, so I'm a bit stuck sadly.
答案1
得分: 0
请查看以下代码的更新部分,其中包括注释:
Option Explicit
Public Lead_time As Double
Private Sub UserForm_Initialize()
' 更简单地将值设置到ListBox中
ListBox1.List = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
End Sub
Private Sub Cmd_OK_Click()
Dim i As Long
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
' 从ListBox中获取(而不是索引)第一个选定项的值
Lead_time = CDbl(ListBox1.List(i))
Exit For
End If
Next i
If Lead_time = 0 Then
MsgBox "请选择提前期的数字"
Else
' 隐藏,但不销毁UserForm,以保持'Lead_time'变量的有效性
Hide
End If
End Sub
你可以像这样使用它:
Dim Lead_time As Double
With New Days_lead_time
.Show
' 因为我们没有卸载UserForm,所以仍然可以访问它的成员
Lead_time = .Lead_time
End With
MsgBox Lead_time
主要更改是UserForm不会立即卸载,因此显示它的代码仍然可以访问它的成员,即存储所选值的Lead_time
。
英文:
Update the code in your UserForm with this ... see the code comments for an explanation
Option Explicit
Public Lead_time As Double
Private Sub UserForm_Initialize()
' simpler setting of values into ListBox
ListBox1.List = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
End Sub
Private Sub Cmd_OK_Click()
Dim i As Long
For i = 0 To ListBox1.ListCount
If ListBox1.Selected(i) Then
' get value (not index) of (first) selected item from ListBox
Lead_time = CDbl(ListBox1.List(i))
Exit For
End If
Next i
If Lead_time = 0 Then
MsgBox "Select the numbers of lead time"
Else
' hide, but don't destroy, the UserForm to keep the 'Lead_time' variable alive
Hide
End If
End Sub
You can then use it like this
Dim Lead_time As Long
With New Days_lead_time
.Show
' because we have not Unloaded the UserForm, we can still access its members
Lead_time = .Lead_time
End With
MsgBox Lead_time
... the key change is that the UserForm is not immediately unloaded and so the code that displayed it (the UserForm) can still get access to its members ... in this case, that is Lead_time
that stores the selected value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论