按索引激活工作表

时间:2013-07-17 02:27:51

标签: vb.net excel

我有一个带前进和后退按钮的winform。我想要做的是允许用户通过单击按钮在工作簿上来回移动。我认为实现这一目标的最佳方法是使用索引。但是,它给了我适合。 IDE告诉我线路上有语法错误:

(WS.Index - 1).Activate() 

If Err.Number <> 0 Then WS(1).Activate()

这是我的整个代码:

    Option Explicit On
Option Strict On

'Import Libraries
Imports Microsoft.Office.Tools.Excel
Imports System.Drawing
Imports System
Imports System.IO
Imports System.Drawing.Printing
Imports System.Windows.Forms

Public Class frmNavigation

    Dim WB As Excel.Workbook
    Dim WS As Excel.Worksheet


    Private Sub btnMoveBack_Click(sender As Object, e As EventArgs) Handles btnMoveBack.Click

        'This event is triggered when the Previous Sheet button is
        'clicked. The sheet moves to the previous sheet. This event
        'is only run throughout the clientworksheets as the tabs and other
        'standard excel navigation may be disabled.

        WB = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)
        WS = CType(WB.ActiveSheet, Excel.Worksheet)

        WS.Application.ScreenUpdating = False

        On Error Resume Next

       (WS.Index - 1).Activate()                              

        If Err.Number <> 0 Then WS(1).Activate() 'If error stay in the active sheet

        WS.Application.ScreenUpdating = True


    End Sub

1 个答案:

答案 0 :(得分:3)

我认为应该是:

WB.WorkSheets(WS.Index -1).Activate()

而不仅仅是

(WS.Index -1).Activate()