PC Review


Reply
Thread Tools Rate Thread

Check for worksheet in closed workbook

 
 
Les
Guest
Posts: n/a
 
      3rd Dec 2007
Hello all, is it possible to get a worksheet name in a closed workbook ?

If it is possible could you help me with some script please ?
--
Les
 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      3rd Dec 2007
Here's a simple function to test fr it

Function IfSheetExists(FileName As String, sh As String) As Boolean
Dim oConn As Object

Set oConn = CreateObject("ADODB.Connection")
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & FileName & ";" & _
"Extended Properties=Excel 8.0;"

On Error Resume Next
oConn.Execute "SELECT 1 FROM [" & sh & "$] WHERE 0=1"
IfSheetExists = (Err.Number = 0)

oConn.Close
Set oConn = Nothing

End Function

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Les" <(E-Mail Removed)> wrote in message
news:52A6C1B7-1885-4DD8-AD7A-(E-Mail Removed)...
> Hello all, is it possible to get a worksheet name in a closed workbook ?
>
> If it is possible could you help me with some script please ?
> --
> Les



 
Reply With Quote
 
Les Stout
Guest
Posts: n/a
 
      3rd Dec 2007
Hi Bob, thanks for the reply, i have done script like this, but get an
error msg. ?

Could you help ?

Sub DoesSheetExist()
'

If IfSheetExists("TOOL TRACKING") = True Then

Workbooks.Open(FileName:="\\nv09002\tpdrive\Projects\General\50_Comparis
ons\KTL's\" & myKTLIH & ".xls"). _
RunAutoMacros Which:=xlAutoOpen
Else
MsgBox "You have loaded the incorrect KTL", vbOKOnly, "ERROR"
End If

End Sub
'-----------------------------------------------------------------
Function IfSheetExists(FileName As String, sh As String) As Boolean
Dim oConn As Object, myKTL As String

myKTL = "90ZA0810"

Set sh = "TOOL TRACKING"
Set FileName =
"\\nv09002\tpdrive\Projects\General\50_Comparisons\KTL's\" & myKTLIH &
".xls"

Set oConn = CreateObject("ADODB.Connection")
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & FileName & ";" & _
"Extended Properties=Excel 8.0;"

On Error Resume Next
oConn.Execute "SELECT 1 FROM [" & sh & "$] WHERE 0=1"
IfSheetExists = (Err.Number = 0)

oConn.Close
Set oConn = Nothing

End Function


Best regards,

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      3rd Dec 2007
First, don't change Bob's function. It should just be pasted into the module.

Second, Bob's function want's a filename passed to the function, then the
worksheet name, so maybe something like:

If IfSheetExists("\\nv09002\tpdrive\Projects\General\50_Comparisons\KTL's\" _
& myKTLIH & ".xls", "TOOL TRACKING") = True Then

Les Stout wrote:
>
> Hi Bob, thanks for the reply, i have done script like this, but get an
> error msg. ?
>
> Could you help ?
>
> Sub DoesSheetExist()
> '
>
> If IfSheetExists("TOOL TRACKING") = True Then
>
> Workbooks.Open(FileName:="\\nv09002\tpdrive\Projects\General\50_Comparis
> ons\KTL's\" & myKTLIH & ".xls"). _
> RunAutoMacros Which:=xlAutoOpen
> Else
> MsgBox "You have loaded the incorrect KTL", vbOKOnly, "ERROR"
> End If
>
> End Sub
> '-----------------------------------------------------------------
> Function IfSheetExists(FileName As String, sh As String) As Boolean
> Dim oConn As Object, myKTL As String
>
> myKTL = "90ZA0810"
>
> Set sh = "TOOL TRACKING"
> Set FileName =
> "\\nv09002\tpdrive\Projects\General\50_Comparisons\KTL's\" & myKTLIH &
> ".xls"
>
> Set oConn = CreateObject("ADODB.Connection")
> oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> "Data Source=" & FileName & ";" & _
> "Extended Properties=Excel 8.0;"
>
> On Error Resume Next
> oConn.Execute "SELECT 1 FROM [" & sh & "$] WHERE 0=1"
> IfSheetExists = (Err.Number = 0)
>
> oConn.Close
> Set oConn = Nothing
>
> End Function
>
> Best regards,
>
> Les Stout
>
> *** Sent via Developersdex http://www.developersdex.com ***


--

Dave Peterson
 
Reply With Quote
 
Les Stout
Guest
Posts: n/a
 
      3rd Dec 2007
Thanks so much Dave, as i am not a programmer i am a little unsure of
these things but have managed to get it working in the mean time, with
the following code.

Sub DoesSheetExist()
'
Dim myKTLih As String

myKTLih = "90IH0810"


If
IfSheetExists("\\nv09002\tpdrive\Projects\General\50_Comparisons\KTL's\"
& myKTLih & ".xls", "TOOL TRACKING") = True Then

Workbooks.Open(FileName:="\\nv09002\tpdrive\Projects\General\50_Comparis
ons\KTL's\" & myKTLih & ".xls"). _
RunAutoMacros Which:=xlAutoOpen

Else
MsgBox "You have loaded the incorrect KTL", vbOKOnly, "ERROR"
End If

End Sub
'-----------------------------------------------------------------
Function IfSheetExists(FileName As String, sh As String) As Boolean
Dim oConn As Object



'FileName = "\\nv09002\tpdrive\Projects\General\50_Comparisons\KTL's\" &
myKTLih & ".xls"
'sh = Worksheets("TOOL TRACKING")

Set oConn = CreateObject("ADODB.Connection")
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & FileName & ";" & _
"Extended Properties=Excel 8.0;"

On Error Resume Next
oConn.Execute "SELECT 1 FROM [" & sh & "$] WHERE 0=1"
IfSheetExists = (Err.Number = 0)

oConn.Close
Set oConn = Nothing

End Function



Best regards,

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help - How to check if value exists in closed workbook? c mateland Microsoft Excel Programming 3 16th Apr 2007 03:05 AM
Check if sheet exists in a closed workbook FrigidDigit Microsoft Excel Programming 2 25th Oct 2005 06:44 AM
copy worksheet from closed workbook to active workbook using vba =?Utf-8?B?bWFuZ28=?= Microsoft Excel Worksheet Functions 6 9th Dec 2004 07:55 AM
VBA to get a range or worksheet from a closed workbook dovrox Microsoft Excel Misc 2 6th May 2004 04:10 PM
save worksheet to another closed workbook Axcell Microsoft Excel Misc 0 27th Feb 2004 02:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:48 AM.