VBA Excel Stop On Error Goto - vba

I have written On Error GoTo ErrorMessage somewhere in a subroutine. I want to stop this command after End if command as shown below.
On Error GoTo ErrorMessage
Sheet2.Range("A1").Font.Bold = True
Sheet2.Range("B1").Font.Bold = True
If LastRow_Sh2 >= First_Row_Sheet2 Then
Sheet2.Range(FromCol & First_Row_Sheet2 & ":" & ToCol & LastRow_Sh2).ClearContents
Exit Sub
End If
' Stop here
' I have some codes here
ErrorMessage:
MsgBox ("Error message: The input values are invalid")

Take a look at the example below that shows both - error handling manually and allow VBA to catch a runtime error:
Sub Test()
On Error GoTo ErrorHandler
Dim Divisor As Integer
Dim Num As Integer
Dim Answer As Double
Num = 100
Divisor = 0
' ================================
' throw an error here forcefully
' and allow ErrorHandler to handle
' the error
' ================================
Answer = Num / Divisor
MsgBox "Answer is " & Answer, vbOKOnly + vbInformation, "Answer"
' stop error handling
On Error GoTo 0
' ================================
' throw an error here forcefully
' and allow VBA to handle the error
' ================================
Answer = Num / Divisor
MsgBox "Answer is " & Answer, vbOKOnly + vbInformation, "Answer"
Exit Sub
ErrorHandler:
MsgBox "Handling the error here", vbOKOnly + vbInformation, "ErrorHandler"
Resume Next
End Sub
Based on this, you can modify your code slightly to allow VBA to handle the error on runtime.
On Error GoTo ErrorMessage
Sheet2.Range("A1").Font.Bold = True
Sheet2.Range("B1").Font.Bold = True
If LastRow_Sh2 >= First_Row_Sheet2 Then
Sheet2.Range(FromCol & First_Row_Sheet2 & ":" & ToCol & LastRow_Sh2).ClearContents
Exit Sub
End If
' Stop here
' The statement below will disable error handling that was
' done by ErrorMessage
On Error GoTo 0
' I have some codes here
' If this block of code has errors, VBA will handle it and
' allow debugging
ErrorMessage:
MsgBox ("Error message: The input values are invalid")

try this
On Error Resume Next '".. GoTo ErrorMessage" replaced by "...resume next"
Sheet2.Range("A1").Font.Bold = True
Sheet2.Range("B1").Font.Bold = True
If LastRow_Sh2 >= First_Row_Sheet2 Then
Sheet2.Range(FromCol & First_Row_Sheet2 & ":" & ToCol & LastRow_Sh2).ClearContents
Exit Sub
End If
If Err.Number > 0 Then GoTo ErrorMessage ' Stop here
' I have some codes here
ErrorMessage:
MsgBox ("Error message: The input values are invalid")

Related

How to make my VBA code look better and reduce redundancy?

