I'm fixing the bellow error "datatable importsheet operation failed. Invalid file" using this way:
On error resume next
DataTable.ImportSheet Environment("STPFilePath"),Environment("TestScriptName"),"Action2"
If Err.Description = "The DataTable.ImportSheet operation failed. Invalid file." \n "Line (20): "DataTable.ImportSheet Environment("STPFilePath"),Environment("TestScriptName"),"Action2""." Then
list of instructions
end if
But I got this error:
The test run cannot continue due to a syntax error.
Expected 'Then'
Line (24): "If Err.Description = "The DataTable.ImportSheet operation failed. Invalid file." \n "Line (20): "DataTable.ImportSheet Environment("STPFilePath"),Environment("TestScriptName"),"Action2""." Then".
Everything looks right. Please any help ? where I'm wrong ?
I'm really struggling with what you are trying to do and your comments are not helping...
When using
On Error Resume Next
and you encounter an error while executing a statement the following things will happen;
The statement will be skipped and the script will move to the next statement and carry on execution.
The object Err will be populated with details of the error that caused the statement to be skipped.
The Err object is made up of a few key properties that describe the error that occurred;
Number - The Error Code of the error that was raised.
Source - The Source of the error that was raised, this can be 3rd party library, the VBScript Runtime etc.
Description - A free text description of the error.
If you want to check for error
The DataTable.ImportSheet operation failed. Invalid file.
when
DataTable.ImportSheet Environment("STPFilePath"),Environment("TestScriptName"),"Action2"
is executed you need to capture the Err.Number code that corresponds to that specific error.
Something like this;
On Error Resume Next
DataTable.ImportSheet Environment("STPFilePath"),Environment("TestScriptName"),"Action2"
'Check that an error occurs
If Err.Number <> 0 Then
'Identify specific error and deal with accordingly.
Select Case Err.Number
Case 20012 'Code for DataTable.ImportSheet operation failed
'List of instructions...
Case Else
'Unhandled error show to screen
MsgBox "Error: " & Err.Number & " (" & Err.Source & ") - " & Err.Description
End Select
'Finished handling the error
Call Err.Clear()
End If
What I find most confusing is the miss-information in this thread.
"I need the description and not the number and the problem is to use Err.Details and not Err.Description in my case. Now, it is working." - Ref
The Err object does not contain a property called Details unless this is something specific to QTP but so far I haven't been able to find anything with a quick Google Search. So how you can say "it is now working" is beyond me.
Basic rules:
Double Quotes (") are escaped by doubling ("")
No escapes like "\n" in VBScript string literals (concatenation (or replacement) needed)
The concatenation operator is &
Basic strategy: Simplify, Divide, and conquer:
>> s = "1" & vbCrLf & "-""pi""-"
>> t = "1" & vbCrLf & "-""pi""-"
>> WScript.Echo s
>> WScript.Echo t
>> If s = t Then
>> WScript.Echo "no surprise"
>> End If
>>
1
-"pi"-
1
-"pi"-
no surprise
>>
Added:
Applied to your string/error message:
's = "The DataTable.ImportSheet operation failed. Invalid file." \n "Line (20): "DataTable.ImportSheet Environment("STPFilePath"),Environment("TestScriptName"),"Action2""."
s = "1" & vbCrLf & "2: ""3(""4""),5(""6""),""7""."
WScript.Echo s
output:
cscript 39878005.vbs
1
2: "3("4"),5("6"),"7".
You are getting a syntax error because the double quotes in your if statement are not escaped.
I'm not sure what you are trying to do here and maybe using Err.Number makes more sense but if you want to avoid the syntax error then use this code:
If Err.Description = "The DataTable.ImportSheet operation failed. Invalid file."" \n ""Line (20): ""DataTable.ImportSheet Environment(""STPFilePath""),Environment(""TestScriptName""),""Action2""." Then
''' list of instructions
End If
Also, you cannot use \n if you want to compare 2 lines in if statement. Use CHR(13) or CHR(10) instead of \n. I.e.
If Err.Description = "The DataTable.ImportSheet operation failed. Invalid file."& CHR(13) &"Line (20): ""DataTable.ImportSheet Environment(""STPFilePath""),Environment(""TestScriptName""),""Action2""." Then
'''list of instructions
End If
Again: Using Err.Number will make more sense as any extra/missing space/newline in above If condition will fail the condition.
Try to Replace the Err.Description line with this
If Err.Description = "The DataTable.ImportSheet operation failed. Invalid file." & vbCrLf & "Line (20): DataTable.ImportSheet Environment(" & STPFilePath & "),Environment(" & TestScriptName & ")," & Action2 & "." Then
Good Luck.!
Related
I have some VB6 code that needs to be migrated to VB.NET, and I wanted to inquire about this line of code, and see if there is a way to implement it in .NET
Error(Err.Number)
Keeps telling me that an Expression is expected.
If Err.Number = 53 Then
GoTo x
Else
Msg = " Error N. " & Err.Number & Chr(13) & Error(Err.Number)
MsgBox(Msg, 16, "Warning")
FileClose(1)
Exit Sub
End If
The VB6 documentation states:
The return value of the Error function corresponds to the Description
property of the Err object.
so instead of using Error(Err.Number) use Err.Description.
I have a input box that the user needs to put in some specific text. The text must be in uppercase. But when i run the code and insert the text in lower case, it still runs.
HERE:
KnownError = InputBox(PayRatesTable.ListColumns("Name").DataBodyRange(i) & " is showing "
& PayRatesTable.ListColumns("Hourly or Salary").DataBodyRange(i) & " for role " &
PayRatesTable.ListColumns("All Roles").DataBodyRange(i) & ". Please type KNOWN ERROR (All
CAPS) to
continue.")
If StrPtr(KnownError) = 0 Then
MsgBox "Process Aborted"
Exit Sub
End If
If KnownError <> "KNOWN ERROR" Then
MsgBox "Please type KNOWN ERROR (All CAPS) to continue or cancel to stop the
proccess"
GoTo HERE
End If
Any ideas on how to force the use of uppercase??
I have been figuring out how to pop up msg box if there is error. I have below code but this code does not jump to error handler if there is error and Exit the sub.
Sub Validate_Region()
Dim str_DEPARTMENT_NAME As String
On Error GoTo Reion_Error
If Left(Range("M4"), 2) = "As" Then
str_DEPARTMENT_NAME = "India"
ElseIf Left(Range("M4"), 2) = "ME" Then
str_DEPARTMENT_NAME = "Middle East"
End If
'Get value of region selected in Master File
str_regionvalue = Workbooks("Master Report").Sheets("Home").Range("T8")
If str_regionvalue = str_DEPARTMENT_NAME Then
MsgBox ("You have selected " & str_regionvalue & " region"), vbInformation
End If
Exit Sub
Reion_Error:
MsgBox ("Please select the correct Region.") & vbNewLine & vbNewLine & ("You have selected " & str_regionvalue & " region" & " In Home Sheet of Master Report and pulled the data for " & str_DEPARTMENT_NAME & " region"), vbCritical
ActiveWorkbook.Close
End Sub
I just copy/pasted your code and it works well for me:
I don't know from where this sub is called and what type of variable is used on str_regionvalue (seems like Variant)
So what can you do with this:
Get rid of Exit Sub on line #19.
Put Err.Number check in your error handler:
Reion_Error:
If Err.Number <> 0 Then
MsgBox ("Please select the correct Region.") & vbNewLine & vbNewLine & ("You have selected " & str_regionvalue & " region" & " In Home Sheet of Master Report and pulled the data for " & str_DEPARTMENT_NAME & " region"), vbCritical
'by the way, you can try to set ActiveWorkbook.Saved to True, to get rid from pop-ups on Close
ActiveWorkbook.Close
End If
Try to look for error number thru debugging (debug.print err.number or place some stop's to do this manually). Or add err.number to Watches and check Break when value changes so you can track where error raised and cleared (if raised).
Err object
P.S. You got some ridiculous MsgBoxes, if you really like parentheses, use them like this:
Call MsgBox("Please select the correct Region." & vbNewLine & vbNewLine & _
"You have selected " & str_regionvalue & " region" & " In Home Sheet of Master Report and pulled the data for " & str_DEPARTMENT_NAME & " region", vbCritical)
Under Tools >> Options in the VB Editor: on the General tab make sure you don't have "Error trapping" set to "Break on all errors"
Check your syntax, you have quite a few unnecessary brackets and commas in your msgbox statements. Try something like:
returnval = MsgBox ("Put message here" , vbInformation)
OR
MsgBox "another message " & vbnewline & "some more message", vbcritical
The code looks fine and compiles now but I think your error may be elsewhere. Possibly in:
str_regionvalue = Workbooks("Master report").Sheets("Home").Range("T8")
I'd suggest:
Leave the 'exit sub' in. It's correct.
Add 'Option Explicit' to the top of the code - it will force you to declare all variables (str_regionvalue isn't declared)
Set your Under Tools >> Options in the VB Editor: to 'Break on Unhandled Errors'.
Put in a breakpoint (Debug >> Add Watch) at the top of the code and run through it step by step using Debug >> Step Into.
I am trying to create a high-quality ErrorHandler. I get run-time error 438:
Object doesn't support this property or method
Here's code:
Sub Function_asdf()
...
On Error GoTo ErrorHandler
...
ErrorHandler:
& vbNewLine & "Error" & Err.Number & Err.line & Err.Description
Like that, you're doing nothing. You're trying to start a chain of strings, but this starts with a join operator (&) which is not connecting to any "first" part, and even if it was it's neither assigned to a variable nor shown into a dialog box.
Start by showing that into a MsgBox :
MsgBox vbNewLine & "Error" & Err.Number & Err.line & Err.Description
or assigning it to a variable first:
Dim errMsg As String
errMsg = vbNewLine & "Error" & Err.Number & Err.line & Err.Description
and then printing it:
Debug.Print errMsg
or logging it into a cell
Range("A1") = errMsg
or in a MsgBox
MsgBox errMsg
... and don't forget to specify on which line you're raising the exception (here it's clear, but it's not always evident) cause it would be impossible for someone to debug without knowing in which point of your code the exception is thrown.
EDIT - I've finally run your code
So, I've run your code and the error is raised because the object Err (i.e. the exception thrown) does not have the property Line. It might have been deprecated, or just never existed.
If you wanted to return the line on which the error occurs, use the ERL property:
MsgBox vbNewLine & "Error" & Err.Number & Erl & Err.Description
I have written an excel VBA script which refers to another open excel document for some of it's data. Recently it has come to my attention that if this secondary document is closed unexpectedly by the user, the primary script ceases to work. Obviously, I need to check to be sure it is open before I search it.
Below is the code I came up with to verify that the workbook is open. If it is, I format it. If it isn't, I open it (which triggers it's own formatting). The problem comes in because my error handler catches the "Object required" error number 424. I try to take care of that by instructing it to just resume next when this happens. Unfortunately it seems to want to pick case else rather than case 424 and stops the script.
On Error GoTo searchGridsError
GridName = Workbooks(SALTname).Sheets(2).Range("B3").value
If Verify.FirstOption.value = True Then
Set Verify.groupGrid = Workbooks(GridName)
If Verify.groupGrid Is Nothing Then
Verify.checkForGrids
Else
formatWorkbook
End If
End If
Below is my error handler:
searchGridsError:
Select Case Err
Case 18
Verify.clearData
Exit Function
Case 424
Resume Next
Case Else
MsgBox "An error has occurred while searching the customer number grid. Please try again or search manually."
Module1.ReportError Err.Number, Erl, Err.Description, "searchGrids", Verify.Address1Box & "," & Verify.Address2Box & "," & Verify.CityBox & "," & Verify.StateBox & "," & Verify.ZipBox & "," & Verify.ContractBox & "," & Verify.PBPBox & "," & Verify.CountyBox
Verify.clearData
Exit Function
End Select
End Function
Does anyone have any ideas about why this is happening? It has to be in the error handler but I have seen many, many examples that look just like mine.
As guitarthrower stated in the comments, simply putting Resume Next in your error handling does not resume your macro back where the error occurred. To do that you would need to put another placeholder after your On Error GoTo searchGridsError line like RestartHere: where you want to jump back to and then replace Resume Next with:
On Error Resume Next
GoTo RestartHere
However, this will bypass your error handling once Error 424 is encountered, so you should be wary of how it is used.
Probably a better solution would be to put your error handling right in your code where you expect the error to occur. You can leave your code mostly as-is. However, right before the line that is throwing Error 424, you add On Error Resume Next. Then after the line in question, you add the following:
If Err.Number = 424 Then
Err.Clear
End If
On Error GoTo searchGridsError