Stopping VBA if 3 scenarios are met

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - I have existing VBA that am looking to enhance currently I have 3 criteria, currently working for 1 & 3 looking to add #2:

I have #2 embedded into the VBA with another sub and currently causing issues as it doesn't stop the current VBA Test1.
  1. Stopping VBA if file 1 is missing (working)
  2. Stopping VBA if file 2 is missing (trying to add)
  3. Stopping VBA is file 2 is empty (working)
VBA Code:
Sub TEST1
    Dim UsdRws As Long
    Dim FilePath As String
    Dim TestStr As String
    Dim FoundFile As Boolean
    Dim rws As Long
    Dim bottomrow, lastblank As Long
    Dim lr As Long
    Dim vCols As Variant, vRows As Variant
    Dim i As Long, k As Long

FilePath = "MY Files\" & Format(Now(), "MM-DD-YY") & " " & "TEST" & ".csv"

    If FilePath <> "" Then
        TestStr = Dir$(FilePath)
        FoundFile = (TestStr <> "")
    Else
        FoundFile = False
    End If

    If FoundFile Then
    
    'OTHER CODE/Processes (i.e. copying, pasting, transfering, filtering data, inputting formulas, closing files/sheets)

Call openFile2  

        Else
    MsgBox "File is Empty"
  End If
      
    Else
        MsgBox "File 1 NOT FOUND"
    End If

Code:
Sub OpenFile2()
    Dim sPath       As String
    Dim sPartial    As String
    Dim sFName      As String
  
    sPath = "MY FILES\"      ' <<<<< change accordingly
  
    sPartial = "dist_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"
    sFName = Dir(sPath & sPartial)
    If Len(sFName) > 0 Then
        Workbooks.OpenText sPath & sFName
    Else
        MsgBox "File not found.", vbExclamation
    End If
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You can turn the Sub into a Function that tells you whether the file was opened successfully:

Rich (BB code):
Sub TEST1
    Dim UsdRws As Long
    Dim FilePath As String
    Dim TestStr As String
    Dim FoundFile As Boolean
    Dim rws As Long
    Dim bottomrow, lastblank As Long
    Dim lr As Long
    Dim vCols As Variant, vRows As Variant
    Dim i As Long, k As Long

FilePath = "MY Files\" & Format(Now(), "MM-DD-YY") & " " & "TEST" & ".csv"

    If FilePath <> "" Then
        TestStr = Dir$(FilePath)
        FoundFile = (TestStr <> "")
    Else
        FoundFile = False
    End If

    If FoundFile Then
 
    'OTHER CODE/Processes (i.e. copying, pasting, transfering, filtering data, inputting formulas, closing files/sheets)

           If openFile2 Then
              ' Do all your processing here
           Else
                ' File2 was missing
           End If
        Else
    MsgBox "File is Empty"
  End If
   
    Else
        MsgBox "File 1 NOT FOUND"
    End If

Function OpenFile2() As Boolean

    Dim sPath       As String
    Dim sPartial    As String
    Dim sFName      As String

    sPath = "MY FILES\"      ' <<<<< change accordingly

    sPartial = "dist_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"
    sFName = Dir(sPath & sPartial)
    If Len(sFName) > 0 Then
        Workbooks.OpenText sPath & sFName
        OpenFile2 = True
    Else
        MsgBox "File not found.", vbExclamation
        OpenFile2 = False
    End If
End Function
 
Upvote 0
so if i change it to a function and file 2 is missing will the VBA stop right there in its tracks or will it just move to the next task?
 
Upvote 0
IF File1 is present AND File2 is present THEN
the code in the area labeled "do your processing here" will be executed. I missed your comment the first time, but addressed it here.

I would also change your logic flow just a bit for clarity. Here is a modification that is cleaner.

Note:
1. You did not show any code that determines whether File2 is empty.
2. You are checking for File1 name being null but it is not possible. See comment.