I have written very ugly VBA code, but I don't know how I can make this look better.
I have a function (say Function Main) that calls another 3 functions inside it. These 3 functions have to happen in order, and if one fails, the rest shouldn't happen. Also, I need to retrieve proper, descriptive error message. I have another software that invokes VBA code and calls this Main function, and if there is an error, I need to use that information, so I am returning a value (either a descriptive error message or "Completed") from Main as well.
Function Main
Dim result As String
result = ProcessA();
If result <> "OK" Then
Main = result
Exit Function
End If
result = ProcessB();
If result <> "OK" Then
Main = result
Exit Function
End If
result = ProcessC();
If result <> "OK" Then
Main = result
Exit Function
End If
Main = "Completed"
End Function
Function ProcessA()
On Error GoTo Errored
'Do some operation
ProcessA = "OK" 'At the end of operation, assign OK and
Exit Function 'Exit function here
Errored: 'In case of an error, get error information and return it
ProcessA = "Error Line: " & Erl & Chr(13) _
& "Error Description: " & Err.Description & Chr(13) _
& "Error at Process A"
End Function
'ProcessB and ProcessC also have a very similar style.
As you can see, my Main function has so many redundant code, but I can't think of any other better way. Could someone give me advice on how to make this better?
I would suggest re-throwing the error then catching it in the calling method:
Function Main()
On Error GoTo haveError
ProcessA
ProcessB
ProcessC
Main = "Completed"
Exit Function
haveError:
Main = err.Description
End Function
Sub ProcessA()
On Error GoTo Errored
'Do some operation
Exit Sub
Errored:
err.Raise Number:=err.Number, Description:=DescribeError(err, "ProcessA")
End Sub
Sub ProcessB()
On Error GoTo Errored
'Do some operation
Debug.Print 1 / 0 'eg. error here....
Exit Sub
Errored:
err.Raise Number:=err.Number, Description:=DescribeError(err, "ProcessB")
End Sub
Sub ProcessC()
On Error GoTo Errored
'Do some operation
Exit Sub
Errored:
err.Raise Number:=err.Number, Description:=DescribeError(err, "ProcessC")
End Sub
'utility
Function DescribeError(err As ErrObject, procName As String)
DescribeError = "Error in method: " & procName & vbLf & _
"Error Line: " & erl & vbLf & _
"Error Description: " & err.Description
End Function
Function Main
Dim result As String
result = ProcessA(): If result <> "OK" Then goto errResult
result = ProcessB(): If result <> "OK" Then goto errResult
result = ProcessC(): If result <> "OK" Then goto errResult
Main = "Completed"
Exit Function
errResult:
Main=result
End Function
Private errorMsg As String
Function Main()
If ProcessA Then
If ProcessB Then
If ProcessC Then
Main = "Completed"
End If
End If
End If
If Main = vbNullString Then Main = errorMsg
End Function
Private Function ProcessA() As Boolean
ProcessA = True
Exit Function
Err:
ReportError "Process A"
End Function
Private Sub ReportError(argSrc As String)
errorMsg = "Error Line: " & Erl & Chr(13) _
& "Error Description: " & Err.Description & Chr(13) _
& "Error at " & argSrc
End Sub
Using Application.Run(), you can process any number of steps in a loop:
Function Main()
Dim f, result
For Each f In Array("ProcessA", "ProcessB", "ProcessC") ' and so on
result = Application.Run(f)
If result <> "OK" Then
Main = result
Exit Function
End If
Next
Main = "Completed"
End Function

MS Access: Trying to create an error if there is a duplicate record but code flags everything

I have a form that if a duplicate record is entered, the form creates an error message and prevents the record from being entered. However, my code is popping up the error message no matter what I'm putting in. My code is this...
Private Sub cmdSave_Click()
' ToDo fix the labels in this function so they match the function name. Just cosmetic.
On Error GoTo Add_CmdSave_Click_Err
On Error Resume Next
' ToDo fix the labels in this function so they match the function name. Just cosmetic.
On Error GoTo Add_CmdSave_Click_Err
On Error Resume Next
Me.cbCompletedTrainingID = Me.IntermediateID
'
Dim OKToSave As Boolean
OKToSave = True
If Not SomethingIn(Me.[fIntermediate FacultyID]) Then ' Null
Beep
MsgBox "A faculty member is required", vbOKOnly, "Missing Information"
OKToSave = False
End If
If Not SomethingIn(Me.[fIntermediate TrainingID]) Then
Beep
MsgBox "A training is required", vbOKOnly, "Missing Information"
OKToSave = False
Else
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[IntermediateID] = " & Me.[fIntermediate FacultyID] And Me.[fIntermediate TrainingID]
If Not rs.EOF Then
Beep
MsgBox "This person has already completed this training", vbOKOnly, "Duplicate Training Completion"
OKToSave = False
End If
End If
If OKToSave Then
' If we get this far, all data is valid and it's time to save
Me.Dirty = False
DoCmd.GoToRecord , "", acNewRec
End If
Add_CmdSave_Click_Exit:
Exit Sub
Add_CmdSave_Click_Err:
Resume Add_CmdSave_Click_Exit
End Sub
The issue, from my standpoint, lies in this part...
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[IntermediateID] = " & Me.[fIntermediate FacultyID] And Me.[fIntermediate TrainingID]
If Not rs.EOF Then
Beep
MsgBox "This person has already completed this training", vbOKOnly, "Duplicate Training Completion"
OKToSave = False
End If
What am I doing wrong?
Have a look at How to debug dynamic SQL in VBA.
This line makes no sense as it is:
rs.FindFirst "[IntermediateID] = " & Me.[fIntermediate FacultyID] And Me.[fIntermediate TrainingID]
You probably want something like
S = "[IntermediateID] = " & Me.[fIntermediate FacultyID] & " And [TrainingID] = " & Me.[fIntermediate TrainingID]
Debug.Print S ' Ctrl+G shows the output
rs.FindFirst S
Also, remove all these On Error Resume Next - this will happily ignore any errors, making debugging nearly impossible.
Also useful: Debugging VBA Code
And there is more: If Recordset.FindFirst doesn't find a match, it doesn't trigger .EOF. It sets the .NoMatch property.
rs.FindFirst S
If rs.NoMatch Then
' all is good, proceed to save
Else
' record exists
End If
This should work as intended:
Dim rs As DAO.Recordset
Dim Criteria As String
Set rs = Me.RecordsetClone
Criteria = "[IntermediateID] = " & Me![fIntermediate FacultyID].Value & " And [TrainingID] = " & Me![fIntermediate TrainingID].Value & ""
Debug.Print OKToSave, Criteria
rs.FindFirst Criteria
If Not rs.NoMatch Then
Beep
MsgBox "This person has already completed this training", vbInformation + vbOKOnly, "Duplicate Training Completion"
OKToSave = False
End If
rs.Close
Debug.Print OKToSave

