empty row with listbox values

Doflamingo

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

I have a code that allows me to put in the column B of an Excel sheet all the items displayed in a listbox

Here is the code

Code:
Sheet9.Activate
For b = 0 To ListBox1.ListCount - 1
With Cells(b + 3, 2)
        .ColumnWidth = 15
        .Value = ListBox1.List(b)
End With
Next

But If the items of the listbox change, I would like the new ones be written below the previous ones
I have a code for an empty row, but How to adapt it with the column B ?

Here is the code for empty row

Code:
Dim emptyRow As Long
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
Cells(emptyRow, 2).Value = ListBox1.Value

Any ideas ? :confused:
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I have a code that allows me to put in the column B of an Excel sheet all the items displayed in a listbox

Here is the code

Code:
For b = 0 To ListBox1.ListCount - 1
    With Cells(b + 3, 2)
        .ColumnWidth = 15
        .Value = ListBox1.List(b)
    End With
Next
This single line of code will do what the above looping code does...
Code:
Range("B3").Resize(ListBox1.ListCount) = ListBox1.List



But If the items of the listbox change, I would like the new ones be written below the previous ones
I have a code for an empty row, but How to adapt it with the column B ?
How is the ListBox populated with its values? Will the "new" items be written into the ListBox underneath any existing items that are already there? If not, does the above mean you want to have the code pick out any new items that were added in between older items and put them in Column B under the list of previously existing items?
 
Last edited:
Upvote 0
Hi @Rick Rothstein

The listbox is populated with the values of different optionbutton
No the ''new items'' are not written into the ListBox underneath any existing items that are already there, given that I have a commandbutton that clear the listbox. Here is the code

Code:
listbox1.clear

What I would like to see is the new items of the listbox be written in the excel sheet in the column B AND just the line below the previous ones.

The code you gave me does not put the new items of the listbox in an empty row for each of them, just below the last item of the listbox written previously
But it change the previous ones since the range ''B3''

Code:
Dim emptyRow As Long
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
For b = 0 To ListBox1.ListCount - 1
Cells(b + 3, emptyRow + 1).Value = ListBox1.List(b)
Next
Range("b3").Resize(ListBox1.ListCount) = ListBox1.List
 
Upvote 0
The code you gave me does not put the new items of the listbox in an empty row for each of them, just below the last item of the listbox written previously
I never said it would... what I said is it did exactly the same thing as the code you had posted. I could not answer your exact question without understanding how your data was set up which, I think, you have now answered. Let me think about how to implement that for you.
 
Upvote 0
Here is the answer

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
 
Upvote 0
Here is the answer

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

I think I may have misunderstood what you wanted. I thought you were going to add new items to an existing ListBox and that you wanted those new items pulled out from the existing list and placed on the worksheet, but the code you are showing says you will put whatever is in the Listbox when you run the code under the current list of items on the worksheet even if some of those items are duplicates. If that is what you want, here is my code modified to do that...
Code:
Cells(Rows.Count, "B").End(xlUp).Offset(1).Resize(ListBox1.ListCount) = ListBox1.List
Remember, this code is a replacement for the code you posted, not an addition to it.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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