我有一个VBA宏,通过向用户询问一系列问题(要求他们说出哪个打开的工作簿执行哪个功能)来打开。我有一系列userform.show命令如下:
===
(现在,在事件之后,我意识到将这些放入一个用户表单会更简单。)
净效果是最后一个不显示 经过一些研究后,我将代码更改为:
UserForm2.Show ' select cost data file
Set piersBook = ActiveWorkbook
UserForm5.Show ' select IRR file
Set irrBook = ActiveWorkbook
UserForm6.Show ' select BC summary file
Set bcSummary = ActiveWorkbook
在恢复到原始问题之前,这可以进行大约5或6次迭代。
我在userform初始化代码中添加了断点。他们都被调用,用户表单全部工作(直到我再次删除了断点)。
最后,我开始删除有问题的用户形式:问题转移到下一个。再一次,当那个被删除时,再去之前的那个。
用户形式'代码完全相同:
UserForm2.Show ' select cost data file
Set piersBook = ActiveWorkbook
UserForm5.Show ' select IRR file
Set irrBook = ActiveWorkbook
DoEvents
UserForm6.Show ' select BC summary file
Set bcSummary = ActiveWorkbook
有什么想法?目前我正在硬编码不理想的输入。 非常感谢。
答案 0 :(得分:1)
仅使用UserForm2
,然后:
按如下方式更改UserForm2
代码
Private Sub ListBox1_Click()
With Me
If ListBox1.ListIndex <> -1 Then
.Tag = .ListBox1.Value
.Hide
Else
MsgBox "You must select a workbook"
End If
End With
End Sub
Private Sub UserForm_Initialize()
Dim wb As Workbook
For Each wb In Workbooks
ListBox1.AddItem wb.Name
Next wb
End Sub
更改“主要”代码,如下所示
Dim piersBook As Workbook, irrBook As Workbook, bcSummary As Workbook
With UserForm2
.Caption = "select cost data file"
.Show ' select cost data file
Set piersBook = Workbooks(.Tag)
.Caption = "select IRR file"
.Show ' select cost data file
Set irrBook = Workbooks(.Tag)
.Caption = "select BC summary file"
.Show ' select BC summary file
Set bcSummary = Workbooks(.Tag)
End With
Unload UserForm2