How To Improve VBA Numerical Error Handler

Mar 21, 2012

how to improve the following error handling strategy and code samples so as to maximise its usefulness for the purposes described.

During function calculations in VBA, such as is done by numerical integrators, numerical run-time errors may occur, such as:
Err.Number = 5 Invalid procedure call eg. LOG(-5)
Err.Number = 6 Overflow
Err.Number = 11 Division by zero

My present strategy to address these errors:

Say the variable x is used in the function calculations, and some particular value of x causes one of the above errors to occur. After the above errors are trapped, the x value is increased slightly from its initial value, and the failed calculation line is retried (Resume). On the next error trap, the x value is decreased slightly from its initial value, and the calculation tried again. This way, the initial x value is cyclically changed by adding increasing magnitude increments of alternating signs, searching for the nearest x value which will allow the function calculation to be done without error. Thus it causes the x value to swing around the initial x value in a pendulum-like manner, but with a widening swing per pass.

So far, I have the following code:

Code:

ErrorHandler:
If Err.Number = 5 Or Err.Number = 6 Or Err.Number = 11 Then
If Initial = True Then

[Code]....

View 5 Replies


ADVERTISEMENT

Even Handler Causes Run-time Error 5

Apr 5, 2013

I have a userform that reads data from a sheet with thousands of records (lines) each record with some 30 cells (columns). I wanted the user to scroll up & down in the sheet, using the keyboard arrows and I wanted the userform to show the relevant record, where the cursor stands. For this purpose I created an event handler (Worksheet_SelectionChange) that identifies the scrolling, populates the form with the relevant record data and then returns the focus back to the sheet. The command I have used to send focus back to the sheet (so the user can keep scrolling) is:

VB:
AppActivate "Microsoft Excel" & " - " & ThisWorkbook.Name

Now comes the bizarre part... This macro can work perfectly for hours and then decide to "die", or can work on one computer but not the other (all with Excel 2007, BTW). When the macro dies (or if it does not work, to begin with), the debugger points to the command I mentioned, with an error message "Run-time error '5' , Invalid procedure all or argument."

how to make my macro "stable" (make it work always and on every computer)?

Unfortunately, I cannot upload the file, because it contains confidential data (and it is not in English...).

VB:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
lr = Cells(Rows.Count, 1).End(xlUp).Row
If Target.Column < 2 And Target.Row > 1 And Target.Row <= lr Then
crow = ActiveCell.Row

[Code]....

View 5 Replies View Related

Reset Error Handler

Sep 21, 2007

I have a piece of code with some error handlers in it but for some reason the error handler can't deal with a second error. The example code below suffers from the same problem in that it falls over when it tries to process the i = "w" line for the second time. If you F8 through the code the error handler works the first time but not the second. Why is this and what can I do to get it to keep going to erH when it hits the i = "w" line?

Sub test()
Dim i As Integer
testing:
On Error GoTo erH
i = "w"
erH:
i = 1
GoTo testing
End Sub

View 9 Replies View Related

Error Handler Runs Without Error

Oct 3, 2008

I have written a long bit of code, which works fine. I decided to add an error handler just in case of any errors. I have run through my code numerous times to confirm that there is no error, but the msgbox still pops up.

View 3 Replies View Related

Error Message / Handler For Calendar

Oct 11, 2009

From my userform I use a calendar to input in two text boxes the "from" and the "to" dates and get a "custom" statement of activity by company.

If I input two dates that do not exist within my summary of sales how can I code in order to get an error message AND clear the text boxes. (textbox1&2)

Example: jan 07 to Dec07 do not exist/ Oct 09 does
"from" and the "to" dates = 01/01/2007 to 12/10/2010 would work (and it does)
01/07/2007 to 12/06/2007 does not exist anywhere then I want to see a message box to that effect AND the textboxes cleared for input of the new "from" date in textbox1
If Impossible at least can you help on an error handler? (on error resume does not work or at least I think it does not)

View 9 Replies View Related

