Link to home
Start Free TrialLog in
Avatar of UniqueData
UniqueDataFlag for United States of America

asked on

open specific Excel Worksheet name

I need to open a particular sheet in an excel workbook.  I tried:
    Set wbk = appExcel.Workbooks.Open(strImportFile)
    Set wks = wbk.Worksheets(rstImportSpec!SheetName)
where SheetName = 'Main Page' but I get a 'Type Mismatch' error
However  Set wks = wbk.Worksheets(1) works perfectly.  The problem is sometimes the sheets are not all the same in all workbooks but the particular Sheet Name always exists

what am I doing wrong?

Michael
Avatar of TextReport
TextReport
Flag of United Kingdom of Great Britain and Northern Ireland image

try

Set wks = wbk.Sheets(rstImportSpec!SheetName)

Cheers, Andrew
Avatar of Malik1947
Malik1947

use this to access a specific sheet

Sheet name is a string.


Set wks = wbk.Sheets(SheetName)

Open in new window

Avatar of Rey Obrero (Capricorn1)
dim xlObj as object, xlSht as object
set xlObj=createobject("excel.application")
      xlObj.workbooks.open(strImportFile)

set xlSht=xlobj.activeworkbook.worksheets(rstImportSpec("Sheetname"))
It should work, make sure that the names are the same:

    Set wbk = appExcel.Workbooks.Open(strImportFile)
Debug.Print wkb.Worksheets(1).name,  rstImportSpec!SheetName
stop
    Set wks = wbk.Worksheets(rstImportSpec!SheetName)
slight correction

dim xlObj as object, xlSht as object
set xlObj=createobject("excel.application")
      xlObj.workbooks.open(strImportFile)

set xlSht=xlobj.activeworkbook.worksheets(cstr(rstImportSpec("Sheetname")))
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of UniqueData

ASKER

Sheets did not work.  It looks like the trick was cstr as cap suggested.