Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-14-2020, 07:29 PM
JustLearnin JustLearnin is offline Listbox showing null Windows 10 Listbox showing null Office 2019
Novice
Listbox showing null
 
Join Date: Mar 2020
Posts: 2
JustLearnin is on a distinguished road
Default Listbox showing null

I have created two list boxes in word VBA. The first Listbox1 when an item is selected it will add an item to the second list box.



The second list box should then use that value as a bookmark.

The error I get is that Listbox2 shows the text but not the value. So when I refer to that Listbox2 is states "ListBox2.value = "" (null or nothing). How do I get this value?


Here is the bit of code that I wrote.
Private Sub ListBox2_Enter()
Dim sMyStr As String
sMyStr = Me.ListBox1.Value
If sMyStr = "P21" Then
ListBox2.AddItem "41"
ElseIf sMyStr = "P69" Then
ListBox2.AddItem "31"
ElseIf sMyStr = "P62" Then
ListBox2.AddItem "11"
ElseIf sMyStr = "P64" Then
ListBox2.AddItem "42"
Else
ListBox2 = ""
End Sub


Many thanks in advance for any assistance.
Reply With Quote
  #2  
Old 03-14-2020, 09:39 PM
gmayor's Avatar
gmayor gmayor is offline Listbox showing null Windows 10 Listbox showing null Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,154
gmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of light
Default

Can we assume that this is part of a userform? It is not clear why you need two list boxes. Your message suggests that you should use a list box and a text box, thus


Code:
Private Sub ListBox1_Click()
Dim sMyStr As String
    sMyStr = ListBox1.value
    If sMyStr = "P21" Then
        TextBox1.Text = "41"
    ElseIf sMyStr = "P69" Then
        TextBox1.Text = "31"
    ElseIf sMyStr = "P62" Then
        TextBox1.Text = "11"
    ElseIf sMyStr = "P64" Then
        TextBox1.Text = "42"
    Else
        TextBox1.Text = ""
    End If
End Sub

Private Sub UserForm_Initialize()
Dim i As Integer
    With ListBox1
        For i = 21 To 69
            .AddItem "P" & i
        Next i
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 03-15-2020, 01:44 AM
JustLearnin JustLearnin is offline Listbox showing null Windows 10 Listbox showing null Office 2019
Novice
Listbox showing null
 
Join Date: Mar 2020
Posts: 2
JustLearnin is on a distinguished road
Default

Thank you for Graham for your reply.


Yes it is part of a userform

Regarding why I used a Listbox was honestly that's all I could think of at that time and just went with it.
On reading the code that you wrote it made it very simple and I adapted it to my userform and it works exactly as it should.
Once again many many thanks.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to filter data before it is pulled into combobox/listbox AND remove data from listbox Izzii0x Excel Programming 8 05-24-2019 07:25 AM
Combine rows in one where other column is not null Hanna Excel 0 04-28-2017 07:31 AM
Help with Invalid Use of Null Error electronicpizza Word VBA 3 12-15-2014 02:15 AM
This is a Userform LIstbox queston: A variable does not set to the value of a listbox CatMan Excel Programming 14 08-18-2014 08:14 PM
Listbox showing null This is a Userform LIstbox queston: A variable does not set to the value of a listbox CatMan Excel 1 08-08-2014 09:41 AM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 04:16 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft