I would like to clear any cell that only contains “?” and/or “,”.
Take a look at the image below:
1.JPG
From the image above we can see that in cell D4, E1, F2, F3 and F4 the contents consist of “?” and/or “,”, so I want my macro to detect such cells and clear its content.
Do not clear contents of cell that has together with “?” and/or “s” there may be other character. For e.g. from the above image if you look at cell D2 there is other words such as jp6_7, jp6_8, so such cells should not be cleared
I would like to do this to the entire column D, E and F
The final outcome should look like the image below
2.JPG
I have also attached a sample worksheet,
Book2.xls
Sheet 1 shows the initial texts
Sheet 2 contains a button where the macro should be written
Sheet 3 shows how the final outcome looks like
Last edited by darkhorse4321; 11-18-2010 at 09:59 PM.
Why not just use the Find & Replace tool found in the EDIT dropdown of the toolbar, search for ? and replace with nothing.
Not all forums are the same - seek and you shall find
Hi Simon,
If the cell just contain "?" or ",", i would have used replace tool and changed it with nothing. But the contents in the cell are something like "?,?,?" or "?,?,?,?,?,?" and so on. It is not consistent that's why i can't use replace tool.take a look at the image i uploaded.
Are there always numbers contained in cells that also contain ? that you want to keep?
Not all forums are the same - seek and you shall find
Yes some cells will contain not only numbers but letters too, together with "?" and/or "," and you are right i want to keep these cells.
If i'm not wrong. The structure for my codes should roughly look like this:
If a cell only contain question mark "?" and/or comma "," then delete the cell content
else
do nothing
loop the above step till the last row of column D,E and F.
Just that i don't know how to translate this into code form.
Well as long as the cells that you want to keep will ALWAYS have at least one number in it this should work'code supplied by Simon Lloyd '16/11/2010 'Microsoft Office Help 'Excel Conference Poll Sub clear_Cells() Dim Rng As Range, MyCell As Range, i As Long Set Rng = ActiveSheet.Range("A1:D" & ActiveSheet.Range("D" & rows.count).end(xlup).row) For Each MyCell In Rng For i = 1 To Len(MyCell.Text) HasNum = IsNumeric(Mid(MyCell, i, 1)) If HasNum = True Then GoTo Nxt End If Next i MyCell.ClearContents Nxt: Next MyCell End Sub
Last edited by Simon Lloyd; 11-16-2010 at 11:59 AM.
Not all forums are the same - seek and you shall find
Thanks Simon,your codes are working fine, as long the cell has a number in it, the cell won't get cleared.
But is it possible to include letters in it too. For e.g. if the cell has a letter (a,b,c...z) in it then the cell shouldn't be cleared
Not tested as i dont have time
'Code supplied by Simon Lloyd '17/11/2010 'Microsoft Office Help 'Excel Conference Poll Sub clear_Cells() Dim Rng As Range, MyCell As Range, i As Long Set Rng = ActiveSheet.Range("A1:D" & ActiveSheet.Range("D" & rows.count).end(xlup).row) For Each MyCell In Rng For i = 1 To Len(MyCell.Text) HasNum = IsNumeric(Mid(MyCell, i, 1)) IF Not Mid(mycell.Value, intI, 1) Like "[A-Z,a-z,0-9,.]" then goto Nxt If HasNum = True and If Not IsNumeric(mycell) Then GoTo Nxt End If Next i MyCell.ClearContents Nxt: Next MyCell End Sub
Not all forums are the same - seek and you shall find
hi simon, the codes didn't work. There is error in the code.
Like i said, i didnt have time to test it, thsi works or seems to work for me:
'Code supplied by Simon Lloyd '18/11/2010 'Microsoft Office Help 'ExcelConference Poll Sub clear_Cells() Dim Rng As Range, MyCell As Range, i As Long Set Rng = ActiveSheet.Range("A1:D" & ActiveSheet.Range("D" & Rows.Count).End(xlUp).Row) For Each MyCell In Rng For i = 1 To Len(MyCell.Text) HasNum = IsNumeric(Mid(MyCell, i, 1)) If HasNum = True Then GoTo Nxt End If If Mid(MyCell.Value, i, 1) Like "[A-Z a-z]" Then GoTo Nxt End If Next i MyCell.ClearContents Nxt: Next MyCell End Sub
Last edited by Simon Lloyd; 11-18-2010 at 07:43 PM.
Not all forums are the same - seek and you shall find
Thanks simon. It worked for me too![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks