Results 1 to 8 of 8

Cannot get value from ListBox in UserForm

Threaded View

  1. #1
    Registered User
    Join Date
    09-20-2016
    Location
    Hong Kong
    MS-Off Ver
    2013
    Posts
    4

    Cannot get value from ListBox in UserForm

    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
    Last edited by howard1324; 05-23-2017 at 10:09 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Userform ListBox depending on a previous Listbox selection
    By Figolu in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2015, 04:31 PM
  2. [SOLVED] if anything in listbox select first item, if listbox empty do nothing (listbox in userform
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2015, 12:49 PM
  3. [SOLVED] copy contents of userform listbox to another userform listbox
    By cfinch100 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-04-2014, 11:39 AM
  4. Transfer Data From One Userform Listbox to Another Userform Listbox with 11 columns
    By sparkoft in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 05-18-2013, 10:54 AM
  5. userform listbox
    By tsiguy96 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-14-2013, 11:09 PM
  6. Listbox to Listbox, no duplicates & submitting same UserForm data for each Listbox entry.
    By jamieswift1977 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-18-2012, 12:18 PM
  7. Replies: 1
    Last Post: 05-17-2006, 04:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1