To Download this file Please goto end of this Page........................
Today we will learn from a different view. Generally there is some restrictions to add items with more than 10 column through ListBox.AddItem method.
But in this video I will use 15 column on listbox through listbox.additem method. If you interested or want to learn then watch this tutorial or follow this blogspot.VBA code which is used in this tutorials
Option Explicit' this is for only one checkbox will checked and others will non-checkedPrivate Sub CheckBox1_Click()If Me.CheckBox1 = True ThenMe.CheckBox2 = FalseMe.CheckBox3 = FalseMe.CheckBox4 = FalseMe.CheckBox5 = FalseMe.CheckBox6 = FalseMe.CheckBox7 = FalseEnd IfEnd SubPrivate Sub CheckBox2_Click()If Me.CheckBox2 = True ThenMe.CheckBox1 = FalseMe.CheckBox3 = FalseMe.CheckBox4 = FalseMe.CheckBox5 = FalseMe.CheckBox6 = FalseMe.CheckBox7 = FalseEnd IfEnd SubPrivate Sub CheckBox3_Click()If Me.CheckBox3 = True ThenMe.CheckBox2 = FalseMe.CheckBox1 = FalseMe.CheckBox4 = FalseMe.CheckBox5 = FalseMe.CheckBox6 = FalseMe.CheckBox7 = FalseEnd IfEnd SubPrivate Sub CheckBox4_Click()If Me.CheckBox4 = True ThenMe.CheckBox2 = FalseMe.CheckBox3 = FalseMe.CheckBox1 = FalseMe.CheckBox5 = FalseMe.CheckBox6 = FalseMe.CheckBox7 = FalseEnd IfEnd SubPrivate Sub CheckBox5_Click()If Me.CheckBox5 = True ThenMe.CheckBox2 = FalseMe.CheckBox3 = FalseMe.CheckBox4 = FalseMe.CheckBox1 = FalseMe.CheckBox6 = FalseMe.CheckBox7 = FalseEnd IfEnd SubPrivate Sub CheckBox6_Click()If Me.CheckBox6 = True ThenMe.CheckBox2 = FalseMe.CheckBox3 = FalseMe.CheckBox4 = FalseMe.CheckBox5 = FalseMe.CheckBox1 = FalseMe.CheckBox7 = FalseEnd IfEnd SubPrivate Sub CheckBox7_Click()If Me.CheckBox7 = True ThenMe.CheckBox2 = FalseMe.CheckBox3 = FalseMe.CheckBox4 = FalseMe.CheckBox5 = FalseMe.CheckBox6 = FalseMe.CheckBox1 = FalseEnd IfEnd Sub' ......................................................................................................................Private Sub CommandButton1_Click()Me.TextBox1 = LCase(Me.TextBox1)If Me.CheckBox7 = True ThenCall search_allExit SubEnd IfDim sh As WorksheetSet sh = Sheets("Sheet1")Dim i As LongDim x As LongDim p As Integer, k As IntegerIf Me.CheckBox1 = True Thenk = 1 ' A colEnd IfIf Me.CheckBox2 = True Thenk = 2End IfIf Me.CheckBox3 = True Thenk = 3End IfIf Me.CheckBox4 = True Thenk = 6End IfIf Me.CheckBox5 = True Thenk = 10End IfIf Me.CheckBox6 = True Thenk = 11End IfWith Me.ListBox1.Clear.ColumnCount = 15.Font.Size = 9.List = sh.Range("A1", "O1").Value.RemoveItem 0.ColumnWidths = "100,100,80,60,60,80,60,70,80,80,80,50,50,50,40".AddItem.List(0, 0) = "Title".List(0, 1) = "AUTHOR".List(0, 2) = "EDITOR/TRANSLATOR".List(0, 3) = "EDITION/YEAR".List(0, 4) = "IMPRINT".List(0, 5) = "SUBJECT".List(0, 6) = "PAGE/VOL".List(0, 7) = "TYPE".List(0, 8) = "CALL NO".List(0, 9) = "ISBN".List(0, 10) = "ACC NO".List(0, 11) = "PRICE".List(0, 12) = "RACK".List(0, 13) = "SHELF".List(0, 14) = "SERIAL NO".Selected(0) = TrueFor i = 2 To sh.Range("A" & Rows.Count).End(xlUp).RowFor x = 1 To Len(sh.Cells(i, 1))p = Me.TextBox1.TextLengthIf LCase(Mid(sh.Cells(i, k), x, p)) = Me.TextBox1 And Me.TextBox1 <> "" Then.AddItem.List(.ListCount - 1, 0) = sh.Cells(i, 1).List(.ListCount - 1, 1) = sh.Cells(i, 2).List(.ListCount - 1, 2) = sh.Cells(i, 3).List(.ListCount - 1, 3) = sh.Cells(i, 4).List(.ListCount - 1, 4) = sh.Cells(i, 5).List(.ListCount - 1, 5) = sh.Cells(i, 6).List(.ListCount - 1, 6) = sh.Cells(i, 7).List(.ListCount - 1, 7) = sh.Cells(i, 8).List(.ListCount - 1, 8) = sh.Cells(i, 9).List(.ListCount - 1, 9) = sh.Cells(i, 10).List(.ListCount - 1, 10) = sh.Cells(i, 11).List(.ListCount - 1, 11) = sh.Cells(i, 12).List(.ListCount - 1, 12) = sh.Cells(i, 13).List(.ListCount - 1, 13) = sh.Cells(i, 14).List(.ListCount - 1, 14) = sh.Cells(i, 15)End IfNext xNext iEnd WithEnd SubPrivate Sub UserForm_Initialize()Me.CheckBox7 = TrueEnd Sub
'........................................................................................................................................
If you want to search item from any where from your database, then just follow the codes carefully. Here I created a module and copy the code which is used in the commandbutton to search item by keywords of TextBox. Here I replaced me with ListBox1 and reset the k value which is from 1 to 15.
Option Explicit
Sub search_all()
UserForm2.TextBox1 = LCase(UserForm2.TextBox1)
Dim sh As Worksheet
Set sh = Sheets("Sheet1")
Dim i As Long
Dim x As Long
Dim p As Integer, k As Integer
With UserForm2.ListBox1
.Clear
.ColumnCount = 15
.Font.Size = 9
.List = sh.Range("A1", "O1").Value
.RemoveItem 0
.ColumnWidths = "100,100,80,60,60,80,60,70,80,80,80,50,50,50,40"
.AddItem
.List(0, 0) = "Title"
.List(0, 1) = "AUTHOR"
.List(0, 2) = "EDITOR/TRANSLATOR"
.List(0, 3) = "EDITION/YEAR"
.List(0, 4) = "IMPRINT"
.List(0, 5) = "SUBJECT"
.List(0, 6) = "PAGE/VOL"
.List(0, 7) = "TYPE"
.List(0, 8) = "CALL NO"
.List(0, 9) = "ISBN"
.List(0, 10) = "ACC NO"
.List(0, 11) = "PRICE"
.List(0, 12) = "RACK"
.List(0, 13) = "SHELF"
.List(0, 14) = "SERIAL NO"
.Selected(0) = True
For i = 2 To sh.Range("A" & Rows.Count).End(xlUp).Row
For x = 1 To Len(sh.Cells(i, 1))
p = UserForm2.TextBox1.TextLength
For k = 1 To 15
If LCase(Mid(sh.Cells(i, k), x, p)) = UserForm2.TextBox1 And UserForm2.TextBox1 <> "" Then
.AddItem
.List(.ListCount - 1, 0) = sh.Cells(i, 1)
.List(.ListCount - 1, 1) = sh.Cells(i, 2)
.List(.ListCount - 1, 2) = sh.Cells(i, 3)
.List(.ListCount - 1, 3) = sh.Cells(i, 4)
.List(.ListCount - 1, 4) = sh.Cells(i, 5)
.List(.ListCount - 1, 5) = sh.Cells(i, 6)
.List(.ListCount - 1, 6) = sh.Cells(i, 7)
.List(.ListCount - 1, 7) = sh.Cells(i, 8)
.List(.ListCount - 1, 8) = sh.Cells(i, 9)
.List(.ListCount - 1, 9) = sh.Cells(i, 10)
.List(.ListCount - 1, 10) = sh.Cells(i, 11)
.List(.ListCount - 1, 11) = sh.Cells(i, 12)
.List(.ListCount - 1, 12) = sh.Cells(i, 13)
.List(.ListCount - 1, 13) = sh.Cells(i, 14)
.List(.ListCount - 1, 14) = sh.Cells(i, 15)
End If
Next k
Next x
Next i
End With
End Sub
The following lines are ver important to additems more than 10 column using ListBox.AddItem method
With UserForm2.ListBox1
.Clear
.ColumnCount = 15
.Font.Size = 9
.List = sh.Range("A1", "O1").Value
.RemoveItem 0
.ColumnWidths = "100,100,80,60,60,80,60,70,80,80,80,50,50,50,40"
End with
I hope You will find your quaries as resolved.
For Support and FeedBack
Please follow my blogspot and subscribe my channel
If you want to download this file Please click on the download button. Download link will show you after 10 sec.
This file will be really helpful and there is lot of things to learn
ReplyDeleteIt does not work
ReplyDelete