Solved

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

Posted on 2011-02-10
24
600 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
  • 3
  • +2
24 Comments
 
LVL 17

Author Comment

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

Expert Comment

by:SiddharthRout
ID: 34864157
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
ID: 34864158
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34864191
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
ID: 34864203
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
ID: 34864209
Sid - I might have misunderstood!
0
 
LVL 30

Expert Comment

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

Sid
0
 
LVL 81

Expert Comment

by:byundt
ID: 34864670
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
ID: 34864855
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 81

Expert Comment

by:byundt
ID: 34864872
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
ID: 34864906
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
ID: 34864909
That's not what you said originally. Delete line 6 from the code I posted.
0
 
LVL 17

Author Comment

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

Expert Comment

by:SiddharthRout
ID: 34864980
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
ID: 34864984
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34865088
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
ID: 34865197
CRXIuser2005,

Please upload your file.

Patrick
0
 
LVL 17

Author Comment

by:MIKE
ID: 34865252
Here you go..thanks for help

CRXIuser2005-Datafile.xlsx
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34865397
Here is the updated file.

Sid
CRXIuser2005-Datafile-2.xlsm
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34865415
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
ID: 34865473
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
ID: 34865488
I think your macros are disabled.

Anyways here is the file.

Sid
CRXIuser2005-Datafile-2.xlsm
0
 
LVL 17

Author Closing Comment

by:MIKE
ID: 34865517
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
ID: 34865522
Do paste the link of that thread here.

Sid
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

735 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