If function that returns message box error if true or creates file if false

Challis

New Member
Joined
Oct 22, 2017
Messages
21
Hi
I have a macro that populates a word document template from a table in excel. Users press a report generator button and it creates the file.
However, for the cases as shown below where the user selects "compliance", "scope", or "vendor" in a specific cell i want the code to return a message box and not proceed further to generate the file. When i run this code below, it keeps creating the file. Can somone please tell me what I'm doing wrong with the IF statement.
Thanks,

Code:
Private Sub GenerateReport_Click()
Dim ws As Worksheet
Dim btmrow As Integer
Set ws = ThisWorkbook.Sheets("Register")


'define btmrow (bottom row)
btmrow = Cells(Rows.Count, "A").End(xlUp).Row


'IF statement for jobs that should not produce a report
If ws.Cells(btmrow, 8).Value = "Compliance" Then
MsgBox ("No report generated for compliance NDT")
ElseIf ws.Cells(btmrow, 8).Value = "Scope" Then
MsgBox ("No report generated for compliance Scope")
ElseIf ws.Cells(btmrow, 8).Value = "Vendor" Then
MsgBox ("No report generated for vendor Scope")
Exit Sub


Else
'Create new file....
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Unfortunately it is not possible to understand your task as for some reason you have not posted your complete SUB code.
 
Upvote 0
Code:
Private Sub GenerateReport_Click()
Dim ws As Worksheet
Dim btmrow As Integer
Set ws = ThisWorkbook.Sheets("Register")


'define btmrow (bottom row)
btmrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row


'IF statement for jobs that should not produce a report
If ws.Cells(btmrow, 8).Text = "COMPLIANCE" Then
MsgBox ("No report generated for compliance NDT")
Exit Sub
ElseIf ws.Cells(btmrow, 8).Text = "SCOPE" Then
MsgBox ("No report generated for scope documents")
Exit Sub
ElseIf ws.Cells(btmrow, 8).Text = "VENDOR" Then
MsgBox ("No report generated for Vendor Scope")
Exit Sub


Else


'Create new file
Dim objWord As Object
Dim objdoc As Object
Set objWord = CreateObject("Word.Application")
Set objdoc = objWord.documents.Add
objWord.Visible = True
objWord.documents.Open "\\BWISHARE1\SHARE\ABU Materials and Inspection Engineering\Gorgon\Team\Working\SCJW\Process Development\Report Register Development\Templates\Pressure Piping Inspection Report Template.docm"
With objWord.ActiveDocument
 .Bookmarks("Date").Range.Text = ws.Cells(btmrow, 11).Text
 .Bookmarks("EquipmentNumber").Range.Text = ws.Cells(btmrow, 7).Text
 .Bookmarks("Name").Range.Text = ws.Cells(btmrow, 13).Text
 .Bookmarks("ReportNumber").Range.Text = ws.Cells(btmrow, 10).Value
 .Bookmarks("Unit").Range.Text = ws.Cells(btmrow, 6).Text
 .Bookmarks("WorkOrder").Range.Text = ws.Cells(btmrow, 3).Text
 .Bookmarks("Location").Range.Text = ws.Cells(btmrow, 5).Text


'Dimension filing convention
Dim Fpath As String
Dim FVariable As String
Dim Fname As String


'Define FPath
Fpath = "\\BWISHARE1\SHARE\ABU MATERIALS AND INSPECTION ENGINEERING\GORGON\EQUIPMENT INDEX\GGP"


'Define FVariable
If ws.Cells(btmrow, 5).Text = "PIPELINE" Then
FVariable = "PIPELINE" & "\" & "WO" & ws.Cells(btmrow, 3).Text
ElseIf ws.Cells(btmrow, 5).Text = "NT" Then
FVariable = "Non-Tagged Equipment" & "\" & "WO" & ws.Cells(btmrow, 3).Value
Else
FVariable = "GGP-" & ws.Cells(btmrow, 5).Value & "\" & "GGP-" & ws.Cells(btmrow, 5).Value & "-" & ws.Cells(btmrow, 6).Value & "\" & ws.Cells(btmrow, 7).Value & "\Inspections\Data"
End If
End With


'to assing file name
Fname = ws.Cells(btmrow, 10).Text


'to save file
objWord.ActiveDocument.SaveAs Filename:=Fpath & "\" & FVariable & "\" & Fname
Debug.Print Fname
Set objWord = Nothing
End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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