Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default Code to state if something if there or not?

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default Code to state if something if there or not?

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Code to state if something if there or not?


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
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Web lookup query (have: city+state | want: zip code) avatarr Excel Discussion (Misc queries) 0 April 7th 10 06:22 PM
replace state names with state code abbreviations se7098 Excel Worksheet Functions 3 July 25th 09 06:41 PM
zip code to city, state function xcelentform Excel Worksheet Functions 1 May 18th 06 11:59 PM
How to convert phone area-code to state name and time zone NoYouShmoopie Excel Discussion (Misc queries) 2 October 7th 05 02:38 PM
testing the state of a checkbox in vba code Paul James[_3_] Excel Programming 10 December 5th 03 07:55 PM


All times are GMT +1. The time now is 05:19 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"