Tidy up columns

Sharid

Well-known Member
Joined
Apr 22, 2007
Messages
1,047
Office Version
  1. 2016
Platform
  1. Windows
I want to clean up some columns with Vba. I currently use this and it works fine more the most part, if the columns has text and numbers then it does not remove the item.

VBA Code:
 Dim lastrow As Integer, i As Integer
            Set wb = ThisWorkbook
                With wb.Sheets("Data")
                lastrow = .Cells(.Rows.Count, "c").End(xlUp).Row
                    For i = 2 To lastrow
      '''phone numbers
            If Not (Sheet9.Range("c" & i).Value Like "*#*#*#*#*") Then Sheet9.Range("c" & i).Value = "-"
            If Not (Sheet9.Range("d" & i).Value Like "*#*#*#*#*") Then Sheet9.Range("d" & i).Value = "-"
            If Not (Sheet9.Range("e" & i).Value Like "*#*#*#*#*") Then Sheet9.Range("e" & i).Value = "-"
            
            If Not (Sheet9.Range("c" & i).Value Like "**#") Then Sheet9.Range("c" & i).Value = "-"
            If Not (Sheet9.Range("d" & i).Value Like "**#") Then Sheet9.Range("d" & i).Value = "-"
            If Not (Sheet9.Range("e" & i).Value Like "**#") Then Sheet9.Range("e" & i).Value = "-"

         .Range("C" & i).Value = Replace(.Range("C" & i).Value, "Call", "")
         .Range("D" & i).Value = Replace(.Range("D" & i).Value, "Call", "")
         .Range("E" & i).Value = Replace(.Range("E" & i).Value, "Call", "")
     Next i
    End With

Results in red are not removing, all the phone numbers have the word "Call" which i can remove, however the other items have too many variables. I will not be able to work each one out the same as removing the word "Call"

PS. Some phone numbers have a "+" at the front e.g. +111222333

1614347810319.png
 

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,267
Office Version
  1. 2010
Platform
  1. Windows
Fixed "0" problem :)

VBA Code:
Sub CleanUp()
    'Copies phone numbers to new column 4 to the right for testing purposes
    Dim lastrow As Integer, Row As Integer, WB As Worksheet
    Dim Col As Long, Temp As String, Row2 As Long
    Set WB = Sheets("Data")
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    With WB
        .Range("G:I").NumberFormat = "@"
        For Col = 3 To 5
            lastrow = .Cells(.Rows.Count, Col).End(xlUp).Row
            For Row = 2 To lastrow
                Temp = .Cells(Row, Col)
                Temp = Replace(Temp, " ", "")
                For Row2 = 1 To Len(Temp) - 9
                    If Val(Mid(Temp, Row2)) > 1000000 Then
                        If Left(Mid(Temp, Row2), 1) <> "0" Then
                            .Cells(Row, Col + 4) = CStr(Val(Mid(Temp, Row2)))
                        Else
                            .Cells(Row, Col + 4) = "0" & CStr(Val(Mid(Temp, Row2)))
                        End If
                        Exit For
                    End If
                Next Row2
            Next Row
        Next Col
    End With
                    
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub
 

Some videos you may like

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Watch MrExcel Video

Forum statistics

Threads
1,127,326
Messages
5,624,018
Members
416,006
Latest member
PCaffrey

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
Top