PC Review


Reply
Thread Tools Rate Thread

Check Workbook name currently opens is correctly

 
 
Len
Guest
Posts: n/a
 
      29th Mar 2010
Hi,

Codes below try to check every existing workbook's name currently
opened is correct and if not, it exit sub
After several attempts, it still not able to run and prompts "
Subscript out of range"

Sub test()
Dim y As Long, x As Variant
x = Array("AB.xls", "CD.xls", "EF.xls", "GH.xls")
For y = LBound(x) To UBound(x)
If Workbooks(x(y)).Name = "AB.xls" Then
MsgBox Workbooks(x(y)).Name & " not found "
Exit Sub
ElseIf Workbooks(x(y)).Name = "CD.xls" Then
MsgBox Workbooks(x(y)).Name & " not found "
Exit Sub
ElseIf Workbooks(x(y)).Name = "EF.xls" Then
MsgBox Workbooks(x(y)).Name & " not found "
Exit Sub
ElseIf Workbooks(x(y)).Name = "GH.xls" Then
MsgBox Workbooks(x(y)).Name & " not found "
Exit Sub
End If
Next y
End Sub

Any helps will be appreciated and thanks in advance

Regards
Len
 
Reply With Quote
 
 
 
 
ozgrid.com
Guest
Posts: n/a
 
      29th Mar 2010
An array starts at zero is no valid for a Workbook Index (start at one).

Sub CheckOpenWB()
Dim wbTest As Workbook

On Error Resume Next
Set wbTest = Workbooks("AB.xls")
If wbTest Is Nothing Then MsgBox "AB.xls is not open"
Set wbTest = Nothing
Set wbTest = Workbooks("CD.xls")
If wbTest Is Nothing Then MsgBox "CD.xls is not open"
Set wbTest = Nothing
Set wbTest = Workbooks("EF.xls")
If wbTest Is Nothing Then MsgBox "EF.xls is not open"

End Sub

"Len" <(E-Mail Removed)> wrote in message
news:994156a6-6592-45cb-8aff-(E-Mail Removed)...
> Hi,
>
> Codes below try to check every existing workbook's name currently
> opened is correct and if not, it exit sub
> After several attempts, it still not able to run and prompts "
> Subscript out of range"
>
> Sub test()
> Dim y As Long, x As Variant
> x = Array("AB.xls", "CD.xls", "EF.xls", "GH.xls")
> For y = LBound(x) To UBound(x)
> If Workbooks(x(y)).Name = "AB.xls" Then
> MsgBox Workbooks(x(y)).Name & " not found "
> Exit Sub
> ElseIf Workbooks(x(y)).Name = "CD.xls" Then
> MsgBox Workbooks(x(y)).Name & " not found "
> Exit Sub
> ElseIf Workbooks(x(y)).Name = "EF.xls" Then
> MsgBox Workbooks(x(y)).Name & " not found "
> Exit Sub
> ElseIf Workbooks(x(y)).Name = "GH.xls" Then
> MsgBox Workbooks(x(y)).Name & " not found "
> Exit Sub
> End If
> Next y
> End Sub
>
> Any helps will be appreciated and thanks in advance
>
> Regards
> Len


 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      29th Mar 2010
Hi Len,

The following code should achieve what you want.

Sub test()
Dim wb As Workbook
Dim y As Long, x As Variant
x = Array("AB.xls", "CD.xls", "EF.xls", "GH.xls")

For y = LBound(x) To UBound(x)
Set wb = Nothing 'Initialize to nothing
On Error Resume Next
Set wb = Workbooks(x(y))
On Error GoTo 0 'Resume error trapping ASAP
If wb Is Nothing Then
MsgBox "Workbook " & x(y) & " not found "
Exit Sub
End If
Next y

End Sub

--
Regards,

OssieMac


"Len" wrote:

> Hi,
>
> Codes below try to check every existing workbook's name currently
> opened is correct and if not, it exit sub
> After several attempts, it still not able to run and prompts "
> Subscript out of range"
>
> Sub test()
> Dim y As Long, x As Variant
> x = Array("AB.xls", "CD.xls", "EF.xls", "GH.xls")
> For y = LBound(x) To UBound(x)
> If Workbooks(x(y)).Name = "AB.xls" Then
> MsgBox Workbooks(x(y)).Name & " not found "
> Exit Sub
> ElseIf Workbooks(x(y)).Name = "CD.xls" Then
> MsgBox Workbooks(x(y)).Name & " not found "
> Exit Sub
> ElseIf Workbooks(x(y)).Name = "EF.xls" Then
> MsgBox Workbooks(x(y)).Name & " not found "
> Exit Sub
> ElseIf Workbooks(x(y)).Name = "GH.xls" Then
> MsgBox Workbooks(x(y)).Name & " not found "
> Exit Sub
> End If
> Next y
> End Sub
>
> Any helps will be appreciated and thanks in advance
>
> Regards
> Len
> .
>

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      29th Mar 2010
Try something like

Dim Sh As Variant
Dim N As Long
Dim WB As Workbook
Sh = Array("Book2", "Book3", "Book99")
For N = LBound(Sh) To UBound(Sh)
On Error Resume Next
Set WB = Nothing
Set WB = Application.Workbooks(Sh(N))
If Err.Number = 0 Then
Debug.Print "book '" & WB.Name & "' is open."
Else
Debug.Print "book '" & Sh(N) & "' is not open."
End If
Next N



Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Sun, 28 Mar 2010 20:03:37 -0700 (PDT), Len
<(E-Mail Removed)> wrote:

>Hi,
>
>Codes below try to check every existing workbook's name currently
>opened is correct and if not, it exit sub
>After several attempts, it still not able to run and prompts "
>Subscript out of range"
>
>Sub test()
>Dim y As Long, x As Variant
>x = Array("AB.xls", "CD.xls", "EF.xls", "GH.xls")
>For y = LBound(x) To UBound(x)
>If Workbooks(x(y)).Name = "AB.xls" Then
> MsgBox Workbooks(x(y)).Name & " not found "
>Exit Sub
>ElseIf Workbooks(x(y)).Name = "CD.xls" Then
>MsgBox Workbooks(x(y)).Name & " not found "
>Exit Sub
>ElseIf Workbooks(x(y)).Name = "EF.xls" Then
>MsgBox Workbooks(x(y)).Name & " not found "
>Exit Sub
>ElseIf Workbooks(x(y)).Name = "GH.xls" Then
>MsgBox Workbooks(x(y)).Name & " not found "
>Exit Sub
>End If
>Next y
>End Sub
>
>Any helps will be appreciated and thanks in advance
>
>Regards
>Len

 
Reply With Quote
 
Len
Guest
Posts: n/a
 
      30th Mar 2010
Hi All,

Thanks for your reply and your codes

I try out all suggested codes and they work perfectly


Regards
Len

 
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
How To Check If The User Has Macros Enabled When The Workbook Opens nouveauricheinvestments@gmail.com Microsoft Excel Programming 6 12th Dec 2008 08:27 PM
when opening an Excel Workbook, another blank workbook also opens spmu Microsoft Excel Misc 4 12th Oct 2007 09:49 PM
How to add a password check when an excel workbook opens? =?Utf-8?B?SXZhbg==?= Microsoft Excel Programming 1 27th Apr 2006 04:46 AM
Personal workbook opens when Excel opens =?Utf-8?B?U2hlcmlUaW5nbGU=?= Microsoft Excel Misc 2 30th Mar 2005 12:22 AM
Excel 97 Workbook opens behind initial blank Workbook B. Green Microsoft Excel Misc 0 9th Jul 2003 03:27 PM


Features
 

Advertising
 

Newsgroups
 


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