Excel

Data Entry - Multiple Column List Box

Ease of Use

Intermediate

Version tested with

2000, 2002, 2003 

Submitted by:

sheeeng

Description:

It has a user form display the value from Excel spreadsheet in multiple column list box. 

Discussion:

I wanted to try out Grid Box or Data Grid at first in VBA. But later I found out VBA only support multiple column list box for similar purposes. I just had in my mind of an example of recording name and telephone in a multiple column list box. So, here is the results of my research. Do give suggestion. I'm hoping to hear it. Thanks. 

Code:

instructions for use

			

'This is my first KB. 'Thanks to all who helped me along this forum. 'It is my time to contribute to VBAEXpress. 'UserForm1 Code Option Explicit Private Sub cmdAdd_Click() Dim strLastRow As Integer 'Get last row strLastRow = xlLastRow("Sheet1") With UserForm1 'If textboxes not null then fill data of textboxes to worksheet. If (.TextBox1.Value <> vbNullString And .TextBox2.Value <> vbNullString And _ .TextBox3.Value <> vbNullString) Then Cells(strLastRow + 1, 1).Value = UserForm1.TextBox1.Value Cells(strLastRow + 1, 2).Value = UserForm1.TextBox2.Value Cells(strLastRow + 1, 3).Value = UserForm1.TextBox3.Value strLastRow = strLastRow + 1 'Update listbox with added values UserForm1.ListBox1.RowSource = "Sheet1!A2:C" & strLastRow 'Empty textboxes .TextBox1.Value = vbNullString .TextBox2.Value = vbNullString .TextBox3.Value = vbNullString Else MsgBox "Please Enter Data" End If End With End Sub Private Sub cmdDel_Click() With UserForm1.ListBox1 'Check for selected item If (.Value <> vbNullString) Then 'If more then one data rows If (.ListIndex >= 0 And xlLastRow("Sheet1") > 2) Then Range(.RowSource)(.ListIndex + 1, 1).EntireRow.Delete 'Update listbox .RowSource = "Sheet1!A2:C" & xlLastRow("Sheet1") 'If only one data row ElseIf (.ListIndex = 0 And xlLastRow("Sheet1") = 2) Then Range(.RowSource)(.ListIndex + 1, 1).EntireRow.Delete 'Update listbox .RowSource = "Sheet1!A2:C2" End If Else MsgBox "Please Select Data" End If End With End Sub Private Sub ListBox1_Change() Dim SourceRange As Excel.Range Dim Val1 As String, Val2 As String, Val3 As String If (ListBox1.RowSource <> vbNullString) Then 'Get Range that the ListBox is bound to Set SourceRange = Range(ListBox1.RowSource) Else 'Get first data row Set SourceRange = Range("Sheet1!A2:C2") Exit Sub End If Val1 = ListBox1.Value 'Get the value of the second column Val2 = SourceRange.Offset(ListBox1.ListIndex, 1).Resize(1, 1).Value 'Get the value of the third column Val3 = SourceRange.Offset(ListBox1.ListIndex, 2).Resize(1, 1).Value 'Concatenate the three values together and display them in Label1 Label1.Caption = "Selected Data: " & vbNewLine & Val1 & " " & Val2 & " " & Val3 'Clean Up Set SourceRange = Nothing End Sub Private Sub UserForm_Initialize() 'Clean data range DeleteBlankRows DeleteBlankColumns 'Set properties of listbox1 With Me.ListBox1 .BoundColumn = 1 .ColumnCount = 3 .ColumnHeads = True .TextColumn = True .RowSource = "Sheet1!A2:C" & xlLastRow("Sheet1") .ListStyle = fmListStyleOption .ListIndex = 0 End With End Sub 'Module1 Implement KB Below 'Delete blank rows or columns 'By mdmackillop (www.vbaexpress.com) 'KB Entry: http://www.vbaexpress.com/kb/getarticle.php?kb_id=395 'Thanks to mdmackillop Option Explicit Sub DeleteBlankRows() Dim Rw As Long, RwCnt As Long, Rng As Excel.Range Application.ScreenUpdating = False Application.Calculation = xlCalculationManual On Error GoTo Exits: 'Determine range to process If Selection.Rows.Count > 1 Then 'Rng = selected cells Set Rng = Selection Else 'Rng = filled cells in row 1 Set Rng = Range(Rows(1), _ Rows(ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row())) End If RwCnt = 0 'Loop through all rows For Rw = Rng.Rows.Count To 1 Step -1 'If entire row is blank If Application.WorksheetFunction.CountA(Rng.Rows(Rw).EntireRow) = 0 Then 'Delete row Rng.Rows(Rw).EntireRow.Delete RwCnt = RwCnt + 1 End If Next Rw Exits: Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic 'Clean up Set Rng = Nothing End Sub Sub DeleteBlankColumns() Dim Col As Long, ColCnt As Long, Rng As Excel.Range Application.ScreenUpdating = False Application.Calculation = xlCalculationManual On Error GoTo Exits: 'Determine range to process If Selection.Columns.Count > 1 Then 'Rng = selected cells Set Rng = Selection Else 'Rng = filled cells in column 1 Set Rng = Range(Columns(1), Columns(ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column())) End If ColCnt = 0 'Loop through all columns For Col = Rng.Columns.Count To 1 Step -1 'If entire column is blank If Application.WorksheetFunction.CountA(Rng.Columns(Col).EntireColumn) = 0 Then 'Delete Column Rng.Columns(Col).EntireColumn.Delete ColCnt = ColCnt + 1 End If Next Col Exits: Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic 'Clean up Set Rng = Nothing End Sub 'Module2 Implement KB Below 'Using Functions to Get a Range 'By Brandtrock (www.vbaexpress.com) 'KB Entry: http://www.vbaexpress.com/kb/getarticle.php?kb_id=496 'Thanks to Brandtrock Option Explicit Dim MyRange As Excel.Range Sub MoveIt() 'Determine used range of active worksheet using 4 fuctions (Below) Set MyRange = Range((Cells(xlFirstRow, xlFirstCol)), (Cells(xlLastRow, xlLastCol))) MyRange.Copy Destination:=Sheets("Sheet2").Range("B4") 'this range can be changed to whatever you like 'Clean up Set MyRange = Nothing End Sub Function xlFirstCol(Optional WorksheetName As String) As Long 'Check for optional worksheetname else use activesheet If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name End If ' find the first populated column in a worksheet With Worksheets(WorksheetName) xlFirstCol = .Cells.Find("*", .Cells(1), xlFormulas, _ xlWhole, xlByColumns, xlNext).Column End With End Function Function xlFirstRow(Optional WorksheetName As String) As Long 'Check for optional worksheetname else use activesheet If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name End If ' find the first populated row in a worksheet With Worksheets(WorksheetName) xlFirstRow = .Cells.Find("*", .Cells(1), xlFormulas, _ xlWhole, xlByRows, xlNext).Row End With End Function Function xlLastRow(Optional WorksheetName As String) As Long 'Check for optional worksheetname else use activesheet If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name End If ' find the last populated row in a worksheet With Worksheets(WorksheetName) xlLastRow = .Cells.Find("*", .Cells(1), xlFormulas, _ xlWhole, xlByRows, xlPrevious).Row End With End Function Function xlLastCol(Optional WorksheetName As String) As Long 'Check for optional worksheetname else use activesheet If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name End If ' find the last populated column in a worksheet With Worksheets(WorksheetName) xlLastCol = .Cells.Find("*", .Cells(1), xlFormulas, _ xlWhole, xlByColumns, xlPrevious).Column End With End Function 'Module3 'Add a button from Forms Toolbar and Assign Macro. Sub Button1_Click() 'Show the form UserForm1.Show End Sub

How to use:

  1. Use the Simple Form GUI.
  2. To ADD data into spreadsheet though list box.
  3. Just enter the value in text boxes and click Add button.
  4. To DELETE data from spreadsheet though list box.
  5. Just click items in list box and click Delete button.
 

Test the code:

  1. Press the button to show the form
  2. Try Add and Delete data as you like.
  3. NOTE: It will not delete redundant data.
 

Sample File:

Mulitple Column List Box.zip 20.98KB 

Approved by mdmackillop


This entry has been viewed 708 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express