excel 如果我双击列表框,它会在用户表单中显示值,会怎样

iyr7buue  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(171)

所以列表框在userform3中。在我进行数据搜索后,它将出现在列表框中。当我双击列表框时,它将显示userform2中文本框的值。我使用了这段代码,但它不起作用

Dim r, lr as integer
lr= sheet7. Cells(rows.count, 2).end(xlup).row
For r=2 to lr

If sheet7.cells(r, 1).value=listbox1.list(listbox1.listindex, 1) and sheet7.cells(r, 2).value=listbox1.list(listbox1.listindex, 2) then
Unload me
Userform2.Textbox2.value = sheet7.cells(r,1) 
End if
End with
Next r

为什么那个代码不起作用?仅供参考,有两个标准

v8wbuo2f

v8wbuo2f1#

显示的代码上下文太少
也许这能帮你找到

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    
    'define the criteria parameters
    With Me ' reference the parent Userform object
        With .ListBox1
            Dim refVal1 As String, _
                refVal2 As String
                refVal1 = .List(.ListIndex, 1)
                refVal2 = .List(.ListIndex, 2)
        End With
    End With
        
    Dim r As Long, lr As Long
        With Sheet7 ' reference "sheet7" sheet

            lr = .Cells(.Rows.Count, 2).End(xlUp).Row
                
            For r = 2 To lr
            
                If .Cells(r, 1).Value = refVal1 And .Cells(r, 2).Value = refVal2 Then
                    UserForm2.TextBox2.Value = .Cells(r, 1) ' hoping UserForm2 has no "Initialize" event active...
                    Exit For
                End If
                
            Next
            unload me 'are you sure you want to unload the parent userform?

        End With
        
End Sub

相关问题