Posting from listbox into testbox and if full post into next textbox

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
115
Hi all,

Stumped once again....

Trying to translate data from a list box into a group of text boxes but placing data in next text box if first is empty. This code just places the same text in all textboxes after the first. I get why its doing it, however not sure how to make it work correctly.

VBA Code:
Private Sub listbox1_Click()
    If Me.TextBox12.value = "" Then
        If Me.ListBox1.value <> "" Then
            Me.TextBox12 = Me.TextBox12 & Me.ListBox1
        End If
    Else
        If Me.ListBox1.value <> "" Then
            Me.TextBox13 = Me.TextBox13 & Me.ListBox1
        End If
    'Else
        If Me.ListBox1.value <> "" Then
            Me.TextBox17 = Me.TextBox17 & Me.ListBox1
        End If
    'Else
        If Me.ListBox1.value <> "" Then
            Me.TextBox16 = Me.TextBox16 & Me.ListBox1
        End If
    'Else
        If Me.ListBox1.value <> "" Then
            Me.TextBox15 = Me.TextBox15 & Me.ListBox1
        End If
    'Else
        If Me.ListBox1.value <> "" Then
            Me.TextBox14 = Me.TextBox14 & Me.ListBox1
        End If
    'Else
        If Me.ListBox1.value <> "" Then
            Me.TextBox18 = Me.TextBox18 & Me.ListBox1
        End If
    'Else
        If Me.ListBox1.value <> "" Then
            Me.TextBox19 = Me.TextBox19 & Me.ListBox1
        End If
    'Else
        If Me.ListBox1.value <> "" Then
            Me.TextBox11 = Me.TextBox11 & Me.ListBox1
        End If
    End If
End Sub
 
Last edited by a moderator:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
115
Attempt 3 not working either.


VBA Code:
Private Sub listbox1_Click()
    If Me.TextBox12.value = "" Then
        If Me.ListBox1.value <> "" Then
            Me.TextBox12 = Me.TextBox12 & Me.ListBox1
        End If
    Else
        If Me.ListBox1.value <> "" Then
            Me.TextBox13 = Me.TextBox13 & Me.ListBox1
        End If
        If Me.TextBox13.value = "" Then
            If Me.ListBox1.value <> "" Then
                Me.TextBox17 = Me.TextBox17 & Me.ListBox1
            End If
        Else
            If Me.ListBox1.value <> "" Then
                Me.TextBox16 = Me.TextBox16 & Me.ListBox1
            End If
            If Me.TextBox16.value = "" Then
                If Me.ListBox1.value <> "" Then
                    Me.TextBox15 = Me.TextBox15 & Me.ListBox1
                End If
            Else
                If Me.ListBox1.value <> "" Then
                    Me.TextBox14 = Me.TextBox14 & Me.ListBox1
                End If
                If Me.TextBox14.value = "" Then
                    If Me.ListBox1.value <> "" Then
                        Me.TextBox18 = Me.TextBox18 & Me.ListBox1
                    End If
                Else
                    If Me.ListBox1.value <> "" Then
                        Me.TextBox19 = Me.TextBox19 & Me.ListBox1
                    End If
                    If Me.TextBox12.value = "" Then
                        If Me.ListBox1.value <> "" Then
                            Me.TextBox11 = Me.TextBox11 & Me.ListBox1
                        End If
                    End If
                End If
            End If
        End If
    End If
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,615
Office Version
  1. 365
Platform
  1. Windows
You can only have one Else in an if statement, but I'm not entirely clear on what you want to do.
Are you looking for the first empty textbox & then putting th listbox value in it?
 

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
115

ADVERTISEMENT

Little difficult to explain, but here goes....

I trying to fill the text boxes with from a list in a listbox. When I click an item in the list box I want to fill the first text box. I then want to click a second third forth and so on item from the list box, although I want the item to place in the next text box if the first, second, or third text box already has data in it.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,615
Office Version
  1. 365
Platform
  1. Windows
Ok, try it like
VBA Code:
Private Sub listbox1_Click()
   If Me.ListBox1.Value = "" Then Exit Sub
   If Me.Textbox12.Value = "" Then
      Me.Textbox12 = Me.ListBox1.Value
   ElseIf Me.Textbox13.Value = "" Then
      Me.Textbox13 = Me.ListBox1.Value
   ElseIf Me.textbox17.Value = "" Then
      Me.textbox17 = Me.ListBox1.Value
   ElseIf Me.TextBox16.Value = "" Then
      Me.TextBox16 = Me.ListBox1.Value
   End If
End Sub
 

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
115
Thanks that works!!!

So ElseIf is acceptable in a statement, however Else is not. Got It!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,615
Office Version
  1. 365
Platform
  1. Windows
You can use Else, but only once.
 

Forum statistics

Threads
1,147,566
Messages
5,741,864
Members
423,692
Latest member
Bhanu1988

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top