Advertisement

09.23.2007 at 08:31PM PDT, ID: 22847563
[x]
Attachment Details

Add to combo box in Excel VBA

Asked by jhev1 in Microsoft Excel Spreadsheet Software, VB Controls

Tags: excel, vba, combo, box

Ok guys, I've come to you with another annoying (for me anyway!) Excel VBA combo box problem.  First of all, this is just something I am doing for fun, I have no professional programming experience so excuse my ignorance.  I have a user form with a combo box on it which I use the RowSource property to populate.  It works as long as what I want is in the list.  If I want to add something to it though, how do I do that?  For example, I have apples and oranges in the list and want to add pears.  Right now, I have a command button that I want to use to add the item to the list if needed.  I know I can programmatically insert a cell or go to end of my list and write the value there, but I don't want to have duplicate items in the list.  How can I search through a range of cells that is always changing (like A2:A5, next time might be A2:A6 as things get added)?  I think I need to use some kind of loop, but I am not sure.  Also, if I reference A:A for my RowSource property there are multiple blank rows in the combo box that I don't want.  Would I then have to count the non-blank cells in A and change the RowSource property each time?  Does this make sense?Start Free Trial
[+][-]09.23.2007 at 08:55PM PDT, ID: 19946463

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.23.2007 at 09:08PM PDT, ID: 19946494

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.24.2007 at 07:29PM PDT, ID: 19953042

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.24.2007 at 08:15PM PDT, ID: 19953193

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.25.2007 at 04:36PM PDT, ID: 19959792

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Excel Spreadsheet Software, VB Controls
Tags: excel, vba, combo, box
Sign Up Now!
Solution Provided By: ahammar
Participating Experts: 1
Solution Grade: A
 
 
[+][-]09.25.2007 at 06:44PM PDT, ID: 19960228

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.25.2007 at 07:26PM PDT, ID: 19960333

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
09.23.2007 at 08:55PM PDT, ID: 19946463
You could do something like putting this in the forms open event:

ComboBox1.RowSource = "A2:" & Range("A1").End(xlDown).Address

(of course replace ComboBox1 with the name of your combo box)

That will reset the RowSource to A2 to the last contigent used cell in that column everytime the form opens
You could also use any other event if that works better for you...


:-)
ah
 
09.23.2007 at 09:08PM PDT, ID: 19946494
I did that wrong.  The correct code would be this:
ComboBox1.RowSource = "A2:" & Range("A2").End(xlDown).Address

I may not have answered your question completely anyway though.  Here is a little bit more info...

Say you have a command button that you want to use to add an item.  In the click event of the button you could have this:


Dim MyValue as string
    MyValue = InputBox("What new value do you want to add?")
    if MyValue = "" then exit sub
    Range("A2").End(xlDown).Offset(1,0).value = MyValue
    ComboBox1.RowSource = "A2:" & Range("A2").End(xlDown).Address



That will add a new value to the list that starts in A2, then reset the rowSource of the combo box to match.

Hope that helps...

:-)
ah

 
09.24.2007 at 07:29PM PDT, ID: 19953042
Yes, ahammar, so far so good!  I do indeed have a command button that I use to update the sheet; however, how do I prevent multiple items from appearing in the list?  I am sick of MS Money, my wife only uses the check register anyway, so I am trying to recreate that.  The combo box has a partial list of payees in it, as we spend money in new places they need to be added.  When I click my command button I want the code to check to see if the payee is already in the list, and if not add it.  This code works great for adding, but how do I check to see if it exist already? If whoever answers this part of the question can point me in the right direction with the type of loop to use(assuming that is the right answer!!), I'd like to try to figure some of it out on my own.  It's not that I don't appreciate the help, believe me I do, I just want a push in the right direction so I can learn more.
 
