Solved

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

Posted on 2011-02-10
24
607 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

628 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