Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7571823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:45:57+00:00 2026-05-30T15:45:57+00:00

I have a problem with my VBA script in Excel. What I do is

  • 0

I have a problem with my VBA script in Excel. What I do is basically creating buttons that when pressed will create a set of two extra comboboxes in one of the sheet. This button can be pressed continuously to add more comboboxes.

These newly created comboboxes will behave like this:

  1. Created 2 Combobox
  2. Combobox1 will load some list in Control sheet
  3. Whenever an item in Combobox1 is selected, Combobox2 will load list of items to be added to Combobox2

The code for adding the button is like this

 Sub Add_Criteria()

       Dim controlNum As Integer
       Dim name1 As String
       Dim name2 As String
       Dim oOle1 As OLEObject
       Dim oOle2 As OLEObject
       Dim uniqueString As String

       Dim cb1 As Object

        controlNum = Sheets("Controls").Range("A16").Value

        'adding Control
        Set oOle1 = Sheets("System").OLEObjects _
            .Add(ClassType:="Forms.ComboBox.1", Left:=10, _
            Top:=75 + (controlNum * 20), Width:=100, Height:=18)
        Set oOle2 = Sheets("System").OLEObjects _
            .Add(ClassType:="Forms.ComboBox.1", Left:=120, _
            Top:=75 + (controlNum * 20), Width:=100, Height:=18)

        'adding properties
        oOle1.Name = "Criteria" & controlNum * 2 - 1
        oOle2.Name = "Criteria" & controlNum * 2

        'adding control var
        Sheets("Controls").Range("A16").Value = controlNum + 1

        With oOle1.Object
           .List = Sheets("Controls").Range("A5:A13").Value
        End With

  End Sub

The question is, I cannot detect events on it. I need to change the value shown on the second combobox created when value in combobox1 changed. I tried to use below reference and I still can’t. Can anyone guide me on how to do this

Reference (Been on this Problem for days):

http://www.dbforums.com/microsoft-excel/1641165-detecting-click-event-dynamically-created-controls.html (This is for userform, I don’t know why I can’t replicate this in Sheet)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:45:58+00:00Added an answer on May 30, 2026 at 3:45 pm

    You can add Events programmatically. The code below adds an event for each combobox

    This reference from Pearson Programming The VBA Editor may also be useful.

    enter image description here

    Sub Add_Criteria()
    
        Dim controlNum As Integer
        Dim name1 As String
        Dim name2 As String
        Dim oOle1 As OLEObject
        Dim oOle2 As OLEObject
        Dim uniqueString As String
        Dim cb1 As Object
        Dim strCode As String
        Dim vbProj As Object
        Dim vbCodeMod As Object
    
        Set vbProj = ActiveWorkbook.VBProject
        Set vbCodeMod = vbProj.vbcomponents(ActiveSheet.CodeName).codemodule
    
        controlNum = Sheets("Controls").Range("A16").Value
    
        'adding Control
        Set oOle1 = Sheets("System").OLEObjects.Add(ClassType:="Forms.ComboBox.1", Left:=10, Top:=75 + (controlNum * 20), Width:=100, Height:=18)
        Set oOle2 = Sheets("System").OLEObjects.Add(ClassType:="Forms.ComboBox.1", Left:=120, Top:=75 + (controlNum * 20), Width:=100, Height:=18)
    
    
        vbCodeMod.AddFromString AddEvent(oOle1.Name)
        vbCodeMod.AddFromString AddEvent(oOle2.Name)
    
    
        'adding properties
        oOle1.Name = "Criteria" & controlNum * 2 - 1
        oOle2.Name = "Criteria" & controlNum * 2
    
        'adding control var
        Sheets("Controls").Range("A16").Value = controlNum + 1
    
        With oOle1.Object
            .List = Sheets("Controls").Range("A5:A13").Value
        End With
    
    End Sub
    
    Function AddEvent(strIn As String) As String
        AddEvent = "Private Sub " & strIn & "_Click()" & Chr(10) & _
                  "MsgBox ""Event Added""" & Chr(10) & _
                  "End Sub"
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have a large VBA script file that references the cells of an excel
I have problem with fancybox. I want to write a function that will run
I have created a VBA script that detects a number and shows a colour
I would like to have a complex form that uses various VBA script files.
I have a problem with my VBA code in an excel spreadsheet containing orders.
I have a problem with a VBA Add-in in Excel 2010. I've created some
I have a little problem with a simple vbScript. The script has to run
I have problem in some JavaScript that I am writing where the Switch statement
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.