Method ‘FindFirst’ of object ‘Recordset2’ failed. After not saving new record

In a form, create a new record, edit some data but before saving it use a combo box on the form to select another record to navigate to. This triggers the cboSalePicker_AfterUpdate. Then during this sub Form_BeforeUpdate executes. The user clicks no on the MsgBox to not save the new record. Then the rest of cboSalePicker_AfterUpdate is executed however the following error message is displayed:
Error Message
Error number -2147417848: Method ‘FindFirst’ of object ‘Recordset2’ failed.
Associated with the line Me.Recordset.FindFirst "[SaleID] = " & Str(Nz(cboSalePicker.Value, 0))
However, if the new record is saved no error is produced and the code performs as expected.
Form_BeforeUpdate
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo ErrorHandler
Dim strMsg As String
Dim iResponse As Integer
'Specify the mesage to display
strMsg = "Do you wish to save the changes?" & Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard changes."
'Display the msg box
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")
'Check response
If iResponse = vbNo Then
'Undo the change.
DoCmd.RunCommand acCmdUndo
'Cancel the update
Cancel = True
End If
Exit Sub
ErrorHandler:
MsgBox "Error number " & Err.Number & ": " & Err.Description
End Sub
cboSalePicker_AfterUpdate
Private Sub cboSalePicker_AfterUpdate()
On Error GoTo ErrorHandler
Me.Recordset.FindFirst "[SaleID] = " & Str(Nz(cboSalePicker.Value, 0))
Exit Sub
ErrorHandler:
MsgBox "Error number " & Err.Number & ": " & Err.Description
End Sub
Thanks
You are converting Your SaleID into a String using this
Str(Nz(cboSalePicker.Value, 0))
But your find first is looking for a number. If SaleID is a number then remove the Str() function from your code around the combobox value.
To show the concatenation try this
Private Sub cboSalePicker_AfterUpdate()
On Error GoTo ErrorHandler
Dim sCriteria as String
sCriteria = "[SaleID] = " & Nz(Me.cboSalePicker, 0)
debug.print sCriteria
Me.Recordset.FindFirst sCriteria
Exit Sub
ErrorHandler:
MsgBox "Error number " & Err.Number & ": " & Err.Description
End Sub
Comment out the first error handler line whilst you are debugging things.

vba error number 9 subscript out of range

