Excel VBA - How to remove the extra column in ListView

Anonymous
2023-11-07T13:13:36+00:00

Dear All,

I'm using a ListView to store the data of a recordset I have created.

Everything is working fine but I noticed I got an extra column at the end (see the red selection in the first below image):

Any way to remove it?

Any property can it be set?

This is the actual behavior:

What I need:

Thanks

Microsoft 365 and Office | Excel | Other | 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
{count} votes
Answer accepted by question author
  1. Rory Archibald 18,875 Reputation points Volunteer Moderator
    2023-11-07T15:59:08+00:00

    Looking closer at that picture, I don't think that is a column at all, I think your control is wider than your total column widths.

    2 people found this answer helpful.
    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Rory Archibald 18,875 Reputation points Volunteer Moderator
    2023-11-07T15:34:43+00:00

    It's not really a good idea to use a class name as a variable name, as it makes your code less clear, but I suspect you just need to clear the column headers first:

    Public Sub ListView_Header(ListView As ListView, Rst As ADODB.Recordset)
        Dim i As Integer
        ListView.ColumnHeaders.Clear
        For i = 0 To Rst.Fields.Count - 1
            ListView.ColumnHeaders.Add , , Rst.Fields.Item(i).Name, ListView.Width
        Next i
        ListView.View = lvwReport
    End Sub
    
    0 comments No comments
  2. Anonymous
    2023-11-07T15:44:33+00:00

    It's not really a good idea to use a class name as a variable name, as it makes your code less clear, but I suspect you just need to clear the column headers first:

    Public Sub ListView_Header(ListView As ListView, Rst As ADODB.Recordset)
        Dim i As Integer
        ListView.ColumnHeaders.Clear
        For i = 0 To Rst.Fields.Count - 1
            ListView.ColumnHeaders.Add , , Rst.Fields.Item(i).Name, ListView.Width
        Next i
        ListView.View = lvwReport
    End Sub
    

    Thanks!

    This is OK but it is not solving my issue in fact I still have an additional column at the end of the table

    0 comments No comments
  3. Anonymous
    2023-11-08T10:18:22+00:00

    Looking closer at that picture, I don't think that is a column at all, I think your control is wider than your total column widths.

    You are right and thanks to your comment I have changed my code to:

    Public Sub AutoResizeListView(MyListView As Variant, Userfrm As MSForms.UserForm)
        Dim TempLabel As Object
        Dim MaxColumnWidth As Double
        Dim TempLastColumnWidth As Double
        Dim SumMaxColumnWidth As Double
        Dim i As Integer
        Dim j As Integer
    
        Set TempLabel = Userfrm.Controls.Add("Forms.Label.1", "Test Label", True)
        With TempLabel
            .Font.Size = MyListView.Font.Size
            .Font.Name = MyListView.Font.Name
            .WordWrap = False
            .AutoSize = True
            .Visible = False
        End With
        TempLabel.Caption = MyListView.ColumnHeaders(1).Text
        MaxColumnWidth = TempLabel.Width
        For i = 1 To MyListView.ListItems.Count
            TempLabel.Caption = MyListView.ListItems(i).Text
            If TempLabel.Width > MaxColumnWidth Then
                MaxColumnWidth = TempLabel.Width
            End If
        Next i
        MyListView.ColumnHeaders(1).Width = MaxColumnWidth + 8
        SumMaxColumnWidth = MyListView.ColumnHeaders(1).Width
        For i = 1 To MyListView.ColumnHeaders.Count - 1
            TempLabel.Caption = MyListView.ColumnHeaders(i + 1).Text
            MaxColumnWidth = TempLabel.Width
            For j = 1 To MyListView.ListItems.Count
                TempLabel.Caption = MyListView.ListItems(j).SubItems(i)
                If TempLabel.Width > MaxColumnWidth Then
                    MaxColumnWidth = TempLabel.Width
                End If
            Next j
            If (i + 1) = MyListView.ColumnHeaders.Count Then
                TempLastColumnWidth = MaxColumnWidth + 8
                SumMaxColumnWidth = SumMaxColumnWidth + TempLastColumnWidth
            Else
                MyListView.ColumnHeaders(i + 1).Width = MaxColumnWidth + 8
                SumMaxColumnWidth = SumMaxColumnWidth + MyListView.ColumnHeaders(i + 1).Width
            End If
            If SumMaxColumnWidth < MyListView.Width And (i + 1) = MyListView.ColumnHeaders.Count Then
                MyListView.ColumnHeaders(i + 1).Width = (MyListView.Width - SumMaxColumnWidth) + TempLastColumnWidth
            Else
                MyListView.ColumnHeaders(i + 1).Width = MaxColumnWidth + 8
            End If
        Next i
        Userfrm.Controls.Remove TempLabel.Name
    End Sub
    
    0 comments No comments