Solved

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

Posted on 2011-02-10
24
593 Views
Last Modified: 2013-11-05
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
0
Comment
Question by:MIKE
  • 10
  • 7
  • 3
  • +2
24 Comments
 
LVL 17

Author Comment

by:MIKE
Comment Utility
Not just a cell, but for the ENTIRE COLUMN A....
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
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
 
LVL 24

Expert Comment

by:StephenJR
Comment Utility
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
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
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
 
LVL 45

Expert Comment

by:patrickab
Comment Utility
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
 
LVL 24

Expert Comment

by:StephenJR
Comment Utility
Sid - I might have misunderstood!
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
No Stephen. I reread the question and I have a feeling (And it is growing every second) that I have misunderstood :)

Sid
0
 
LVL 80

Expert Comment

by:byundt
Comment Utility
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
 
LVL 17

Author Comment

by:MIKE
Comment Utility
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
 
LVL 80

Expert Comment

by:byundt
Comment Utility
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
 
LVL 17

Author Comment

by:MIKE
Comment Utility
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
 
LVL 24

Expert Comment

by:StephenJR
Comment Utility
That's not what you said originally. Delete line 6 from the code I posted.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Author Comment

by:MIKE
Comment Utility
I need a refresher course on "how" to add macro to Excel 2007...?
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
Simplest way. From the Sheet, Press Alt + F11 and then insert a module and write the macro.

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
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
 
LVL 45

Expert Comment

by:patrickab
Comment Utility
CRXIuser2005,

Please upload your file.

Patrick
0
 
LVL 17

Author Comment

by:MIKE
Comment Utility
Here you go..thanks for help

CRXIuser2005-Datafile.xlsx
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
Here is the updated file.

Sid
CRXIuser2005-Datafile-2.xlsm
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
To run the macro Click on the Developer tab. and follow what the screen shot says.

Sid
Untitled.jpg
0
 
LVL 17

Author Comment

by:MIKE
Comment Utility
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
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
Comment Utility
I think your macros are disabled.

Anyways here is the file.

Sid
CRXIuser2005-Datafile-2.xlsm
0
 
LVL 17

Author Closing Comment

by:MIKE
Comment Utility
Thanks thats exactly what i needed...now I'm posting another Macro question under different thread...related to this same columne of data....
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
Do paste the link of that thread here.

Sid
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now