VB6 Excel Worksheet help needed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Craigbob
    New Member
    • Sep 2006
    • 3

    #1

    VB6 Excel Worksheet help needed

    Hi all,

    I'm using the code below to open a work book and move to a particular worksheet in the work book.

    The problem arises in the Else Clause of the IF Then statement. I'm not sure how to set Excel to a particular sheet in the workbook, even though I have both the name of the sheet and the number of the sheet.

    The work books can have either 1 or 2 worksheets in them. If it is 1 sheet, there is no problem. If there the workbook has 2 sheets I want to set the 2nd sheet as the active sheet.

    Any help is appreciated.

    Thanks

    Craig

    Public Function OpenWorkbook(st rFilename) As String
    ' Comments : Open an Excel workbook and returns the FSR Type.
    ' Parameters: DocumentName - Path and file name to open
    ' Returns : FSR Type (Old or New)
    '
    Dim strFSRType As String
    Dim xlActiveSheet As Excel.Worksheet

    On Error GoTo PROC_ERR

    Set m_Excel = CreateObject("E XCEL.APPLICATIO N")
    Set m_Workbook = m_Excel.Workboo ks.Open(strFile name)
    Set xlActiveSheet = m_Workbook.Acti veSheet


    If xlActiveSheet.N ame = "main" Then
    'ActiveWorkbook .Sheets.Count
    OpenWorkbook = "Old"
    Else
    OpenWorkbook = "New"
    xlActiveSheet = m_Workbook.Shee ts("FSR - 45043 Report Sheet")
    End If

    PROC_EXIT:
    Exit Function

    PROC_ERR:
    MsgBox "Error: " & Err.Number & ". " & Err.Description
    Resume PROC_EXIT

    End Function
Working...