Rich (BB code):
Sub TEST1()

    Dim UsdRws As Long
    Dim FilePath As String
    Dim TestStr As String
    Dim FoundFile As Boolean
    Dim rws As Long
    Dim bottomrow, lastblank As Long
    Dim lr As Long
    Dim vCols As Variant, vRows As Variant
    Dim i As Long, k As Long

    FilePath = "MY Files\" & Format(Now(), "MM-DD-YY") & " " & "TEST" & ".csv"

    If FilePath = "" Then ' This test will never be TRUE because you are setting it to a string above.
       MsgBox "File is Empty"
    Else
        TestStr = Dir$(FilePath)
        FoundFile = (TestStr <> "")

        If Not OpenFile2 Then
           MsgBox "File2 is missing"
        Else
           'OTHER CODE/Processes (i.e. copying, pasting, transfering, filtering data, inputting formulas, closing files/sheets)
        End If
      
    End If
  
End Sub

Function OpenFile2() As Boolean

    Dim sPath       As String
    Dim sPartial    As String
    Dim sFName      As String

    sPath = "MY FILES\"      ' <<<<< change accordingly

    sPartial = "dist_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"
    sFName = Dir(sPath & sPartial)
    If Len(sFName) > 0 Then
        Workbooks.OpenText sPath & sFName
        OpenFile2 = True
    Else
        MsgBox "File not found.", vbExclamation
        OpenFile2 = False
    End If
End Function
 
Upvote 0
So this is File 1

VBA Code:
FilePath = "MY Files\" & Format(Now(), "MM-DD-YY") & " " & "TEST" & ".csv"

This is File 2

VBA Code:
OpenFile2()

And where I have file is emptying i see i left it out was right above my other processes

VBA Code:
If Workbooks("Compare").Sheets("Periodic").Range("I" & rows.count).End(xlUp).row > 2 Then

Not sure if it changes anything
 
Upvote 0
Ok tested it out but it is telling me twice my file 2 is missing. never telling me file 1 is missing.
 
Upvote 0
ok got it to work had to maneuver things around. Only open question is if both files are missing it will just tell me first file is missing but not telling me 2nd file is missing as well. And not sure how to still do that and not have my process keep going. Because other than that its working

Code:
Option Explicit
Sub RUN_DIV()
    Dim UsdRws As Long
    Dim FilePath As String
    Dim TestStr As String
    Dim FoundFile As Boolean
    Dim rws As Long
    Dim bottomrow, lastblank As Long
    Dim lr As Long
    Dim vCols As Variant, vRows As Variant
    Dim i As Long, k As Long
    
Application.ScreenUpdating = False
Application.EnableEvents = False

'find path if not found give msg (end of vba)
    FilePath = "MYPATH" & Format(Now(), "MM-DD-YY") & " " & "Div" & ".csv"
    
    If FilePath <> "" Then
        TestStr = Dir$(FilePath)
        FoundFile = (TestStr <> "")

    Else
        FoundFile = False
    End If
    
    If FoundFile Then
    
'If found check if Exists
    If Not OpenCopyFile2 Then
        MsgBox "File2 NOT FOUND"
    Else

'If found check if file2 is NOT empty
If Workbooks("Compare").Sheets("Periodic").Range("A" & rows.count).End(xlUp).row > 3 Then

Application.Calculation = xlCalculationManual

'OTHER PROCESSES

'Close File2
Call CLOSEFile2

Application.Calculation = xlCalculationAutomatic
    
Application.ScreenUpdating = True
Application.EnableEvents = True
      
         Else
         Call CLOSEFile2
    MsgBox "File2 is Empty"
  End If
      
  End If
    Else
    MsgBox "DIV File NOT FOUND"
    End If     
End Sub

Function OpenCopyFile2() As Boolean
    Dim sPath       As String
    Dim sPartial    As String
    Dim sFName      As String
  
    sPath = "MY PATH\"      ' <<<<< change accordingly
  
    sPartial = "dist_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"
    sFName = Dir(sPath & sPartial)
    If Len(sFName) > 0 Then
        Workbooks.OpenText sPath & sFName
        OpenCopyFile2 = True
    Else
        OpenCopyFile2 = False
    End If
End Function

Sub CLOSEFile2()
    Dim sPath       As String
    Dim sPartial    As String
    Dim sFName      As String
  
    sPath = "MY PATH\"      ' <<<<< change accordingly
  
    sPartial = "dist_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"
    sFName = Dir(sPath & sPartial)
    If Len(sFName) > 0 Then
        Workbooks(sFName).Close SaveChanges:=False
    Else
    End If
