If Error vs. On Error

AndyTampa

Board Regular
Joined
Aug 14, 2011
Messages
186
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
This macro is supposed to refresh a worksheet that already exists. I'm trying to error check before deleting the existing data and replacing it with new data.

I'm creating a new temporary sheet and scraping a table from a webpage. If the scrape fails, I want to delete the new sheet and display a message box., and not continue. I tried an IF Error, but it always errored. I can't find how to do "On Error, do this and this and this."

If there is no error, I want to delete the existing worksheet and rename the new worksheet. I'm familiar with adding/deleting/renaming worksheets, but it's the error handling I'm having difficulty with.

The other thing is whether there is an error or not, once it's done with the related actions, I need to end the macro. Putting EndIf in the Then or Else part of an If loop caused an issue when I tried it.
 
Normally you can remove any properties that you didn't actually set when recording. However, I do recommend that people understand what they are doing before they mess with something.

It won't hurt anything to leave it. It just clogs up the code and uselessly sets defaults.
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I'm having another problem with errors and I don't know how to get around it. It has to do with the value inside the cell.

My macro runs through each line and tests each cell to validate it. Every test seems to be working except one. For all other tests, an invalid entry is highlighted and a counter is incremented. After all tests, there is a final test which looks for blank cells and highlights them gray.

VBA Code:
    For Each Cell In Range("A" & RNum & ": E" & RNum)
        If Cell.Value = "" Then
        Cell.Interior.ColorIndex = 15
        BLKErr = BLKErr + 1
        End If
    Next Cell

This will actually re-highlight the blank cell to gray after the invalid entry counter is incremented. However, the macro breaks if there is an error in a cell. So if someone puts -Abc in a cell that is supposed to be a number or a date, the result in the cell is #NAME?. Since that is an invalid entry, my macro highlights it pink and increments the counter but then when it gets to this code, the above macro keeps crashing with Run-time error '13' Type Mismatch. I tried
VBA Code:
On Error Resume Next
but that just allowed the cell to be changed to gray incorrectly.

What test would I use that would see these errors as the non-blank cells they are?
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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