Macro to REMOVE unwanted data from a cell...?

Experts,

I need a macro that will REMOVE all NON NUMERIC data from Column A of my worksheet.

Some of the cells are NUMBERS, some are BLANK, others have dashes  "--", others have words...etc... I need the column to ONLY contain / KEEP the NUMBER fields.

I need another macro after I acheive the above, but I'm starting with just this one,...thanks
LVL 17
MIKESoftware Solutions ConsultantAsked:
Who is Participating?
 
SiddharthRoutCommented:
I think your macros are disabled.

Anyways here is the file.

Sid
CRXIuser2005-Datafile-2.xlsm
0
 
MIKESoftware Solutions ConsultantAuthor Commented:
Not just a cell, but for the ENTIRE COLUMN A....
0
 
SiddharthRoutCommented:
Try this. Please replace the Sheet names and column names accordingly.

Dim i As Long
Sub Sample()
    Dim lastrow As Long
    
    lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 1 To lastrow
        Sheets("Sheet1").Range("A" & i).Value = OnlyNumbers(Sheets("Sheet1").Range("A" & i).Value)
    Next
End Sub

Function OnlyNumbers(strInput As String) As String
    Dim strChar As String, strOutput As String
    
    strOutput = ""

    For i = 1 To Len(strInput)
        strChar = Mid(strInput, i, 1)
        If (IsNumeric(strChar)) Then
            strOutput = strOutput & strChar
        End If
    Next i
    OnlyNumbers = strOutput
End Function

Open in new window


Sid
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
StephenJRCommented:
Perhaps this?
Sub x()

With Columns(1)
    On Error Resume Next
    .SpecialCells(xlCellTypeConstants, xlTextValues).Delete shift:=xlUp
    .SpecialCells(xlCellTypeBlanks).Delete shift:=xlUp
    On Error GoTo 0
End With

End Sub

Open in new window

0
 
SiddharthRoutCommented:
CRXIuser2005: After looking at stephen's suggestion, I think I might have misunderstood your query.

Do you want to remove values like abc123abf or do you want to just keep the number from that string and ignore the rest?

Sid
0
 
patrickabCommented:
CRXIuser2005,

Please try the macro in the attached file.

Just paste your data into column A and press the button.

Patrick
Sub specialmacro()
Dim rng As Range
Dim celle As Range
Dim i As Long
Dim str1 As String

With Sheets("Sheet1")
    Set rng = Range(.Cells(1, "A"), .Cells(.Rows.Count, "A").End(xlUp))
End With

For Each celle In rng
    For i = 1 To Len(celle)
        Select Case Asc(Mid(celle, i, 1))
            Case 48 To 57
                str1 = str1 & Chr(Asc(Mid(celle, i, 1)))
            Case Else
        End Select
    Next i
    celle = str1
    str1 = ""
Next celle

End Sub

Open in new window

CRXIuser2005-01.xls
0
 
StephenJRCommented:
Sid - I might have misunderstood!
0
 
SiddharthRoutCommented:
No Stephen. I reread the question and I have a feeling (And it is growing every second) that I have misunderstood :)

Sid
0
 
byundtCommented:
Are some of the cells that contain numbers actually text that looks like numbers? If so, you may want to convert them to numbers before clearing them. The following macro uses the SpecialCells method (like StephenJR), but indicates by comments statements that convert text into numbers, clear non-numeric cells, delete non-numeric cells, and delete blank cells. The macro is restricted to the used range because it would otherwise take a long time in Excel 2007 or later to go through every cell in column A.
Sub DeleteEverythingButNumbers()
Application.ScreenUpdating = False                                      'Removes the screen flicker when macro runs
With Intersect(Columns(1), ActiveSheet.UsedRange)
    On Error Resume Next
    Cells(Rows.Count, Columns.Count).Copy
    .PasteSpecial Operation:=xlPasteSpecialOperationAdd         'Converts text that looks like numbers into numbers
    .SpecialCells(xlCellTypeConstants, 22).ClearContents        'Clear all non-numeric cells
    .SpecialCells(xlCellTypeConstants, 22).Delete shift:=xlUp   'Delete all non-numeric cells
    .SpecialCells(xlCellTypeBlanks).Delete shift:=xlUp          'Delete blank cells
    On Error GoTo 0
End With
Application.ScreenUpdating = True
End Sub

Open in new window


Brad
0
 
MIKESoftware Solutions ConsultantAuthor Commented:
I only want to keep cell data that contains a SINGLE NUMERIC VALUE:

Keep:
1234564
651316
681211

Remove:
sum
trans
any word
any words etc...
yesno1234
---
%this too

I need to achieve a BLANK for the cell data removed per above...so that ONLY numbers remain. So end result should be cells with NUMBERS and BLANKS....

THANKS


0
 
byundtCommented:
I deleted the two statements that deleted the blanks from my macro.
Sub DeleteEverythingButNumbers()
Application.ScreenUpdating = False                                      'Removes the screen flicker when macro runs
With Intersect(Columns(1), ActiveSheet.UsedRange)
    On Error Resume Next
    Cells(Rows.Count, Columns.Count).Copy
    .PasteSpecial Operation:=xlPasteSpecialOperationAdd         'Converts text that looks like numbers into numbers
    .SpecialCells(xlCellTypeConstants, 22).ClearContents        'Clear all non-numeric cells
    On Error GoTo 0
End With
Application.ScreenUpdating = True
End Sub

Open in new window

0
 
MIKESoftware Solutions ConsultantAuthor Commented:
PatrickAB:

I tried your sheet and it worked for a few cells then crap'd out. I think it crap'd on this:

=-SERV

Which is showing up as a #NAME? in the actual cell.....

I think you are close....thanks
0
 
StephenJRCommented:
That's not what you said originally. Delete line 6 from the code I posted.
0
 
MIKESoftware Solutions ConsultantAuthor Commented:
I need a refresher course on "how" to add macro to Excel 2007...?
0
 
SiddharthRoutCommented:
Simplest way. From the Sheet, Press Alt + F11 and then insert a module and write the macro.

Sid
0
 
SiddharthRoutCommented:
0
 
SiddharthRoutCommented:
Also try this code

Dim i As Long, j As Long
Sub Sample()
    Dim lastrow As Long
    
    lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    For j = 1 To lastrow
        If OnlyNumbers(Sheets("Sheet1").Range("A" & j).Value) = False Then _
        Sheets("Sheet1").Range("A" & j).ClearContents
    Next
End Sub

Function OnlyNumbers(strInput As String) As Boolean
    Dim strChar As String, strOutput As String
    
    OnlyNumbers = True
    strOutput = ""

    For i = 1 To Len(strInput)
        strChar = Mid(strInput, i, 1)
        If Not (IsNumeric(strChar)) Then
            OnlyNumbers = False
            Exit Function
        End If
    Next i
End Function

Open in new window


Sid
0
 
patrickabCommented:
CRXIuser2005,

Please upload your file.

Patrick
0
 
MIKESoftware Solutions ConsultantAuthor Commented:
Here you go..thanks for help

CRXIuser2005-Datafile.xlsx
0
 
SiddharthRoutCommented:
Here is the updated file.

Sid
CRXIuser2005-Datafile-2.xlsm
0
 
SiddharthRoutCommented:
To run the macro Click on the Developer tab. and follow what the screen shot says.

Sid
Untitled.jpg
0
 
MIKESoftware Solutions ConsultantAuthor Commented:
Can't get it to work...if you have it working,...can you please just run it...then copy/paste VALUES into another column and post back to me...????
0
 
MIKESoftware Solutions ConsultantAuthor Commented:
Thanks thats exactly what i needed...now I'm posting another Macro question under different thread...related to this same columne of data....
0
 
SiddharthRoutCommented:
Do paste the link of that thread here.

Sid
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.