Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
JP JP is offline
external usenet poster
 
Posts: 103
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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   Report Post  
Posted to microsoft.public.excel.programming
JP JP is offline
external usenet poster
 
Posts: 103
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Test if file exists Daniel Bonallack Excel Discussion (Misc queries) 2 May 4th 09 04:19 PM
Test for Worksheet Exists bcmiller[_8_] Excel Programming 4 July 2nd 04 11:46 AM
Opening excel spreadsheet from within a VBscript Disco[_2_] Excel Programming 7 January 23rd 04 03:50 PM
Test if a folder exists Jeff Marshall Excel Programming 6 September 30th 03 05:21 PM
VBScript Automation of Excel Spreadsheet Sorting & Filtering Tom Ogilvy Excel Programming 1 September 10th 03 03:01 PM


All times are GMT +1. The time now is 08:44 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"