Home » excel » excel vba – How to add columns to a listview in visual basic?

excel vba – How to add columns to a listview in visual basic?

Posted by: admin May 14, 2020 Leave a comment

Questions:

This is probably an easy question, but I am new to visual basic and I’m following tutorials online, I have this to add columns to a listview, but I’m getting compilation errors. How do I add columns?

Private Sub UserForm_Initialize()

    ListView1.Columns.Add("Column1", 100, HorizontalAlignment.Left)
    ListView1.Columns.Add("Column2", 100, HorizontalAlignment.Left)
    ListView1.Columns.Add("Column3", 100, HorizontalAlignment.Left)
    ListView1.Columns.Add("Column4", 100, HorizontalAlignment.Left)

End Sub

And do you know any good manuals for vb? Most of what I’ve found aren’t as clear as tutorials for other languages.

Thanks

How to&Answers:

This worked.

ListView1.View = lvwReport
ListView1.ColumnHeaders.Add , , "COLUMN1", 100
ListView1.ColumnHeaders.Add , , "COLUMN2", 100
ListView1.ColumnHeaders.Add , , "COLUMN3", 100
ListView1.ColumnHeaders.Add , , "COLUMN4", 100

Answer:

I believe you are wanting to call ListView1.ColumnHeaders.Add instead of ListView1.Columns.Add.