none
VSTO C# Excel worksheet activation event RRS feed

  • Question

  • I am a beginner of Visual Studio and want to use C # to create an Excel 2013 and 2016 VSTO template
    There are three worksheets in the workbook template, assuming Sheet1, Sheet2, and Sheet3 enter the current time in Cells (1, 1) of Sheet1 when Sheet1 is activated.

    Worksheet activation event function similar to Excel VBA.

    Private Sub Worksheet_Activate()
        Cells(1, 1) = Now
    End Sub

    When Sheet1 is activated, enter the current time in Cells (1,1) of Sheet1, switch to other worksheets, then switch back to Sheet1, and then enter a new time in Sheet1.

    using System;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    
    namespace ExcelTemplate
    {
    
        public partial class Sheet1
        {
    
            private void Sheet1_Startup(object sender, System.EventArgs e)
    
            {   
                this.Cells[1, 1].Value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            }
    
         }
    
    }
    

    For example, the Sheet1. CS code of the worksheet above, when switching to other worksheets and then switching back to Sheet1, the above program did not respond. How to modify the request?

    I would like to ask similar VBA "Worksheet_Activate()" worksheet activation events, such code in VSTO how to do? Can you give a complete code sample file? Thank you!

    Wednesday, March 27, 2019 8:41 AM

All replies

  • Hello,

    the Startup Event fires only once. You must use the WorkSheet Activate Event. To get this Event. Show the Code Window for your Sheet and then select Events. Now you can see on the right all possible events.

    Hope this helps

    Thomas

    Monday, April 1, 2019 2:24 PM