willow1985
Well-known Member
- Joined
- Jul 24, 2019
- Messages
- 931
- Office Version
- 365
- Platform
- Windows
To anyone who can help.
I am still learning VBA and wrote a code to open a file but want to add some sort of error handler that if that file name/location does not exist, try another location and if that does not exist do a msg box that says "document not found" clear a certain cell and end sub.
Note/Context: The purpose of my whole code is to open a document, make sure it is filled out completely, and if so allow the user to close the log and if not, prompt the user that the form is incomplete.
The problem is there could be 2 possible names for the document which are referenced in 2 separate columns (-24 or 10) and locations (9 or 12).
Here is my code and how I would like to modify it done in comments:
Thank you to anyone who can help with the comments sections of this code
I am still learning VBA and wrote a code to open a file but want to add some sort of error handler that if that file name/location does not exist, try another location and if that does not exist do a msg box that says "document not found" clear a certain cell and end sub.
Note/Context: The purpose of my whole code is to open a document, make sure it is filled out completely, and if so allow the user to close the log and if not, prompt the user that the form is incomplete.
The problem is there could be 2 possible names for the document which are referenced in 2 separate columns (-24 or 10) and locations (9 or 12).
Here is my code and how I would like to modify it done in comments:
VBA Code:
Application.ScreenUpdating = False
Dim FORM As String
Dim FILE As String
FORM = ActiveCell.Offset(0, 9).Range("A1").Value
FILE = ActiveCell.Offset(0, -24).Range("A1").Value & ".xlsx"
FORM2 = ActiveCell.Offset(0, 12).Range("A1").Value
FILE2 = ActiveCell.Offset(0, 10).Range("A1").Value & ".xlsx"
Workbooks.Open FORM
'if error GoTo TryFORM2
If Worksheets("CAPA Form").Range("L12").Value = False Then
Workbooks("Quality Log.xlsm").Activate
ActiveCell.ClearContents
Workbooks(FILE).Activate
MsgBox "CAPA Report is Incomplete. Please Complete the Report Before Closing Out"
Else
Workbooks("Quality Log.xlsm").Activate
Workbooks(FILE).Close Savechanges:=False
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
ActiveCell.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWorkbook.Save
End If
'TryFORM2:
'Workbooks.Open FORM2
'if error Workbooks("Quality Log.xlsm").Activate ActiveCell.ClearContents MsgBox "Document not found" End Sub
'If Worksheets("CAPA Form").Range("L12").Value = False Then
' Workbooks("Quality Log.xlsm").Activate
'ActiveCell.ClearContents
' Workbooks(FILE2).Activate
' MsgBox "CAPA Report is Incomplete. Please Complete the Report Before Closing Out"
'Else
'Workbooks("Quality Log.xlsm").Activate
'Workbooks(FILE2).Close Savechanges:=False
'ActiveCell.Offset(0, 1).Range("A1").Select
' ActiveCell.FormulaR1C1 = "=TODAY()"
' ActiveCell.Select
' Selection.Copy
' Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
' :=False, Transpose:=False
'Application.CutCopyMode = False
'ActiveWorkbook.Save
'End If
Application.ScreenUpdating = True
End Sub
Thank you to anyone who can help with the comments sections of this code