简体   繁体   中英

Excel worksheets in vb.net

Is there a way when to make the current worksheet your on in excel to be the worksheet shown when changing worksheets in vb.net? Say I'm on worksheet 1 and change it to worksheet 2 through vb.net, If I enter in data it will show up on worksheet two but in order to see the data I have to physically go to the excel file and select worksheet 2 in order to change the actually worksheet page being displayed.

Tried this code and it seems to work on my side. The trick is the method sheet.Activate()

Sub Main
    Dim excelApp as Microsoft.Office.Interop.Excel.Application = _
                    new Microsoft.Office.Interop.Excel.Application()
    Try  
        Dim inFile As String = "D:\temp\test.xls"
        ' Show excel ' 
        excelApp.Visible = true
        Dim excelWorkbook As Microsoft.Office.Interop.Excel._Workbook = _
                             excelApp.Workbooks.Open(infile)  

        Dim x as Integer 
        for x = excelApp.Sheets.Count to 1 step -1 
            Dim sheet as _Worksheet = CType(excelApp.Sheets(x), _Worksheet)
            sheet.Activate()   ' Activate the sheet'
            Thread.Sleep(5000) ' Sleep for 5 seconds to see the sheet'
        Next
        Finally  
            if excelApp IsNot Nothing then
                excelApp.DisplayAlerts = false
                'excelApp.Quit();  'remove the comment to close excel'
            End if
        End try
 End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM