Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA将列表框值传递给子对象_Excel_Vba - Fatal编程技术网

Excel VBA将列表框值传递给子对象

Excel VBA将列表框值传递给子对象,excel,vba,Excel,Vba,我无法找出将userform listbox选择传递给sub时的错误所在。scode变量未从listbox选择中获取值(scode=ScrapReasonCodes.ListBox1.value),从而导致无效使用null错误 这是电子表格代码: Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Dim rInt As Range Dim rCell As Range Set rIn

我无法找出将userform listbox选择传递给sub时的错误所在。scode变量未从listbox选择中获取值(scode=ScrapReasonCodes.ListBox1.value),从而导致无效使用null错误

这是电子表格代码:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim rInt As Range
Dim rCell As Range
Set rInt = Intersect(Target, Range("a1:az40"))
 If Not rInt Is Nothing Then
    For Each rCell In rInt
        rCell.Value = "x"
    Next
 End If
Set rInt = Nothing
Set rCell = Nothing
Cancel = True
Call ScrapData
End Sub

Sub ScrapData()
Dim Stamp As String
Dim scode As String
Dim whereitat As String
whereitat = Selection.Address(False, False, xlA1)
Stamp = Format(Now(), "mm-dd-yyyy hh:nn:ss AM/PM")
'scode = InputBox("Issue Code")
ScrapReasonCodes.Show
scode = ScrapReasonCodes.ListBox1.Value
Sheets("Data").Select
ActiveCell.Offset(1, 0).FormulaR1C1 = Stamp
ActiveCell.Offset(1, 1).FormulaR1C1 = scode
ActiveCell.Offset(1, 2).FormulaR1C1 = whereitat
ActiveCell.Offset(1, 0).Select
Sheets("DWG").Activate
End Sub
这是用户表单和按钮代码

Sub CommandButton1_Click()
scode = ScrapReasonCodes.ListBox1.Value
' MsgBox (scode)
Unload Me

End Sub

Sub UserForm_Initialize()

Dim ScrapCodes As Range
Dim WS As Worksheet
Set WS = Worksheets("Data")
 For Each ScrapCodes In WS.Range("ScrapCodes")
    With Me.ListBox1
        .AddItem ScrapCodes.Value
    End With
 Next ScrapCodes

End Sub

谁能看出我错在哪里

问题是,当您单击CommandButton1\u单击(我假设是关闭的)时,您然后卸载表单-这会将其从内存中擦除-然后在您实际重新初始化表单(尽管不显示表单)后执行此操作时,ScrapReasonCodes.ListBox1.Value。要避免这种情况,请按如下所示隐藏表单,读取您的值,然后从调用代码中卸载表单


在剪贴簿中,数据更改在两行以下

ScrapReasonCodes.Show
scode = ScrapReasonCodes.ListBox1.Value
用下面三行

ScrapReasonCodes.Show
scode = ScrapReasonCodes.ListBox1.Value
unload ScrapReasonCodes

然后在CommandButton1\下的用户表单中单击下面的更改

Sub CommandButton1_Click()
scode = ScrapReasonCodes.ListBox1.Value
' MsgBox (scode)
Unload Me

End Sub


您是否可以尝试执行以下操作:在cmdbutton_之后,单击do MsgBox(ScrapReasonCodes.Listbox1.Value)并查看它是否提供了任何值?
Sub CommandButton1_Click()
me.hide
End Sub