Fixing errors with userform while macro running?

Lil2606

Board Regular
Joined
Jan 29, 2019
Messages
79
Hi all,

Question: Is it possible to set up an inputbox that would fix problems if the macro runs into an error?

I have Countries in my DataTable and I have Countries in the LookUpTable, with a colour assigned to the Countries, and I have a code, which loops through each row in the DataTable, and tells me if the Country is "RED" or "Not RED". The only problem is, that in the DataTable, I have blanks, and some users input city names instead of countries, or abbreviations.

So would it be possible, to fix it while my macro is running, with a userform? It hopefully would be able to do 2 things:

First example: Macro runs into Spain, which is not in my LookUpTable, and I would like the userform to pop up saying:

"Spain is not in the LookUpTable, do you want to change the cell in the DataTable?" Corrected country name: (Inputbox here) YES / NO

If the user inputs something in the Inputbox and clicks yes, then change the cell value to the inputbox value, and resume the macro checking if the country name that has just been put in is "RED" or "Not RED"

If the user clicks no, then stop the macro, so the user can go back to the LookUpTable, and add the country that caused the error, to the LookUpTable.

For the blanks the same userform should pop up and the user can decide to input a country name, or just write "BLANK" which is in the LookUpTable.

This is a VBA project, and I need to solve it with VBA, so please don't waste your own time solving the problem with formulas!

On ExcelForum I posted this question with an example workbook link:
https://www.excelforum.com/excel-programming-vba-macros/1265455-fixing-errors-with-userform-while-macro-running-with-example.html#post5069775

Code in the example:

Code:
Sub Error_correct_macro()

Dim i As Long
Dim CLUV As String
Dim CLUR As String


For i = 1 To Sheet2.ListObjects("DataTable").ListRows.Count


CLUV = Sheet2.ListObjects("DataTable").ListRows(i).Range(1, Sheet2.ListObjects("DataTable").ListColumns("Countries").Range.Column)


CLUR = WorksheetFunction.VLookup(CLUV, Sheet1.ListObjects("LookUpTable").DataBodyRange, 2, False)


If CLUR = "RED" Then


MsgBox "RED", vbOKOnly


Else


MsgBox "Not RED", vbOKOnly


End If


Next


End Sub

Could someone help me with this please?
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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