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.