Results 1 to 6 of 6

VBA Code to optimize and clean data

Threaded View

  1. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA Code to optimize and clean some unwanted data

    Hello JoaoVr ,

    Welcome to the Forum!

    I have attached an example workbook with the macro shown here. The macro is run from a button added to the worksheet. The only column that has special formatting is column "E" which has been formatted as text to allow for the leading zero or zeroes in the Code number.

    The following checks are made. The Email address must contain at least 1 character in the domain part , an at sign ("@") separator, 1 character in the followed by at least 1 period followed at least 1 character in the local part. Also, no spaces are allowed in the Email address. The macro is not designed to check for a valid email address according to RFC syntax rules.

    Some adjustments may be needed to adapt the code to your workbook. If you have any problems, let me know.
    ' Thread:  http://www.excelforum.com/excel-programming/820537-vba-code-to-optimize-and-clean-some-unwanted-data.html
    ' Poster:  JoaoVr
    ' Written: March 20, 2012
    ' Author:  Leith Ross
    
    Sub CheckData()
    
        Dim Code As Variant
        Dim Data As Variant
        Dim Email As String
        Dim I As Long
        Dim Matches As Collection
        Dim Phone As Variant
        Dim Rng As Range
        Dim Text As String
        Dim Wks As Worksheet
        
            Set Wks = Worksheets("Sheet1")
            
                Set Rng = Wks.Range("A1").CurrentRegion.Offset(1, 0)
                Set Rng = Rng.Resize(RowSize:=Rng.Rows.Count - 1)
                 
                Data = Rng.Value
                
                Application.ScreenUpdating = False
                Application.Calculation = xlCalculationManual
                
                  ' Check Email, Phone, and Code
                    For I = 1 To UBound(Data, 1)
                        Email = Data(I, 3)
                            If (Not Email Like "?*@*?.*?") Or InStr(1, Email, " ") Then
                                Rng.Item(I, 3).Font.Color = vbGreen
                                Rng.Item(I, 7) = "X"
                            End If
                        Phone = CStr(Data(I, 4))    ' 9 digits
                            If Not Phone Like "#########" Then
                                Rng.Item(I, 4).Font.Color = vbGreen
                                Rng.Item(I, 7) = "X"
                            End If
                        Code = Data(I, 5)           ' 3 Digits
                            If Not Code Like "###" Then
                                Rng.Item(I, 5).Font.Color = vbGreen
                                Rng.Item(I, 7) = "X"
                            End If
                    Next I
                    
                    Set Matches = New Collection
                  
                  ' Check for Duplicate rows with no problems
                    For I = 1 To UBound(Data, 1)
                        If Rng.Item(I, 7) <> "X" Then
                            Text = Trim(LCase(Data(I, 1) & Data(I, 2) & Data(I, 3) & Data(I, 4) & Data(I, 5)))
                            On Error Resume Next
                                Matches.Add I, Text
                                If Err <> 0 Then
                                    Rng.Rows(I).Font.Color = vbRed
                                    Rng.Rows(Matches(Text)).Font.Color = vbRed
                                    Rng.Item(I, 7) = "X"
                                End If
                            On Error GoTo 0
                        End If
                    Next I
                    
                Application.Calculation = xlCalculationAutomatic
                Application.ScreenUpdating = True
                
    End Sub
    [code]
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1