Cell value must match criteria before save can continue

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
Evening,
I am using the code shown below which works fine, just need an edit to catch an error.

In cell A3 will be a value like so.04 APRIL 05 MAY 06 JUNE etc etc

When i run my code i need to make sure that the value in cell A3 is valid from a list BEFORE the save continues,thus show me a MsgBox to say otherwise.

Example.
IF the value is 06 JUNE then the code will run & the save will take place.
BUT
If the value is say 01 MARCH then a MsgBox will be shown & the code doesnt run anymore.

Im not sure how i write my list of what is allowed,

Something like,
Rich (BB code):
If Range("A3").Value = 04 APRIL, 05 MAY, 06 JUNE Then
Code will contine.
Else
MsgBox "INVALID MONTH"
Exit sub



Rich (BB code):
Private Sub INCOMETRANSFER()

    Dim strFileName As String
    
        strFileName = "C:\Users\Ian\Desktop\GRASS CUTTING\CURRENT GRASS SHEETS\INCOME\" & _
        Range("A3") & " " & Range("D3") & " " & Range("E3") & ".pdf"

    If Dir(strFileName) <> vbNullString Then
        MsgBox "GRASS CUTTING INCOME SHEET " & Range("A3") & " " & Range("D3") & " WAS NOT SAVED AS IT ALREADY EXISTS", vbCritical + vbOKOnly, "INCOME SUMMARY GRASS SHEET FAILED MESSAGE"
        PDFExists = True
        Exit Sub
    Else
        PDFExists = False
    End If
    
    With ActiveSheet
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True
        MsgBox "GRASS CUTTING INCOME SHEET " & Range("A3") & " " & Range("D3") & " WAS SAVED SUCCESSFULLY", vbInformation + vbOKOnly, "INCOME SUMMARY GRASS SHEET SUCCESSFULL MESSAGE"
        Range("A5:A30").NumberFormat = "@"
        
    End With

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Please advise how i write this part correctly to allow various values.

Rich (BB code):
If Range("A3").Value = "04 APRIL" "05 MAY" "06 JUNE" Then
 
Upvote 0
Code now put in correct place.

Im not sure if the long range code is correctly written BUT i get no RTE.
If i enter 05 MAY in cell A3 & run the code i see the MsgBox 05 MAY IS AN INVALID MONTH

So something is wrong as it should be allowed to save & not give me the MsgBox

Rich (BB code):
Private Sub TransferButton_Click()
    If Range("A3").Value = "04 APRIL, 05 MAY, 06 JUNE, 07 JULY, 08 AUGUST, 09 SEPTEMBER, 10 OCTORBER, 11 NOVEMBER, 12 DECEMBER, 13 JANUARY, 14 FEBRUARY, 15 MARCH, 16 APRIL" Then
    
    Call INCOMETRANSFER
    
    If PDFExists Then
    Exit Sub
    
    Else
        Call SUMMARYTRANSFER
    End If
        INCOMEMONTHYEAR.Show
    Else
        MsgBox Range("A3") & vbNewLine & "IS AN INVALID MONTH", vbCritical + vbOKOnly, "INVALID MONTH IN CELL A3"
    Exit Sub
    End If
End Sub
 
Upvote 0
Are you trying to check to see if cell A3 is EXACTLY equal to the full string:
"04 APRIL, 05 MAY, 06 JUNE, 07 JULY, 08 AUGUST, 09 SEPTEMBER, 10 OCTORBER, 11 NOVEMBER, 12 DECEMBER, 13 JANUARY, 14 FEBRUARY, 15 MARCH, 16 APRIL"
or if A3 is equal to any one of those dates contained in that string (i.e. A3 equal to "07 JULY")?
 
Upvote 0
If cell A3 value is one of the values in the string then let the code continue.

If cell A3 value isnt in the string then show the MsgBox,

Rich (BB code):
MsgBox Range("A3") & vbNewLine & "IS AN INVALID MONTH", vbCritical + vbOKOnly, "INVALID MONTH IN CELL A3"
 
Upvote 0
The way you have it written, you are checking to see if it is equal to that entire long string.
Try this instead:
VBA Code:
Private Sub TransferButton_Click()
    
    Dim dteString As String
    dteString = "04 APRIL, 05 MAY, 06 JUNE, 07 JULY, 08 AUGUST, 09 SEPTEMBER, 10 OCTORBER, 11 NOVEMBER, 12 DECEMBER, 13 JANUARY, 14 FEBRUARY, 15 MARCH, 16 APRIL"

    If InStr(dteString, Range("A3").Value) > 0 Then
        Call INCOMETRANSFER
    
        If PDFExists Then
            Exit Sub
        Else
            Call SUMMARYTRANSFER
        End If
        INCOMEMONTHYEAR.Show
    Else
        MsgBox Range("A3") & vbNewLine & "IS AN INVALID MONTH", vbCritical + vbOKOnly, "INVALID MONTH IN CELL A3"
        Exit Sub
    End If

End Sub
 
Upvote 0
Solution
That worked thanks.

Most things im unsure how to write but put that as i cant explain 100%
Once i get a code back i can work through it but unable to do it myself at the start

Many Thanks
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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