Display all the items of a listbox (userform) in specific merged cells ?

Doflamingo

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

I try to design an automate structure/organizational chart and I'm just at the Beginning of my project

First, Here is the code of the List Box with the button of the macro that displays all the items contained in the listbox


Private Sub CommandButton_Click()

Dim I As Long

For I = 0 To ListBox1.ListCount - 1

Cells(1, I + 1) = ListBox1.List(I)

end sub

All the items of the listbox are displayed in a page of an excel spreadsheet in the same row in different columns, like that :
Item A (one column) Item B (one column) Item C (one column) etc.

but what I would like is to have all the items displayed like that ;
item A in a merged cell (2 colums), Empty column, item B in a merged cell (2 colums), Empty column, item C in a merged cell (2 colums), etc.

Any ideas ?:confused:
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
This will enter the values in every other column
Code:
   Dim i As Long
   For i = 0 To ListBox1.ListCount - 1
      Cells(1, i * 2 + 1) = ListBox1.List(i)
   Next i
However I would STRONGLY advise against using merged cells, as they will only come back to haunt you.
 
Upvote 0
Thank you so much Fluff !!

Indeed it allows me to displays all the item of the listbox (item a, item b, item c etc.) almost like I wanted:

Item A (one column), Empty column, Item B (one column), Empty column, item C (one column), Empty column etc.

I've found another lines of code that allows me to merge in two column the last item of the listbox. Here is the code

Private Sub CommandButton4_Click()


Dim i As Long
Dim MyRange As Worksheet
a = 1


For i = 0 To ListBox1.ListCount - 1
Cells(1, i + 1) = ListBox1.List(i)

Cells(i, i + 3).Select

Set MyRange = Sheets("Sheet5")
With MyRange
.Range(.Cells(1, a + 3), .Cells(1, a + 4)).Merge
End With

But what I would like is to combine your code and this one, maybe a little bit modified to get this;

item A in a merged cell (2 colums), Empty column, item B in a merged cell (2 colums), Empty column, item C in a merged cell (2 colums), etc.

Any ideas ?:confused:

Thanks again for your answer :)
 
Upvote 0
Any ideas ?
yes, DO NOT use merged cells.
They play havoc with VBA, filtering, sorting, among other things.
Have a look at the link supplied by @Joe4
 
Upvote 0
Hi all,

Indeed, I think it would be wiser to give up the idea of merged cells,

Given that the final goal is to automate a structure chart,

here is the beginning of my code:

Dim i As Long
For i = 0 To ListBox1.ListCount - 1
Cells(5, i * 3 + 3) = ListBox1.List(i)

Cells(5, i * 3 + 3).Select


With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With

With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With

ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 144, 136.5, 216, 208.5). _
Select
Selection.ShapeRange.ShapeStyle = msoLineStylePreset15
Selection.ShapeRange.IncrementLeft -24
Selection.ShapeRange.IncrementTop -45
Selection.ShapeRange.ScaleWidth 0, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 1.25, msoFalse, msoScaleFromTopLeft



Next i

I've succeeded to put a connector to one of the item of the listbox, the next step and goal is to put connector to all the item of the listbox and... the next step will be link a specific connector between 2 items of an item coming from listbox A and one item coming from listbox B.

Any ideas ? :confused:
 
Upvote 0
As this is now a completely different question , you will need to start a new thread.
Thanks
 
Upvote 0
Hello @Joe4,

I relaunch you about the link you gave me.

It's great to ''merge'' something horizontally but it drives me crazy about how to merge it vertically ...

Any ideas :confused:
 
Upvote 0
It's great to ''merge'' something horizontally but it drives me crazy about how to merge it vertically ...

Any ideas :confused:
I have never seen a suitable replacement for vertical merging.
I suppose you could just play around with row height, wrapping text, and vertical centering, and might be able to do whatever it is you are trying to accomplish.
I just make it a point to never merge cells, ever.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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