Resume VBA Code From Error Handler

Oct 23, 2008

Before doing some modifications in a data sheet, I first check with the below code whether a certain list is complete or not using the VLOOKUP function. If I encounter a missing value in the list, a userform appears asking to fill out 3 data fields, which are then added to the original list to complete it. All of this works fine if only one line is missing; if there are two (or more) lines missing, all goes well untill the VLOOKUP function errors out on the second line missing.
I can't find out why everythign goes welll with the first missing line, but not with the second missing line.

Sub proCheckVesselCodes()
Sheets("Sheet4").Select
Range("K1").Value = "Check"
Set varRange1 = Range("A1")
fctCountNrRows
varRow1 = varRow
varRow2 = 2

jump:
On Error Goto addvessel...............

View 3 Replies View Related

Improve Error Handling (give A Short Explanation To The User)

Feb 18, 2007

Improve Error Handling...

i would like to improve my error handling efforts (such as give a short explanation to the user, etc.)

Private Sub btn_SaveFile() ...

View 4 Replies View Related

Create Error Handler For Entire Workbook?

Mar 15, 2014

Is it possible to create an Error Handler for the entire workbook, or do you have to put one on every sub routine?

View 1 Replies View Related

Finding File: Catch Error Handler Out On The Web

Jan 19, 2009

I can not seem to find a good example of code for a try and catch error handler out on the web for something I am working on. Here is the code I am working with:

View 2 Replies View Related

Error Handler For Finding Values On Each Worksheet

Dec 11, 2007

I am writing a macro to track stats in a workbook. The workbook has the check every tab in the given workbook, hidden and unhidden. In order to track the stats I find a column labeled "Read Dates" on each worksheet. The "Read Dates" column is not always in the same location so I have the macro Find the words "Read Dates". To make sure the cell the macro finds is the column label and not just another cell where someone may have used the words "Read Dates", I have an 'If/Then' statement checking the column to the left to ensure "Rev Mo" is there. If the state is true, the macro begins tracking stats. If the statement is false it attempts to find the next cell containing "Read Dates".

My problem occurs when the macro selects a sheet that contains no data or does not have the words "Read Dates". To help remedy this, I have an Errhandler that simply tells the macro to move onto the next worksheet. My problem is that there are several worksheets that do not have a cell with the value of "Read Date" so the second time the ErrHandler errors. Below is the section of the code I currently have an issue with.

Sub Begin_tracking()

'Error Handler
On Error Goto ErrHandler

WCount = Worksheets.Count
For i = 1 To WCount
If Worksheets(WCount - i + 1).Visible Then
Worksheets(WCount - i + 1).Select

Can anyone think of a better way of doing this so the entire workbook is worked or tell me how I can reset the Error Handler is it will move onto the next worksheet? I've looked into help on this forum to rest the ErrHandler but what I've found and tried has not worked. The ErrHandler may be invoked 2 times or it may be invoked 20 times depending on the workbook.

View 8 Replies View Related

Error Handler For Incorrect Password (Protected Sheet)

Feb 1, 2007

For some reason I thought this would be simple, but not as simple as I thought. I have a UserForm that enters data into a sheet, but before it is entered, the user is prompted for the sheet password (sheet is protected). What I tried to do is use an error handler to exit the sub if the password is incorrect, user hits Cancel, etc. Here is the code I am using:

View 10 Replies View Related

Error Handler's: To Error Or Not To Error

May 11, 2006

What I'm wondering is when it is a good idea to use Error Handler's. I've got maybe 50 procedures in the project I'm working on. Some of the procedures are merely parent procedures calling other procedures. Since other people will be using this workbook, I want there to be a way to troubleshoot if errors occur, but it almost seems bad form to have an Error Handler for EVERY single procedure. Conversely, somehow, someone always finds a new and creative way to error out my workbook, and they don't understand the code and/or are not VBA literate. I can always fix afterwards, but solving the problem immediately over the phone is not ideal. I'm very new to debugging for other users. The workbook is currently in constant development/improvement used by only a handfull of users, but eventually it will need to be at some decent release stage, where the VBA code will be password protected, and I will not want the user getting into the code.

