Add Columns to the Listbox to the Dynamically

  • Change the height and Width of the Userform
  • Change the height and Width of the Listbox
  • Insert multiple columns dynamically
  •  

    Download The Workbook

     

    Public sht As Worksheet
    Sub Four_Columns()

    Set sht = ActiveWorkbook.Sheets("Four_Cols")
    sht.Activate
    Dim Colcount As Integer
    Colcount = sht.Range("A1").CurrentRegion.Columns.Count

    Dim RngAddress As String
    LastRow = sht.Range("A1").End(xlDown).Row

    RngAddress = sht.Range(Cells(1, 1), Cells(LastRow, Colcount)).Address
    FormatForm = Format_Userform(Colcount, RngAddress)

    End Sub
    Sub Eleven_Columns()
    Set sht = ActiveWorkbook.Sheets("Eleven_Cols")
    sht.Activate
    Dim Colcount As Integer
    Colcount = sht.Range("A1").CurrentRegion.Columns.Count
    Dim RngAddress As String
    LastRow = sht.Range("A1").End(xlDown).Row

    RngAddress = sht.Range(Cells(1, 1), Cells(LastRow, Colcount)).Address
    FormatForm = Format_Userform(Colcount, RngAddress)
    End Sub

    Sub Single_Column()
    Set sht = ActiveWorkbook.Sheets("Single_Col")
    Dim Colcount As Integer
    Colcount = sht.Range("A1").CurrentRegion.Columns.Count
    sht.Activate
    Dim RngAddress As String
    LastRow = sht.Range("A1").End(xlDown).Row

    RngAddress = sht.Range(Cells(1, 1), Cells(LastRow, Colcount)).Address
    FormatForm = Format_Userform(Colcount, RngAddress)
    End Sub

    Sub Two_Columns()
    Set sht = ActiveWorkbook.Sheets("Two_Columns")
    Dim Colcount As Integer
    Colcount = sht.Range("A1").CurrentRegion.Columns.Count
    sht.Activate
    Dim RngAddress As String
    LastRow = sht.Range("A1").End(xlDown).Row

    RngAddress = sht.Range(Cells(1, 1), Cells(LastRow, Colcount)).Address
    FormatForm = Format_Userform(Colcount, RngAddress)
    End Sub

    Sub ThreeRows()
    Set sht = ActiveWorkbook.Sheets("Three_Rows")
    Dim Colcount As Integer
    Colcount = sht.Range("A1").CurrentRegion.Columns.Count
    sht.Activate
    Dim RngAddress As String
    LastRow = sht.Range("A1").End(xlDown).Row

    RngAddress = sht.Range(Cells(1, 1), Cells(LastRow, Colcount)).Address
    FormatForm = Format_Userform(Colcount, RngAddress)
    End Sub
    Function Format_Userform(Colcount, RngAddress)
    '==============================
    'Define the Min and Max Measurements of Userform and Listbox
    UFMinHeight = 400
    UFMaxHeight = 450
    UFMinWidth = 400
    UFMaxWidth = 700
    LBMinWidth = 110
    LBMaxWidth = 600
    '==============================
    Dim UserformWidth As Long
    UserformWidth = Colcount * 110


    If UserformWidth > UFMaxWidth Then
    UserformWidth = UFMaxWidth
    End If

    If UserformWidth < UFMinWidth Then
    UserformWidth = UFMinWidth
    End If

    'Defined the height of the Userform based on Number of rows in a worksheet
    Dim UserformHeight As Long
    UserformHeight = sht.Range("A1").CurrentRegion.Rows.Count * 18

    'Add additional 200 for command buttons
    UserformHeight = UserformHeight + 200

    If UserformHeight > UFMaxHeight Then
    UserformHeight = UFMaxHeight
    End If

    If UserformHeight < UFMinHeight Then
    UserformHeight = UFMinHeight
    End If

    '===========Format the Userform================
    With UserForm1
    .Width = UserformWidth
    .Height = UserformHeight
    .BackColor = RGB(210, 95, 95)
    .BorderColor = RGB(255, 0, 0)
    .BorderStyle = fmBorderStyleSingle
    .Caption = "Select The Data"
    End With
    '==========Format the Listbox======================

    With UserForm1.ListBox1
    .Left = 10
    .Top = 15

    'From the Userform Height deducted 200 to place the command buttons
    ListBoxHeight = UserformHeight - 200
    .Height = ListBoxHeight

    'Assigned 110 for each column of Listbox
    ListboxWidth = 110 * Colcount

    'Maintained the Minimum width
    If ListboxWidth < 110 Then
    ListboxWidth = LBMinWidth
    End If

    'if Listbox width exceeds Userform width then .. use Listbox max width
    If ListboxWidth > UserformWidth Then
    ListboxWidth = LBMaxWidth
    End If

    .Width = ListboxWidth
    .ColumnCount = Colcount
    .ColumnHeads = False

    'Provided the column width as 90
    For C = 1 To Colcount
    ColWidth = ColWidth & ";" & 90
    Next
    ColWidth = Right(ColWidth, Len(ColWidth) - 1)
    .ColumnWidths = ColWidth
    .RowSource = "=" & sht.Name & "!" & RngAddress
    .ListStyle = fmListStyleOption
    .MultiSelect = fmMultiSelectMulti
    .Font.Size = 18
    .ForeColor = RGB(0, 0, 0)
    .Font.Name = "Estrangelo Edessa"
    .BackColor = RGB(250, 240, 205)
    .TextAlign = fmTextAlignLeft
    End With

    '========Formatting CommandButton1=====
    With UserForm1.CommandButton1
    .BackColor = RGB(140, 0, 0)
    .ForeColor = RGB(255, 255, 255)
    .Font.Size = 15
    .Font.Name = "Estrangelo Edessa"
    .Visible = True
    'From the userform height deducted Listbox Top + Listbox Height + Commandbutton Height
    RemainingSpace = UserformHeight - 15 - ListBoxHeight - 30

    'Centered the remaing space to place the command buttons
    HalfReaminingSpace = RemainingSpace / 2
    .Top = 15 + ListBoxHeight + HalfReaminingSpace
    .Left = 25
    End With

    '=======Formatting CommandButton2==============
    With UserForm1.CommandButton2
    .BackColor = RGB(140, 0, 0)
    .ForeColor = RGB(255, 255, 255)
    .Font.Size = 15
    .Font.Name = "Estrangelo Edessa"
    .Visible = True
    .Top = 15 + ListBoxHeight + HalfReaminingSpace
    .Left = UserformWidth - 150

    End With



    'Show Userform
    UserForm1.Show
    ColWidth = ""

    Set sht = Nothing
    RngAddress = ""
    ActiveWorkbook.Sheets("Buttons").Activate
    End Function