Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
How to test if an Excel spreadsheet exists in VBScript?
I'm trying to reference an Excel spreadsheet that may not exist at the time
of the test, like this: xlApp.WorkSheets("Test").Select I would like to test if the "Test" sheet exist before selecting it. How to do it in VBScript? I tried this: IsObject(xlApp.WorkSheets("Test")), but it only worked if "Test" existed, it gave me an error when it didn't. Thanks a bunch, JP |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
How to test if an Excel spreadsheet exists in VBScript?
Dim sh as Excel.Worksheet
On Error Resume Next set sh = xlApp.WorkSheets("Test").Select On Error goto 0 if not sh is nothing then sh.select Else msgbox "Test does not exist" End if -- Regards, Tom Ogilvy "JP" wrote in message ... I'm trying to reference an Excel spreadsheet that may not exist at the time of the test, like this: xlApp.WorkSheets("Test").Select I would like to test if the "Test" sheet exist before selecting it. How to do it in VBScript? I tried this: IsObject(xlApp.WorkSheets("Test")), but it only worked if "Test" existed, it gave me an error when it didn't. Thanks a bunch, JP |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
How to test if an Excel spreadsheet exists in VBScript?
That's what you get for copying from the OP. <g
-- Regards, Tom Ogilvy "Dave Peterson" wrote in message ... Typo alert <vbg: set sh = xlApp.WorkSheets("Test").Select should be: set sh = xlApp.WorkSheets("Test") (drop the .select) Tom Ogilvy wrote: Dim sh as Excel.Worksheet On Error Resume Next set sh = xlApp.WorkSheets("Test").Select On Error goto 0 if not sh is nothing then sh.select Else msgbox "Test does not exist" End if -- Regards, Tom Ogilvy "JP" wrote in message ... I'm trying to reference an Excel spreadsheet that may not exist at the time of the test, like this: xlApp.WorkSheets("Test").Select I would like to test if the "Test" sheet exist before selecting it. How to do it in VBScript? I tried this: IsObject(xlApp.WorkSheets("Test")), but it only worked if "Test" existed, it gave me an error when it didn't. Thanks a bunch, JP -- Dave Peterson |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
How to test if an Excel spreadsheet exists in VBScript?
The code you provided is not VBScript is it? The program I'm running only
understands VBScript. Your code gave me some good ideas, however it didn't run in VBScript. Even the declaration "sh as Excel.Worksheet" gave me an error. Besides the variable declaration this is what I'm trying to do: Thanks Set xlApp = Nothing On Error Resume Next 'get an object reference to an open Excel app Set xlApp = GetObject(,"Excel.Application") On Error Goto 0 If xlApp Is Nothing Then MsgBox "Fatal error" End If 'This is the piece that you wrote, but it doesn't work Set sh = Nothing On Error Resume Next set sh = xlApp.WorkSheets("Test") 'This line gives me an error If sh Is Nothing Then msgbox "Test does not exist" Else sh.select End if 'Get the number of used rows from "Test" sheet a = sh.UsedRange.Rows.Count MsgBox(a) "Tom Ogilvy" wrote: Dim sh as Excel.Worksheet On Error Resume Next set sh = xlApp.WorkSheets("Test").Select On Error goto 0 if not sh is nothing then sh.select Else msgbox "Test does not exist" End if -- Regards, Tom Ogilvy "JP" wrote in message ... I'm trying to reference an Excel spreadsheet that may not exist at the time of the test, like this: xlApp.WorkSheets("Test").Select I would like to test if the "Test" sheet exist before selecting it. How to do it in VBScript? I tried this: IsObject(xlApp.WorkSheets("Test")), but it only worked if "Test" existed, it gave me an error when it didn't. Thanks a bunch, JP |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Test if file exists | Excel Discussion (Misc queries) | |||
Test for Worksheet Exists | Excel Programming | |||
Opening excel spreadsheet from within a VBscript | Excel Programming | |||
Test if a folder exists | Excel Programming | |||
VBScript Automation of Excel Spreadsheet Sorting & Filtering | Excel Programming |