Quantcast
Channel: [OTP] VBA Using combobox in Excel
Viewing all articles
Browse latest Browse all 14

[OTP] VBA Using combobox in Excel

$
0
0

Hello,

Here is one approach, Have two work sheets, "Countries" and the other "Main".  You could have one but I chose to have two so that the user does not see all the country and city data!.

Sheet "Countries" contains all the country and city data.

From Cell A1 downwards will be the list of countries.

The cities start from A2 across (I didn't think it would not be a good idea to have a sheet for each country).  So will look like this:

Argentina  ArgCity1  ArgCity2  ArgCity3 etc

Brazil         BraCity1  BraCity2 etc   

On sheet "main" embed two comboboxes ie combobox1 and combobox2.

For combobox1 set the "ListFillRange" to the range area covered by countries ie "countries!A1:A196".  (If you decide to use only one sheet you do not need to include "countries!" and change the code accordingly.

To set the ListFillRange, when in "design" mode, right click the combobox1 nd select properties and you will see all the properties for that combobox.

Still indesign mode, right click on combobox1 and select view code.  A coding screen will appear and type the following:

[Code Start]

Sub ComboBox1_Change()
Dim off As Integer
Dim thisCell As String
Dim thisRow As Integer

Application.ScreenUpdating = False                                 ' turn off screenupdating so you dont see screen flicker
Worksheets("countries").Select                                          ' select the sheet with list of countries and cities
thisRow = Worksheets("main").ComboBox1.ListIndex    ' Country list must start at row 1 for this to work
Worksheets("countries").Cells(thisRow + 1, 2).Select     ' select the cell that corresponds with listindex+1                                                                                                     ' (listindex 0 is row 1, listindex 1 is row 2 etc)
Worksheets("main").ComboBox2.Clear                           ' Clear all list items from combobox2
off = 0                                                                                    ' initialise offset
thisCell = ActiveCell.Offset(0, off).Value                          ' read current city
Do While thisCell <> ""                                                       '    while the city is not blank
  Worksheets("main").ComboBox2.AddItem (thisCell)         '      add this city to combobox2's list
  off = off + 1                                                                               '      set next cells offset
  thisCell = Selection.Offset(0, off).Value                                '      add this city to combobox2's list
Loop                                                                                     '    repeat process
Worksheets("Main").Select                                               ' select the sheets with comboboxes again
End Sub 

[Code Ends]

Good Luck,

Any problems please write for my email address, and yours and I'll send you the sheet I created

 

Chas


Viewing all articles
Browse latest Browse all 14

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>