2013-07-17 68 views
1

我有一個向前和向後按鈕的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 
+0

試試這個'WS(WS.Index - 1).Activate()' – Santosh

回答

3

我覺得應該是:

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

而不僅僅是

(WS.Index -1).Activate() 
+1

謝謝,那有效。但是因爲我有Option Strict On,所以不允許後期綁定。我必須設置選項嚴格關閉,然後它工作正常。 –