PC Review


Reply
Thread Tools Rate Thread

Code to state if something if there or not?

 
 
Simon
Guest
Posts: n/a
 
      27th Oct 2008
Hi,

Every month I run macros which populate the latest months folder. I
wish to run a macro to test whether I have ran this macro yet this
month. So I thought of running a macro that tried to call up a known
file from that folder and if a file is there, a MsgBox returns "Macro
already ran this month".

However if I run it and nothing is there, because I asked for the
designated workbook to be there, it will cause a debug error.

How do I get round this (I thought with an If statement)

Thanks in advance :-)

Simon
 
Reply With Quote
 
 
 
 
dmoney
Guest
Posts: n/a
 
      27th Oct 2008
use the following code to suit your needs. set strDest to your filename.

Function FileExists(strDest As String) As Boolean
' Comments : Determines if the named file exists
' must be file, not directory
' Parameters: strDest - file to check
' Returns : True-file exists, false otherwise
'
Dim intLen As Integer

On Error Resume Next
intLen = Len(Dir(strDest))

FileExists = (Not Err And intLen > 0)

If DirExists(strDest) Then FileExists = False

End Function



PROC_EXIT:
Exit Function

PROC_ERR:
DirExists = False
Resume PROC_EXIT

End Function

"Simon" wrote:

> Hi,
>
> Every month I run macros which populate the latest months folder. I
> wish to run a macro to test whether I have ran this macro yet this
> month. So I thought of running a macro that tried to call up a known
> file from that folder and if a file is there, a MsgBox returns "Macro
> already ran this month".
>
> However if I run it and nothing is there, because I asked for the
> designated workbook to be there, it will cause a debug error.
>
> How do I get round this (I thought with an If statement)
>
> Thanks in advance :-)
>
> Simon
>

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      27th Oct 2008

Or, shorter,

Function FileExists(FullFileName As String) As Boolean
FileExists = (Dir(FullFileName) <> vbNullString)
End Function

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)

On Mon, 27 Oct 2008 15:03:00 -0700, dmoney
<(E-Mail Removed)> wrote:

>use the following code to suit your needs. set strDest to your filename.
>
>Function FileExists(strDest As String) As Boolean
> ' Comments : Determines if the named file exists
> ' must be file, not directory
> ' Parameters: strDest - file to check
> ' Returns : True-file exists, false otherwise
> '
> Dim intLen As Integer
>
> On Error Resume Next
> intLen = Len(Dir(strDest))
>
> FileExists = (Not Err And intLen > 0)
>
> If DirExists(strDest) Then FileExists = False
>
>End Function
>
>
>
>PROC_EXIT:
> Exit Function
>
>PROC_ERR:
> DirExists = False
> Resume PROC_EXIT
>
>End Function
>
>"Simon" wrote:
>
>> Hi,
>>
>> Every month I run macros which populate the latest months folder. I
>> wish to run a macro to test whether I have ran this macro yet this
>> month. So I thought of running a macro that tried to call up a known
>> file from that folder and if a file is there, a MsgBox returns "Macro
>> already ran this month".
>>
>> However if I run it and nothing is there, because I asked for the
>> designated workbook to be there, it will cause a debug error.
>>
>> How do I get round this (I thought with an If statement)
>>
>> Thanks in advance :-)
>>
>> Simon
>>

 
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
replace state names with state code abbreviations se7098 Microsoft Excel Worksheet Functions 3 25th Jul 2009 06:41 PM
Help - State Code Table Lisa - NH Microsoft Access Getting Started 2 3rd Mar 2008 05:49 PM
State code ladybug via AccessMonster.com Microsoft Access VBA Modules 2 7th Mar 2007 08:27 PM
zip code to city, state function xcelentform Microsoft Excel Worksheet Functions 1 18th May 2006 11:59 PM
color code a state however I need 2 colors =?Utf-8?B?YnJhZA==?= Microsoft Powerpoint 1 23rd Nov 2004 05:42 PM


Features
 

Advertising
 

Newsgroups
 


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