Cleaning up records

jwburritt

New Member
Joined
May 22, 2019
Messages
49
Office Version
  1. 365
Platform
  1. Windows
Hello: I have part of a macro that needs to clean up cells and replace the bad data with the good data. I'm using a loop nested if else, but is there a more efficient way? This looks cumbersome. Any help would be appreciated . Thank you!

Code:
Sub cleandata()    

    Dim i As Long
    Dim DataValue As Variant
    Dim LastRow As Long
    Dim StartRow As Long

    StartRow = 7
    LastRow = Cells(StartRow, 6).End(xlDown).Row

    For i = StartRow To LastRow

    DataValue = Cells(i, 6).Value

        If InStr(1, Cells(i, 6), "Apples Today") = 1 Then
            Cells(i, 6) = "Apple"
            Else
            If InStr(1, Cells(i, 6), "Oranges Not Good") = 1 Then
                Cells(i, 6) = "Oranges"
                Else
                If InStr(1, Cells(i, 6), "Blue") = 1 Then
                    Cells(i, 6) = "Blueberries"
                    Else
                    If InStr(1, Cells(i, 6), "Apples Tomorrow") = 1 Then
                        Cells(i, 6) = "Apples plus 2"
                        Else
                        If InStr(1, Cells(i, 6), "Fruit") = 1 Then
                            Cells(i, 6) = "Fruit Stock"
                            Else
                            If InStr(1, Cells(i, 6), "Bananas") = 1 Then
                                Cells(i, 6) = "Ban"
                                Else
                            End If
                        End If
                    End If
                End If
            End If
        End If
    Next i

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Make a separate sheet that has all the ones you want to translate. Like column A would have all the bad data like "Apples Today" and "Oranges Not Good" etc etc. Then column B would have the "corrected" versions. Then just loop through your sheet and lookup each value in your new Column A of bad values. And if it matches then change it to what's in column B. Does that make sense?
 
Upvote 0
Thank you. I thought of that earlier, but the sheet I'm manipulating the raw data in is only temporary. Once the data is cleaned and formulated properly, I'll copy all the data to the target sheet and close the raw data file without saving. But that's also when my exception list was one or two. Now, it's five or so, and may get bigger. Plus, I didn't want to maintain another sheet (ugh). But, it looks like your idea is probably the best -- and unavoidable.

Thanks again!
 
Upvote 0
Another option would be
Code:
Sub jwburrit()
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array("Apples Today*", "Apple", "Oranges Not Good*", "Oranges", "Blue*", "Blueberries")
   For i = 0 To UBound(Ary) Step 2
      Range("A:A").Replace Ary(i), Ary(i + 1), xlWhole, , False, , False, False
   Next i
End Sub
Just add the rest of the values to the array. Although if the values are likely to change, I would use a list on a sheet somewhere as suggested by svendiamond.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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