I'm trying to make a Excel 2007 vba code to select a sheet using userform. While testing, I'm getting 'Subscript out of range' Error if I input a sheetname which is not there in workbook. my code is below.
Private Sub Okbtn_Click()
sheetname = Me.ComboBox1.Value
If sheetname = "" Then
Unload Me
MsgBox "Sheet Name not enetered", vbExclamation, "Hey!"
Exit Sub
End If
ThisWorkbook.Sheets(sheetname).Activate 'Error points here!!
On Error GoTo errmsg
'If Error.Value = 9 Then GoTo errmsg
Unload Me
MsgBox "Now you are in sheet: " & sheetname & "!", vbInformation, "Sheet Changed"
errmsg:
MsgBox ("Sheet name not found in " & ThisWorkbook.Name & " !")
End Sub
The error is in ThisWorkbook.Sheets(sheetname).Activate . I tried to put some error handing tricks before and after the problem line, but Im getting the same error-9.
Im very new to coding. I think I explained the problem correctly. I want to avoid the error from popping up, but should show a customized message instead.
If you move your On Error GoTo errmsg code line above the worksheet activation, the error should be handled by the error trap routine. You just need to exit the sub before reaching the same routine if successful.
On Error GoTo errmsg
ThisWorkbook.Sheets(sheetname).Activate 'Error points here!!
Unload Me
MsgBox "Now you are in sheet: " & sheetname & "!", vbInformation, "Sheet Changed"
Exit Sub
errmsg:
MsgBox ("Sheet name not found in " & ThisWorkbook.Name & " !")
End Sub
You need to set the error handler before executing the instruction that might cause an error. Something like this
Private Sub Okbtn_Click()
sheetname = Me.ComboBox1.Value
If sheetname = "" Then
Unload Me
MsgBox "Sheet Name not enetered", vbExclamation, "Hey!"
Exit Sub
End If
On Error GoTo errmsg
ThisWorkbook.Sheets(sheetname).Activate 'Error points here!!
'If Error.Value = 9 Then GoTo errmsg
Unload Me
MsgBox "Now you are in sheet: " & sheetname & "!", vbInformation, "Sheet Changed"
Exit Sub ' Avoid executing handler code when ther is no error
errmsg:
MsgBox ("Sheet name not found in " & ThisWorkbook.Name & " !")
End Sub
put On Error GoTo errmsg above ThisWorkbook.Sheets(sheetname).Activate
'''''
On Error GoTo errmsg
ThisWorkbook.Sheets(sheetname).Activate
'''''
error handling always must be before the line where you can receive the error
It would be clearer if you error handling was wrapped around a single line testing if the worksheet existed
As an example your current code will flag any error - such as the sheet being hidden - as the sheet not existing.
Private Sub Okbtn_Click()
Dim strSht As String
Dim ws As Worksheet
strSht = Me.ComboBox1.Value
If Len(strSht) = 0 Then
Unload Me
MsgBox "Sheet Name not entered", vbExclamation, "Hey!"
Exit Sub
End If
On Error Resume Next
Set ws = ThisWorkbook.Sheets(strSht)
On Error GoTo 0
If Not ws Is Nothing Then
If ws.Visible Then
Application.Goto ws.[a1]
MsgBox "Now you are in sheet: " & strSht & "!", vbInformation, "Sheet Changed"
Else
MsgBox ("Sheet exists but is hidden")
End If
Else
MsgBox ("Sheet name not found in " & ThisWorkbook.Name & " !")
End If
End Sub

Excel VBA & VB6 Printer

