0

vb.netでワークシートを変更するときに表示されるワークシートになるように、現在のワークシートをExcelで作成する方法はありますか? ワークシート 1 を使用していて、vb.net を介してワークシート 2 に変更するとします。データを入力すると、ワークシート 2 に表示されますが、データを表示するには、物理​​的に Excel ファイルに移動してワークシート 2 を選択する必要があります。実際に表示されているワークシートのページを変更するため。

4

1 に答える 1

0

このコードを試してみましたが、私の側ではうまくいくようです。秘訣は方法です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
于 2012-08-18T21:38:27.270 に答える