Hello,
Below are my codes I typed in Excel Macro.
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 2014 To 2025
ListBox1.AddItem i
Next i
For i = 1 To 12
ListBox2.AddItem i
Next i
For i = 2016 To 2027
ListBox3.AddItem i
Next i
For i = 1 To 12
ListBox4.AddItem i
Next i
ListBox1.Value = Year(Date)
ListBox2.Value = Month(Date)
ListBox3.Value = Year(Date)
ListBox4.Value = Month(Date)
End Sub
Private Sub Calinterest_Click()
Dim sy, sm, ey, em As Integer
sy = ListBox1.Value
sm = ListBox2.Value
ey = ListBox3.Value
em = ListBox4.Value
If sy = "" Or sm = "" Or ey = "" Or em = "" Then
MsgBox ("Cannot leave blank.")
Exit Sub
End If
End Sub
I created 4 ListBoxs in a UserForm, then I added years into ListBox1 and ListBox3, and months into ListBox2 and ListBox 4. After that, I set the defalut values to be today year and month repectively, and there is a button called "Calinterest" to get the values from the ListBoxs. But it seems the program cannot get the ListBox4's value if I just click the button without clicking a value in the ListBox4, even if the value in ListBox 4 is highlighted, it always returns nothing. The program works fine if I click all the ListBoxs. Is there something wrong when I set the default value for the ListBoxs?
Thanks,
Howard
Bookmarks