How to code macro with condition?

E

Eric

If specific file is currently opened, then not processing following coding,
else process them.
Does anyone have any suggestions on how to code it in macro?
Thanks in advance for any suggestions
Eric

Sub RunOrNot

'Is specific file opened? if not, then process following coding
Coding
'End If

End Sub
 
J

JLGWhiz

fName = "SomeFile.xls" '<<< The file you want to check
For Each wb In Application.Workbooks
If wb.Name = fName Then
Exit Sub
Else
"Run the code
End If
Next
 
J

Jacob Skaria

Use a function as below...

Sub Macro()

If IsWorkbookOpen("Book1") = False Then
'Place your code below

End If

End Sub

Function IsWorkbookOpen(strWorkbook) As Boolean
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks(strWorkbook)
If Not wb Is Nothing Then IsWorkbookOpen = True
End Function
 
E

Eric

It does not work, the opened file is still being opened as read only.
Does anyone have any suggestions?
Thanks in advance for any suggestions
Eric

Dim WkbkName As String
WkbkName = "D:\file1.xls"

If IsWorkbookOpen(WkbkName) = False Then
'Place your code below
End IF
 
E

Eric

Is there any code to check whether the file is opened or not?
Thanks in advance for any suggestions
Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top