I have the following code, this code was written in VB6 but i can not open the form or check any references.
Private Sub PopulatePrinterCombo(cmbDestination As ComboBox)
Dim objPrinter As Printer 'a printer in the Printers collection object
'Add the printers to the combo box
For Each objPrinter In printers
cmbPrinter.AddItem objPrinter.DeviceName
Next
'Set current selection to the default printer
cmbDestination.Text = Printer.DeviceName
End Sub
I am currently copying the code onto Excel VBA macro, the problem is the Dim objPrinter As Printer code, i keep getting an error message saying "USER DEFINED TYPE NOT DEFINED", do i need a reference to add on VBA to be able to get the option of declaring a variable as a "Printer" or something?
My second question is that i do not fully understand the "Printers" in the line For Each objPrinter In printers, what is "Printers"? can someone please explain that to me.
Thank you
PART 2
I am now trying to print files, i have the following as my code:
'Initialize values
intDraftsPrinted = 0
If objDraftPaths.Count > 1 Then
Else
intSelectedDraftCount = CountSelectedDrafts
End If
'prompt user to make sure
intMsgBoxResponse = MsgBox("You selected " & intSelectedDraftCount & " part numbers. After removing duplicates" & vbNewLine & "there were " & objDraftPaths.Count & " unique draft files found." & vbNewLine & "Do you want to print these files?", vbYesNo, "TD Printer")
If intMsgBoxResponse <> vbYes Then
intSelectedDraftCount = 0 'So the following for loop will not entered
Else
intSelectedDraftCount = objDraftPaths.Count
End If
For i = 1 To intSelectedDraftCount
booSuccess = False
'open the draft file
Set objDraftDocument = OpenSolidEdgeDraft(objDraftPaths.Item(i))
If objDraftDocument Is Nothing Then
'could not open file
MsgBox "Could not open the following draft file:" & vbNewLine & _
objDraftPaths.Item(i), vbExclamation, "Solid Edge Error"
Else
'Print the draft file
For Each objSheet In objDraftDocument.Sheets
strSheetSize = DetermineSheetSize(objSheet)
If strSheetSize <> "" Then
'Determine orientation
If InStr(1, strSheetSize, "90") <> 0 Then
'Print as landscape
intOrientation = vbPRORLandscape
Else
'Print as portrait
intOrientation = vbPRORPortrait
End If
'Specify Sheet Size
Select Case Left(strSheetSize, 1)
Case "A"
intPaperSize = VBRUN.PrinterObjectConstants.vbPRPSLetter
Case "B"
intPaperSize = VBRUN.PrinterObjectConstants.vbPRPS11x17
Case "C"
intPaperSize = VBRUN.PrinterObjectConstants.vbPRPSCSheet
Case "D"
intPaperSize = VBRUN.PrinterObjectConstants.vbPRPSDSheet
Case "E"
intPaperSize = VBRUN.PrinterObjectConstants.vbPRPSESheet
Case Else
intPaperSize = VBRUN.PrinterObjectConstants.vbPRPSLetter
End Select
'Enable error handling
On Error Resume Next
'Activate the current sheet
objSheet.Activate
If Err Then
'Could not activate sheet
MsgBox "An error occurred while attempting to print: " & vbNewLine & objDraftPaths.Item(i) & vbNewLine & "The error was:" & vbNewLine & "Error Number: " & Err.Number & vbNewLine & "Error Description: " & Err.Description, vbExclamation, "Solid Edge Error"
Err.Clear
Else
'Print to the printer specified by the combo box
objDraftDocument.PrintOut cmbPrinter.Text, 1, intOrientation, intPaperSize, , , , igPrintSelected
If Err Then
'Could not print document
MsgBox "An error occurred while attempting to print: " & vbNewLine & objDraftPaths.Item(i) & vbNewLine & "The error was:" & vbNewLine & "Error Number: " & Err.Number & vbNewLine & "Error Description: " & Err.Description, vbExclamation, "Solid Edge Error"
Err.Clear
Else
booSuccess = True
End If
End If
'Disable error handling
On Error GoTo 0
End If
Next
'Close the file
objDraftDocument.Close False
intDraftsPrinted = intDraftsPrinted + 1
End If
Next i
'Dereference objects
Set objSheet = Nothing
Set objDraftDocument = Nothing
'Set objDraftPaths = Nothing
PrintSelectedDrafts = intDraftsPrinted
Now the problem comes when i hits the line that says: intOrientation = vbPRORLandscape
in excel VBA, it does not recognize "vbPRORLandscape" as well as the next line "vbPRORPortrait". Is there a way to fix that?
Also, i have a feeling that VBRUN.PrinterObjectConstants.vbPRPSLetter and the rest of those lines might not work out as well. It works in VB6 though.
Thank you
It appears the Printers Collection is available in the MS Access VBA environment but I do not believe it is intrinsic to the Excel VBA environment.
I use the WshNetwork object of Windows Script Host to list the available printers. I use the subroutine below to populate a ComboBox with the list of printers that are connected to the system. In order for this code to work you will need to add the "Windows Script Host Object Model" reference to your VBA project. (Menu: Tools > References [Select from list])
I added the (j) loop to alphabetize the list.
Sub populatePrintersList()
Dim nwo As New WshNetwork
Dim i As Integer
Dim j As Integer
Dim bAdd As Boolean
bAdd = True
cmbPrinter.Clear
For i = 0 To (nwo.EnumPrinterConnections.Count / 2) - 1
For j = 0 To cmbPrinter.ListCount - 1
If nwo.EnumPrinterConnections(i * 2 + 1) < cmbPrinter.List(j) Then
cmbPrinter.AddItem nwo.EnumPrinterConnections(i * 2 + 1), j
bAdd = False
Exit For
End If
Next j
If bAdd Then cmbPrinter.AddItem nwo.EnumPrinterConnections(i * 2 + 1): bAdd = True
Next i
cmbPrinter.ListIndex = 0
End Sub
Part 2:
MSDN contains reference material for the Worksheet.PrintOut method: Worksheet.PrintOut
In depth documentation for the methods and properties of the Worksheet.PageSetup object can also be found on MSDN: Worksheet.PageSetup
I suggest using these resources to find a plethora of answers.