Marcus Aurelius
asked on
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
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
Try this. Please replace the Sheet names and column names accordingly.
Sid
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
Sid
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
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
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
CRXIuser2005,
Please try the macro in the attached file.
Just paste your data into column A and press the button.
Patrick
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
CRXIuser2005-01.xls
Sid - I might have misunderstood!
No Stephen. I reread the question and I have a feeling (And it is growing every second) that I have misunderstood :)
Sid
Sid
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.
Brad
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
Brad
ASKER
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
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
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
ASKER
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
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
That's not what you said originally. Delete line 6 from the code I posted.
ASKER
I need a refresher course on "how" to add macro to Excel 2007...?
Simplest way. From the Sheet, Press Alt + F11 and then insert a module and write the macro.
Sid
Sid
To record a macro see this tutorial.
http://office.microsoft.com/en-us/excel-help/record-and-use-excel-macros-HA001054837.aspx
Sid
http://office.microsoft.com/en-us/excel-help/record-and-use-excel-macros-HA001054837.aspx
Sid
Also try this code
Sid
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
Sid
CRXIuser2005,
Please upload your file.
Patrick
Please upload your file.
Patrick
ASKER
ASKER
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...????
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks thats exactly what i needed...now I'm posting another Macro question under different thread...related to this same columne of data....
Do paste the link of that thread here.
Sid
Sid
ASKER