Share via

Hide Column in Listview

Anonymous
2014-08-09T09:03:03+00:00

Hello,

       I am having following code to populate a listview in userform. However, I would like to hide specific column for example I want to hide column number 2 & column 3. Can anybody help out:

Private Sub UserForm_Activate()

 'Set some of the properties for the ListView

       'Set some of the properties for the ListView

    With Me.ListView3

        .Gridlines = True

        .View = lvwReport

    End With

    'Call the sub to fill the ListView

    Call LoadListView

End Sub

Private Sub LoadListView()

    'Declare the variables

    Dim wksSource As Worksheet

    Dim rngData As Range

    Dim rngCell As Range

    Dim LstItem As ListItem

    Dim RowCount As Long

    Dim ColCount As Long

    Dim i As Long

    Dim j As Long

    'Set the source worksheet

    Set wksSource = Worksheets("Data")

    'Set the source range

    Set rngData = wksSource.Range("A6").CurrentRegion

    'Add the column headers

    For Each rngCell In rngData.Rows(2).Cells

        Me.ListView3.ColumnHeaders.Add Text:=rngCell.Value, Width:=60

    Next rngCell

    'Count the number of rows in the source range

    RowCount = rngData.Rows.count

    'Count the number of columns in the source range

    ColCount = rngData.Columns.count

    'Fill the ListView

    For i = 6 To RowCount

        Set LstItem = Me.ListView3.ListItems.Add(Text:=rngData(i, 1).Value)

        For j = 2 To ColCount

            LstItem.ListSubItems.Add Text:=rngData(i, j).Value

        Next j

    Next i

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2014-08-09T10:19:38+00:00

Pankaj,

You can set the width of  ColumnHeaders(2) to 0.

Like this code under CommandButton1:

Private Sub CommandButton1_Click()

    If Me.ListView3.ColumnHeaders(2).Width = 0 Then

        Me.ListView3.ColumnHeaders(2).Width = 60

        Me.ListView3.ColumnHeaders(3).Width = 60

    Else

        Me.ListView3.ColumnHeaders(2).Width = 0

        Me.ListView3.ColumnHeaders(3).Width = 0

    End If

End Sub

Jan

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2014-08-09T10:36:20+00:00

    Hi,

        Thanks a lot for the help, I tried to modified the same (Marked in bold) & I am getting the right output

    Private Sub UserForm_Activate()

    'Set some of the properties for the ListView

           'Set some of the properties for the ListView

        With Me.ListView3

            .Gridlines = True

            .View = lvwReport

        End With

        'Call the sub to fill the ListView

        Call LoadListView

        Me.Listview3.ColumnHeaders(2).Width= 0

    Me.Listview3.ColumnHeaders(3).Width= 0

    End Sub

    Private Sub LoadListView()

        'Declare the variables

        Dim wksSource As Worksheet

        Dim rngData As Range

        Dim rngCell As Range

        Dim LstItem As ListItem

        Dim RowCount As Long

        Dim ColCount As Long

        Dim i As Long

        Dim j As Long

        'Set the source worksheet

        Set wksSource = Worksheets("Data")

        'Set the source range

        Set rngData = wksSource.Range("A6").CurrentRegion

        'Add the column headers

        For Each rngCell In rngData.Rows(2).Cells

            Me.ListView3.ColumnHeaders.Add Text:=rngCell.Value, Width:=60

        Next rngCell

        'Count the number of rows in the source range

        RowCount = rngData.Rows.count

        'Count the number of columns in the source range

        ColCount = rngData.Columns.count

        'Fill the ListView

        For i = 6 To RowCount

            Set LstItem = Me.ListView3.ListItems.Add(Text:=rngData(i, 1).Value)

            For j = 2 To ColCount

                LstItem.ListSubItems.Add Text:=rngData(i, j).Value

            Next j

        Next i

    End Sub

    Was this answer helpful?

    0 comments No comments