View 2 Replies View Related

How To Improve The Speed Of A Macro

Mar 2, 2010

I wrote a macro that works fine, although it runs extremely slowly as if plodding along through all of the cells one at a time. I'm sure the computer is faster than that, so I would like it to chug through more quickly.

I am using some syntax that could be optimized ....

View 9 Replies View Related

Background Colours To Improve Readability?

Dec 24, 2013

Do any of the templates/backgrounds offer the user a white background with every fifth row and column an off-white colour?

My document will be read by LOADS of people, so I want to be able to follow my eye across. A soft system like that lets someone scroll across twenty columns and see they're still on the same row. It's like writing 7 Oct 2013 instead of 07/10/2013. It makes sense whether you use UK dd/mm/yyyy or US mm/dd/yyyy. People say 'October' not 'month no 10.' That's my logic and design principle.

View 3 Replies View Related

Improve Quality Of Exported Chart

Oct 20, 2008

I was working with automatically exporting the contents of a textbox to an image file and found this post which I was able to modify slightly for my purposes.

http://www.excelforum.com/excel-prog...ml#post1982405

Is it possible to improve the quality of the output image. I need to print out the resulting image and the quality of the image this method creates is too poor for my purposes.

View 7 Replies View Related

Improve Code To Copy Data

Apr 7, 2009

Attached is a spreadsheet that does exactly what I need it to do but I know that the code can be written better. Perhaps some experts can clue me in to how I could improve.

There is a button on the sheet Mandatory Training that looks at the sheet CS CM and finds certain numbers, when it does it makes them red and copies them to the Mandatory Training sheet. If other lines are added to CS CM that have the same numbers and the button is clicked again, only the new entries are made red and copied to Mandatory Training.

View 10 Replies View Related

Macro To Improve Print View

Jul 16, 2009

