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

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
203
Office Version
  1. 2013
Platform
  1. Windows
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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
the "'else" is blocked out attempting to fix code.
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
Thanks that works!!!

So ElseIf is acceptable in a statement, however Else is not. Got It!
 
Upvote 0
You can use Else, but only once.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,853
Members
449,051
Latest member
excelquestion515

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
Back
Top