1

VBA'ers,

I'll cut to the chase. I have a userform with all the bells and whistles(Label's,textboxes,listboxes,tabstrips,etc.). Currently I have three subs.

Here's my code. I know people only asked for the userform initialize but seeing all of it might help find the problem.

Private x As Single
Private y As Single
'------------------------------------------
Private Sub CommandButton1_Click()

    Unload Me

End Sub
'------------------------------------------
Private Sub ListBox1_Click()

    x = 2
    y = 2

    name = ListBox1.Value

    'Loop to match names
    Do Until name = Cells(x, y)
        x = x + 1
    Loop

    'Changes lables on click <- I realize I can handle this better with listbox.values
    Label2.Caption = Sheet2.Cells(x, 2)  'Name
    Label5.Caption = Sheet2.Cells(x, 3)  'Current Positions
    Label7.Caption = Sheet2.Cells(x, 4)  'Previous Positions
    Label9.Caption = Sheet2.Cells(x, 5)  'DOB
    Label11.Caption = Sheet2.Cells(x, 6) 'POB
    Label13.Caption = Sheet2.Cells(x, 7) 'Party Affiliation

    'Changes tab strip accordingly
    Call TabStrip1_Change

    'Handles Picture
    If Cells(x, 8) <> "" Then
        Image1.Picture = LoadPicture(ThisWorkbook.Path & Cells(x, 8))
    Else
        Image1.Picture = LoadPicture(ThisWorkbook.Path & "..\pics\nophoto.jpg")
    End If

End Sub
'------------------------------------------
Private Sub TabStrip1_Change()

    'Handle Tab Strip
    If TabStrip1.Value = 0 Then
        TextBox1.Value = Cells(x, 9)
    ElseIf TabStrip1.Value = 1 Then
        TextBox1.Value = Cells(x, 10)
    Else
        TextBox1.Value = Cells(x, 11)
    End If

End Sub
'------------------------------------------
Private Sub UserForm_Initialize()

    'Initialize global variables
    x = 2

    'Initialize lists within userform.
    ListBox1.RowSource = "B2:B11"

    'Set tab strip to first tab.
    TabStrip1.Value = 0                  
    TextBox1.Value = Sheet2.Cells(2, 9)

    'Grab photo if path is in cell
    If Cells(2, 8) <> "" Then
        Image1.Picture = LoadPicture(ThisWorkbook.Path & Cells(2, 8))
    Else
        Image1.Picture = LoadPicture(ThisWorkbook.Path & "..\pics\nophoto.jpg")
    End If

End Sub

The problem is that when I run the code, via vba or a commandButton (Userform1.show) its a coins flip on whether or not the userform populates the listbox. The labels are initialized correctly, but the listbox shows no text. If I continue to run and stop the macro, it will eventually work fine.

Is this a memory issue? Am I not activating the userform properly? Or is this due to sloppy coding?

Any suggestions would be appreciated.

4

1 回答 1

1

由于我们看不到完整的Userform_Initialize(),我假设您只将列表填充到列表框中。

如果您希望列表框在显示时选择某些内容,则需要调用类似ListBox1.ListIndex = 0或默认值的索引。这必须在填充列表之后。

更新:

谢谢,我相信当它不起作用时,这是因为 activesheet 不在您拥有列表项的位置。输入完整的公式地址或范围名称,我的测试工作簿是“Test.xlsm”:

ListBox1.RowSource = "[Test.xlsm]Sheet1!B2:B11"' 更改工作簿和工作表名称以适合您的

或者

ListBox1.RowSource = "Test.xlsm!MyListItems"' 更改工作簿名称,创建和更改包含列表项的范围的名称。

于 2013-09-18T04:14:01.817 回答