09.24.2007 at 08:15PM PDT, ID: 19953193
Ok...a change....This will do everything you want I think...There are a lot of comments (maybe you don't need them, but just in case they help you).
Anyway...the comments make it look a lot longer than it is.  Get rid of them and it will be much shorter.  Remember to change 'A2' to the first cell on the list, or the first cell where you want to start your list...(ps...my last code would not have worked if there was not at least 2 items already in the list.  I just noticed that)


Dim NewValue As String
Dim r As Range 'First cell of the list
Dim FirstAddress As String 'Variable to hold address of first cell
Dim AlreadyExists As Boolean 'True/False variable..will be set to true if value exists


    NewValue = InputBox("What new value do you want to add?") 'Set NewValue = to the value to add
    If NewValue = "" Then Exit Sub
   
    Set r = Range("A2") 'Change this to first cell of list...r is now this cell
    FirstAddress = r.Address 'this will make FirstAddress = to r's address
   
    Do Until r = "" 'loop until the first blank cell is found
        If r.Value = NewValue Then AlreadyExists = True
        Set r = r.Offset(1, 0) 'Move r down 1 row
    Loop
   
'r will now be the first empty cell in the list and 'AlreadyExists will be
'true if any of the cells values were = to NewValue, otherwise it will be false

    If AlreadyExists = True Then 'Value was found
        MsgBox ("That value already exists.  Cannot add it to the list")
        'set the RowSource to range from FirstAddress to the address directly above where r
        'is...(remember r is at the first blank cell)
        ComboBox1.RowSource = FirstAddress & ":" & r.Offset(-1, 0).Address
    Else 'Value was not found
        r.Value = NewValue
        MsgBox ("Value has been successfully added to the list")
        'set the RowSource to range from FirstAddress to r's address
        ComboBox1.RowSource = FirstAddress & ":" & r.Address
    End If
   



Let me know if you have any problems or questions...

:-)
ah
 
09.25.2007 at 04:36PM PDT, ID: 19959792
Be sure to read my last suggested code snippit, but I just wanted to say...
I am glad you want to learn how to do this.  It is an awesome thing to be able to do.  Once you master it, you can make your life a whole lot easier.  I have many automated applications that I wrote specifically the way I want them, and the one I use most is my Check and Debit card register application, just like what it appears you are trying to create.  Excellent choice of idea!
The reason I mention this, is just in case you want a copy of it, let me know.  I like it a lot.  It is automated to jump from cell to cell where you need it...has a list of payees that is listed in an invisible combo box that will appear on double click in certain column, has calendar control to enter dates by the click of a button...checks back a predifined number of entries every time you make a new entry to see if it is a duplicate, automatically puts the decimal or negative signs in for you if you choose either of those options, has a dupe previous date button to make an entry that is a duplicate of the previous (date only), it fills my checks out for me...converts a number to the spelled out version for the check...(only up to a thousand dollars though...like I'm gonna be writing checks out that big on a regular basis...lol )...automated font colors for credits and debits...plus more
I don't want to take the fun away from you, just give you some ideas.  You have started what will be an excellent choice of a very useful application and I am glad you are designing it the way you want it to work and do the things you want it to do.  That's the nice thing about writing your own stuff.  I never even tried Microsoft money or quick books or any of that.  I am already pretty sure I wouldn't like the way it worked.
Anyway...if you want a copy, let me know...I'll upload it so you can download it...

If you have any questions, be sure and ask...

Good luck and happy programming!

:-)
ahammar
Accepted Solution
 
09.25.2007 at 06:44PM PDT, ID: 19960228
Thanks for the help ahammar.  It's kind of funny, your program sounds very similar to what I am trying to do here.  While it is tempting to use your program, like I said I enjoy programming and the challenges it presents.  Thanks for taking the time to comment your code, it makes perfect sense.  I'm sure I'll be asking for help in the future, as well as using some of the ideas from your program.  I had already put a calendar control on there, I like the idea of the font colors too.  I was thinking in the future of adding another combo box for a category for each item, so you can get an idea of how much is being spent where.  Anyway, thanks again for the help.  Talk to you soon (not too soon I hope, lol).
 
09.25.2007 at 07:26PM PDT, ID: 19960333
Thanks for the points and the grade!  I'm glad you have your own ideas.  Sounds like you already got it figured out and I'm glad you came up with what you want to do.  I like the catagory idea...I hadn't thought of that one...I may have to incorporate that into mine...

Good Luck!

ah
 
 
20080716-EE-VQP-32 / EE_QW_2_20070628