Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having following code to populate a listview in userform. However, I would

ID: 3560528 • Letter: I

Question

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

Explanation / Answer

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