End Sub
 
Upvote 0
This will check all file conditions first, and if there are any errors give an error message describing all errors. Otherwise it will do the processing. I recommend keeping your indentation organized so it is easier to read the code.
VBA Code:
Option Explicit

Sub RUN_DIV()

   Dim UsdRws As Long
   Dim FilePath As String
   Dim TestStr As String
   Dim FoundFile As Boolean
   Dim rws As Long
   Dim bottomrow, lastblank As Long
   Dim lr As Long
   Dim vCols As Variant, vRows As Variant
   Dim i As Long, k As Long
   Dim ErrMsg As String
 
   Application.ScreenUpdating = False
   Application.EnableEvents = False
 
   'find path if not found give msg (end of vba)
   FilePath = "MYPATH" & Format(Now(), "MM-DD-YY") & " " & "Div" & ".csv"
 
   If FilePath <> "" Then
      TestStr = Dir$(FilePath)
   Else
      ErrMsg = "DIV File NOT FOUND"
   End If
 
   'If found check if Exists
   If Not OpenCopyFile2 Then
      ErrMsg = ErrMsg & vbCrLf & "File2 NOT FOUND"
   End If
    
   'If found check if file2 is NOT empty
   If Workbooks("Compare").Sheets("Periodic").Range("A" & Rows.Count).End(xlUp).Row <= 3 Then
      Call CLOSEFile2
      ErrMsg = ErrMsg & vbCrLf & "File2 is Empty"
   End If
 
   If ErrMsg <> "" Then
      MsgBox ErrMsg
   Else
       
      Application.Calculation = xlCalculationManual
    
      'OTHER PROCESSES
    
      'Close File2
      Call CLOSEFile2
    
      Application.Calculation = xlCalculationAutomatic
    
      Application.ScreenUpdating = True
      Application.EnableEvents = True
    
   End If
 
End Sub

Function OpenCopyFile2() As Boolean
    Dim sPath       As String
    Dim sPartial    As String
    Dim sFName      As String

    sPath = "MY PATH\"      ' <<<<< change accordingly

    sPartial = "dist_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"
    sFName = Dir(sPath & sPartial)
    If Len(sFName) > 0 Then
        Workbooks.OpenText sPath & sFName
        OpenCopyFile2 = True
    Else
        OpenCopyFile2 = False
    End If
End Function

Sub CLOSEFile2()
    Dim sPath       As String
    Dim sPartial    As String
    Dim sFName      As String

    sPath = "MY PATH\"      ' <<<<< change accordingly

    sPartial = "dist_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.txt"
    sFName = Dir(sPath & sPartial)
    If Len(sFName) > 0 Then
        Workbooks(sFName).Close SaveChanges:=False
    Else
    End If
End Sub
 
Upvote 0
I do agree on the indentation. when I copied over I think it messed up that is my fault.

I tested with file 1 missing and file 2 found but empty. So it is saying file 2 was empty but not saying file 1 was missing. I would expect to see File 1 missing then file 2 is empty. I even tried with File 1 present and still same message as prior which is what I would expect if file 1 is present in the latter test.

is it related to this?
If FilePath <> "" Then. if i flip the <> to = when the file is present it shows nicely just the inverse saying file wasnt found
 
Upvote 0
You have a problem that I mentioned earlier but it's still there.

VBA Code:
   'find path if not found give msg (end of vba)
   FilePath = "MYPATH" & Format(Now(), "MM-DD-YY") & " " & "Div" & ".csv"
 
   If FilePath <> "" Then
      TestStr = Dir$(FilePath)
   Else
      ErrMsg = "DIV File NOT FOUND"
   End If

FilePath can never be "". I think maybe you want to do this:

VBA Code:
   'find path if not found give msg (end of vba)
   FilePath = "MYPATH" & Format(Now(), "MM-DD-YY") & " " & "Div" & ".csv"
 
   TestStr = Dir$(FilePath)
   If TestStr = "" Then
      ErrMsg = "DIV File NOT FOUND"
   End If
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,877
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top