textbox value in one column next to the first item of a listbox in another column

Doflamingo

Board Regular
Joined
Apr 16, 2019
Messages
238
Hi all,

Does anyone know the lines of code that would allow me to put the value of a textbox1 from a userform in the column A just next to the 1st item of the listbox1 that is displayed in the column B ? It drives me crazy..

Here is the code of the listbox 1

Code:
For b = 0 To ListBox1.ListCount - 1
With Sheets("Data")
     nextrow = .Range("B" & Rows.Count).End(xlUp).Row + 1
     .Range("B" & nextrow) = ListBox1.List(b)
End With
Next

Here is the code of the textbox value

Code:
With Sheets("Data")
     arow = .Range("a" & Rows.Count).End(xlUp).Row + 1
     .Range("a" & arow) = TextBox1.Value
End With

But the textbox value is written in column A, line after line AND NOT next to the 1st item of listbox1 that is displayed in the column B
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
How about
Code:
With Sheets("Data")
   NextRow = .Range("B" & Rows.Count).End(xlUp).Row + 1
   .Range("A" & NextRow).Value = TextBox1.Value
   For b = 0 To ListBox1.ListCount - 1
      .Range("B" & NextRow) = ListBox1.list(b)
      NextRow = NextRow + 1
   Next b
End With
 
Upvote 0
So many thanks @Fluff that works perfectly :) !!

Just a note, when I activate the commandbutton of the userform, indeed it displays the items in the listbox1 in the column B and the textbox value in the column A. Do you have any idea how to add 2 empty rows at each time I activate the commandbutton, because I would like for each statement of the listbox 1 be separated by 2 empty rows...

I've changed the items in red, but it adds one empty row in the column B between each items of the listbox 2 and I rather would like 2 empty rows at each time I activate the command button. Any ideas ?

For b = 0 To ListBox1.ListCount - 1
With Sheets("Data")
nextrow = .Range("B" & Rows.Count).End(xlUp).Row + 2
.Range("B" & nextrow) = ListBox1.List(b)
End With
Next
 
Upvote 0
Thanks so much @Fluff. That works perfectly!! :)

Just a last question, I know many of VBA user are against the use of merged cells but do you have any idea how to merge the cells of column A where it is written the textbox value depending of the number of items of listbox 1 written in column B ?
 
Upvote 0
Simple answer is DON'T!
Merged cells are an abomination & will only cause you major problems in the future.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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