PC Review


Reply
Thread Tools Rate Thread

How to code macro with condition?

 
 
Eric
Guest
Posts: n/a
 
      20th Apr 2010
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
 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      20th Apr 2010
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



"Eric" <(E-Mail Removed)> wrote in message
news:96366C4F-9190-4ADB-9695-(E-Mail Removed)...
> 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



 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      20th Apr 2010
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


--
Jacob (MVP - Excel)


"Eric" wrote:

> 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

 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      23rd Apr 2010
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

"Jacob Skaria" wrote:

> 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
>
>
> --
> Jacob (MVP - Excel)
>
>
> "Eric" wrote:
>
> > 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

 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      23rd Apr 2010
Is there any code to check whether the file is opened or not?
Thanks in advance for any suggestions
Eric

"JLGWhiz" wrote:

> 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
>
>
>
> "Eric" <(E-Mail Removed)> wrote in message
> news:96366C4F-9190-4ADB-9695-(E-Mail Removed)...
> > 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

>
>
> .
>

 
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
Code condition LiAD Microsoft Excel Programming 3 8th Dec 2009 10:45 AM
Run Macro when a condition is met EXCELMACROS Microsoft Excel Programming 7 6th Jan 2009 08:14 PM
How to code macro with if condition? Eric Microsoft Excel Programming 2 16th Mar 2008 03:54 PM
Prevent macro or code from running until a condition is met Richardson Microsoft Access Form Coding 3 4th May 2004 04:40 PM
Condition Macro Faris_dxb Microsoft Access Macros 4 21st Apr 2004 12:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:13 AM.