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 9295485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:36:56+00:00 2026-06-18T21:36:56+00:00

I have an Excel spreadsheet that sits in an automated testing application that is

  • 0

I have an Excel spreadsheet that sits in an automated testing application that is written in C#. The testing app populates the sheet with final stats and the spreadsheet is then used to generate metrics. Part of that procedure is to place dropdowns into Column L of the spreadsheet, and write their associated VBA change events into the sheet from the app.

Here is the code that writes the dropdown to the spreadsheet
Excel.DropDown xlDropDown;

//set range for insert cell
 range = wrkSheet.get_Range("L" + (x + 9).ToString() + ":L" + (x + 9).ToString());

//insert the dropdown into the cell
xlDropDown = xlDropDowns.Add((double)range.Left, (double)range.Top, (double)range.Width, (double)range.Height, true);

//set the nbame of the new dropdown
xlDropDown.Name = "expFail" + (x + 1).ToString();

//assign dropdown name to cmbName 
cmbName = xlDropDown.Name;

 //call function to write change macro for this box 
 cmbWriteMacro(cmbName, xlApp, xlBook, wrkSheet); 

The cmbWrite Function:

    StringBuilder sb;
    VBA.VBComponent xlModule;
    VBA.VBProject prj;
    string modName;
    int modExists;

    prj = wrkBook.VBProject;
    modExists = 0;

    sb = new StringBuilder();

    //build string with module code 
    sb.Append("Sub " + cmbName + "_Change()" + "\n");
    sb.Append("\t" + "Call lstBox_Update(" + cmbName + ")" + "\n");
    sb.Append("End Sub");

    foreach (VBA.VBComponent comp in prj.VBComponents)
    {
        modName = comp.Name;

        if (modName == "Module2")
        {
            modExists = 1;
            break;
        }
    }

     //check to see if module already exists
     if (modExists != 1)
     {
        //set an object for the new module to create
        xlModule = wrkBook.VBProject.VBComponents.Add(VBA.vbext_ComponentType.vbext_ct_StdModule);
     }
     else
     {
        xlModule = wrkBook.VBProject.VBComponents.Item("Module2");
     }

     //add the cmbbox macro to the spreadsheet
     xlModule.CodeModule.AddFromString(sb.ToString());

This writes the following VBA events into the spreadsheet to perform action in the sheet as failures are noted.

Sub expFail1_Change(ByVal Target As Range)
    Call lstBox_Update("expFail1")
End Sub

All the dropdowns call the same function (lstBox_Update) based on what the selection is.

Everything is working OK. The dropdowns show up where they should and the macros are being written into the spreadsheet correctly. The problem seems to be getting the associated change events to fire when the selection is changed. The solution is probably easy, but I have looked all over and can’t seem to find an answer

  • 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-06-18T21:36:57+00:00Added an answer on June 18, 2026 at 9:36 pm

    I just did a small example based on your code. After some googling around and testing some source code pieces it comes down to a single line of code:

    xlDropDown.Name = "expFail" + (x + 1).ToString(); // your code
    xlDropDown.OnAction = xlDropDown.Name + "_Change"; // new code: IMPORTANT
    

    Found this code within an Microsoft KB article How To Create an Excel Macro by Using Automation from Visual C# .NET.

    It seems that you need this line of code to set some kind of delegate pointing to your custom vba code.

    In case you need my complete source let me know and I will mail it to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a VBA macro in an excel spreadsheet that opens all workbooks
I have an Excel spreadsheet that I have exported from some other program. It
I have an Excel spreadsheet that uses a TFS query to pull information on
I have an Excel spreadsheet that looks like: spreadsheet http://img186.imageshack.us/img186/6495/exelf.jpg I'd like to convert
I have an Excel spreadsheet that will sit out on a network share drive.
I have an excel spreadsheet that is password-protected. I need to open this spreadsheet
I have an excel spreadsheet that has two columns. When I choose to save
Ok, so I have an excel spreadsheet that contains data that I would like
i have an excel spreadsheet that is about 300,000 rows and about 100 columns
All, We have an Excel spreadsheet that uses ADOMD to query OLAP cubes and

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.