VBA example - Excel 2007: VBA dictionary

VBA dictionary


In this example, We use VBA built-in function Ucase() to help us create a simple dictionary.  By using this function, we can search for a term in the list box without caring about case-sensitivity.

Besides using the VBA built-in function, this example also aims to provide you some techniques used in writing items to the list box, reading an item from it, and selecting the list item by VBA code.


To run this example VBA code, you will need a Form, a TextBox, and Two ListBox. You may type the terms and their translation text in to your current worksheet.

Term Translation
Abandon លះបង់់ចោល
Ancle ឪពុកមា
Bad អាក្រក់
Cat ឆ្មា
Damage បំផ្លាញ

VBA for Excel: VBA code to create a simple dictionary


VBA code for this example:

Private Sub ListBox1_Change()    
ListBox2.Clear 'clear list
   
ListBox2.AddItem Cells(ListBox1.ListIndex + 2, 2) 'show translation text
   
End Sub
 

Private Sub TextBox1_Change()
   
Dim i As Integer
   
i = 0
    For i = 0 To ListBox1.ListCount - 1        
If UCase(TextBox1.Text) = UCase(Left(ListBox1.List(i, 0), Len(TextBox1.Text))) Then
                      ListBox1.ListIndex = i 'select list item when search found        
End If
   
Next

End Sub
 

Private Sub UserForm_Initialize()
   
Dim i As Integer
    i = 2    
While Cells(i, 1) <> ""
       
ListBox1.AddItem Cells(i, 1) 'load terms in to the list
       
i = i + 1
   
Wend
    ListBox1.ListIndex = 0 'select the first item    
TextBox1.SetFocus 'focus the text box

End Sub




Comments

Name:
Comment:
Enter the text:
CAPTCHA image

...................................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word.
However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like