I am generating the chart from the following code but when it prints i.e (Mode#1 or 2 or 3 etc....) with type i.e. (GL_Weld or Bend or any other text). Printing as i.e. (Mode#1Bend or Mode#2GL_Weld). I would like some space in between (Mode#1 Bend) or (Mode#2 GL_Weld) or even put it in next line underneath it ( Mode#1 Bend).
How do i incorporate and space or enter next line? Code as follows:

View 3 Replies View Related

Improve Macro To Move Row To Column?

Nov 25, 2013

i have this macro from this webiste to move every 2nd cell to the row above...(ie B1---after macro---becomes A2) I want this macro to be able to do exactly the same but instead of having a cell, it will have a row...

Code:
Sub moveDefs()
Dim i, LastRowFrom As Integer
'Identify the last row of the rule set that contains data

[Code]....

View 4 Replies View Related

Improve Loop, Copy Speed

Aug 18, 2007

I have a large worksheet (Sheet1) containing approximately 15,000 records (15,000 rows x 21 columns). I need to search through all these records, and manually decide whether a record should be copied onto another worksheet (Sheet2) or not. The code I have written works very well (as far as I can tell), but it is extremely slow (the searching takes forever).

The code is as follows:

Sub SearchAndCopy()
Dim SearchRow As Long
Dim LastRow As Long
Dim SearchColumn As Long
Dim LastColumn As Long
Dim CopyToRow As Long
Dim SearchString As String
Dim Found As Boolean
Dim Response As Integer
On Error Goto Err_Execute
Found = False
'Fetch Search Term
Sheets("Sheet2").Select
SearchString = LCase( Range("F3").Text)......................

View 3 Replies View Related

How To Reduce Formula Size/improve Efficiency

Sep 1, 2009

how to reduce formula size/improve excel efficiency?

I have a really complicated formula with lots of COUNTAs, VLOOKUPs, IFs, etc etc etc which returns a variety of values, ZEROs (for blank cells being referenced), and #NAs.

I would like to turn all ZEROs and #NAs into ""s without repeating this super long formula over and over.

I am curently using:

=IF(or(ISBLANK([superlongformula]),ISNA([superlongformula])),"",[superlongformula])

View 9 Replies View Related

Status Formula (find Out The Formula Where Numerical Valued Between 1-2000 Equals True And Numerical Under 0 Equals False)

Dec 12, 2009

I'd like to find out the formula where numerical valued between 1-2000 equals True and numerical under 0 equals False. Examples are in the attachment.

View 3 Replies View Related

Does SSD Hard Disk Or A Faster Chip Improve Excel Performance

Apr 26, 2013

If I want to improve my excel performance like ability to open multiple excel and sheets to perform calculation, should I change to a SSD or a faster intel chip? My current PC sometimes give me a black screen for my excel sheet or is unable to save certain sheets when I open too many sheets.

View 3 Replies View Related

Clear Event Handler Code

Dec 23, 2009

I wonder if there is a way to clear event handler code in a userform programmatically?
Haven't quite managed it yet.

Something like:


dim x as integer
With ThisForm.CodeModule
x = .CountOfLines
For 1 to x
.line = ""
Next x

End With

View 9 Replies View Related

Control Object Event Handler

Aug 22, 2006

Is there an Event Handler that runs a macro whenever any control object in a worksheet has its state changed or whenever any checkbox is checked/unchecked. I want to have a method handle this outside of the checkbox since the number of checkboxes are dynamic and should not have to be individually coded by the user.

View 5 Replies View Related

Event Handler For Multiple CommandButtons

Oct 26, 2007

I want to create an event handler for multiple buttons. I know that it can be done in VB but I'm not sure about VBA. Auto Merged Post;bump* Auto Merged Post;bump*

View 4 Replies View Related

Changing A Workbook Event Handler Via Code

Feb 5, 2007

I have a workbook, that when opened the first time needs to prompt the user to save it. I got that working with no issues by using

Private Sub Workbook_Open()
SaveOnOpen
End Sub

where SaveOnOpen is a procedure in another module. What I would like to do now is re-assign the Workbook_Open sub to be set to null, so that it doesn't run any more. Is it possible to somehow assign Workbook_Open to call a null procedure, as opposed to setting up an onTime call to delete the code itself?

View 9 Replies View Related

Simultaneous XMLHTTP Posts W/Event Handler

Jul 12, 2006

I have a standard module in which I gather information from a workbook, create an XML document, Post it, and collect the value I need from the response XML. Thing is I have to run through it maybe hundreds or thousands of times depending on the number of records I have. It takes ten years to finish the loop. I have read that it might be possible to post them concurrently using a class module and an event handler, but I have not worked with Class modules before. Compiling all the XML documents into memory is easy, posting them and getting my return values in a timely manner is the problem at hand.

View 2 Replies View Related

Event Handler Attached To Runtime Created Object

Jul 21, 2006

I'm trying to attach Worksheet_Change to a worksheet created at runtime. Usually you put it in the code window of the Sheet object but what do you do when you create the worksheet at runtime?

View 9 Replies View Related

Event-handler Subroutine To Transfer Checkbox Text To Cell

Mar 17, 2007

to save typing the same things over and over I have created a dialog box with checkboxes, named with several common terms we use when writing an invoice. i.e. dig a hole, paint a fence etc. I have assigned the dialog box to a button on the worksheet.

When I check the checkboxes, I want the text to go to a blank section of the invoice one underneath the other. The reading I have done suggests this is an event-handler subroutine, I just don't know enough about VBA yet to be able to write the code.

View 9 Replies View Related

Improve Coding For Copying Existing Workbook To New Workbook

Sep 18, 2006

i came up with this..

Sub Copy_To_New_Workbook()

Application. ScreenUpdating = False

Dim strFileName As String
strFileName = Application. GetOpenFilename(filefilter:="Excel Files (*.xls), *.xls", Title:="Please select the excel file to be copied")

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved