ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Weird result (https://www.excelbanter.com/excel-programming/336733-weird-result.html)

Roger B.

Weird result
 
I would like to know what is wrong with the following macro.
If Fast05Chts.XLS is closed it works but not if that file is open. Instead I
get a message that the file is open. This is strange when the 'exit sub'
should have prevented this.

Any suggestions would be appreciated.

Roger

Dim File_Name As Variant, book As Object

'Dim book As Variant
File_Name = "Fast05Chts.XLS"
For Each book In Workbooks
If book.Name = File_Name Then
Windows(File_Name).Activate
Exit Sub
End If
Next book

Workbooks.Open Filename:="D:\Fast05\" & File_Name, UpdateLinks:=True



Jim Thomlinson[_4_]

Weird result
 
The comparison is case sensitive. Try this code...

Dim File_Name As String
Dim book As Workbook

File_Name = "fast05chts.xls"
For Each book In Workbooks
If LCase(book.Name) = File_Name Then
Windows(File_Name).Activate
Exit Sub
End If
Next book

Workbooks.Open Filename:="D:\Fast05\" & File_Name, UpdateLinks:=True

--
HTH...

Jim Thomlinson


"Roger B." wrote:

I would like to know what is wrong with the following macro.
If Fast05Chts.XLS is closed it works but not if that file is open. Instead I
get a message that the file is open. This is strange when the 'exit sub'
should have prevented this.

Any suggestions would be appreciated.

Roger

Dim File_Name As Variant, book As Object

'Dim book As Variant
File_Name = "Fast05Chts.XLS"
For Each book In Workbooks
If book.Name = File_Name Then
Windows(File_Name).Activate
Exit Sub
End If
Next book

Workbooks.Open Filename:="D:\Fast05\" & File_Name, UpdateLinks:=True




Roger B.

Weird result
 
Jim, many thanks. Your macro worked really well.
Roger

"Jim Thomlinson" wrote in message
...
The comparison is case sensitive. Try this code...

Dim File_Name As String
Dim book As Workbook

File_Name = "fast05chts.xls"
For Each book In Workbooks
If LCase(book.Name) = File_Name Then
Windows(File_Name).Activate
Exit Sub
End If
Next book

Workbooks.Open Filename:="D:\Fast05\" & File_Name, UpdateLinks:=True

--
HTH...

Jim Thomlinson


"Roger B." wrote:

I would like to know what is wrong with the following macro.
If Fast05Chts.XLS is closed it works but not if that file is open.
Instead I
get a message that the file is open. This is strange when the 'exit sub'
should have prevented this.

Any suggestions would be appreciated.

Roger

Dim File_Name As Variant, book As Object

'Dim book As Variant
File_Name = "Fast05Chts.XLS"
For Each book In Workbooks
If book.Name = File_Name Then
Windows(File_Name).Activate
Exit Sub
End If
Next book

Workbooks.Open Filename:="D:\Fast05\" & File_Name, UpdateLinks:=True







All times are GMT +1. The time now is 01:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com