I have a worksheet with data that goes up to 20,000 rows and 60 columns depending on the amount of data entries for the period. The entries for a particular column (column W) were not properly entered and therefore some of the data that should appear under column W appear under other columns but the same row.The data in column W should have only 2 characters; the first character should be a number and the second character should be an alphabet(eg 2B, 5A, 3H, 4F). For instance on one row instead of 2B appearing on Column W it would appear on the same row but under Column Z . In another row instead of , say, 5A,appearing on column W it would appear on say, column T or V or X but the same row. The rows are correct the problem is the data not appearing on column W. There can be only one correct entry for column W on each row. Also note that some of the entries in cloumn W are correct for some rows. Finally note that the list of entries for Column W are pre-defined and is listed in a range that the user can provide. I wrote the macro below to loop through all the cells matching the content with a range that the user will provide where the pre-defined data for column column W is . Where it finds a match it copies the content to a new range column "BK" on the same row. On the other hand where there is no correct entry on that row then it puts "NULL" in column "BK" for that row. However the macro doesn't seem to work; excel hangs for several minutes and nothing is produced. what am I doing wrong? I need help.
Code:
Sub FindOutletType()
Dim MasterList As Range
Dim I As Integer
Dim J As Integer
Dim FinalRow As Long
Dim FoundRow As Variant
On Error Resume Next
'get the ranges from the user.
'The above on error handles cancel being selected
Set MasterList = Application.InputBox( _
prompt:="Select the range to look for type of outlet mapping",Type:=8)
'if no range supplied, exit macro
If MasterList Is Nothing Then End
'restrict the ranges to the used range on the sheet in case entire columns selected above
Set MasterList = Intersect(MasterList, ActiveSheet.UsedRange)
'rotate through each cell and see if it is in the first range
FinalRow = Cells(65536, 1).End(xlUp).Row
For I = 2 To FinalRow
For J = 1 To 55
'check only non-blank cells
If Application.Trim(Cells(I, J))<> "" Then
'reset Err for each loop
Err = 0
'use the match function to see if there is a match. Using a value
'of Zero for the last argument means that an exact match is required
FoundRow = Application.Match(Cells(I, J).Value, MasterList, 0)
'If a match is found Err stays zero; Copy the cell in that case
If Err = 0 Then
Cells(I, J).Copy Destination:=Cells(I, "BK")
End If
End If
Next J
FoundRow = Application.Match(Cells(I, "BB").Value, MasterList, 0)
If Err <> 0 Then
Cells(I, "BK").Value = "NULL"
End If
Next I