Indenting Items in a List Box

Boswell

Board Regular
Joined
Jun 18, 2010
Messages
224
Hello All,

I have a multicolumn listbox that is populated with elements that are in the parent -> child -> grandchild format. The elements labels are as follows:
Code:
01 - Text
01.01 - Text
01.01.01 - Text
01.01.02 - Text
The listbox is populated by RowSource. I would like to indent each item in the listbox according to its position in the hierarchy. Similar to the following:
Code:
01 - Text
   01.01 - Text
      01.01.01 - Text
      01.01.02 - Text
My code for performing the indent process is as follows:
Code:
Sub ListBox_Indent()

    Dim i As Long
    Dim j As Long
    Dim c As String
    Dim r As Long
    Dim s As Long
    Dim ind As String
    
    With UserForm1.ListBox1
        For i = 0 To .ListCount - 1

            c = .Column(2, i)
            r = Len(c)
            s = Len(WorksheetFunction.Substitute(c, ".", ""))
            
            If (r - s) = 0 Then
                'do nothing
            Else
                ind = String((r - s), " ")
                c = ind & c
                .Column(2, i) = c '<--Error Here
            End If
        Next i
    End With
    
End Sub
I'm having trouble applying the Additem property to the multicolumn listbox. Essentially what I want to do is reference an item in a listbox, edit it by adding some spaces to it, and then plug it back in to the same position in the listbox.

Any suggestions would be greatly appreciated.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
If it's populated with Rowsource, you can't edit the items or use AddItem.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,754
Members
452,940
Latest member
rootytrip

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