|
-
Aug 2nd, 2010, 02:15 AM
#1
Thread Starter
Member
[Solved] Macro Looking for Blank cell or Cell with space(s) & deleting it
hello everyone,
I am stuck on a problem, I want a macro that will look for a cell that has a space or more within and then deletes that cell automatically.
However the Excel sheet is relatively big and thus I do not want to go through the sheet cell by cell.. (My computer / Excel does not seem to manage it..)
I have also tried this, but that did not seem to work at all.. all cells with spaces were still there.
Code:
Sub clear()
Dim c As Range
For Each c In Range("A1:Z2000")
If c = " " Then Range("A" & c.Row & ":A" & c.Row).ClearContents
Next c
For Each c In Range("A1:Z2000")
If c = " " Then Range("A" & c.Row & ":A" & c.Row).ClearContents
Next c
For Each c In Range("A1:Z2000")
If c = " " Then Range("A" & c.Row & ":A" & c.Row).ClearContents
Next c
End Sub
** This is not my code, it was copied from the internet (slightly modified)
Thanks for the help in advance!
Last edited by Fr0mi; Aug 3rd, 2010 at 04:36 AM.
Reason: solved
-
Aug 2nd, 2010, 04:11 AM
#2
Re: Macro Looking for Blank cell or Cell with space(s) & deleting it
your code only clears column A regardless of which column the spaces are found in, is that what you want?
are you looking for any cell content that contains any spaces or cells that contain only spaces?
one way to test for spaces is to use instr
or
if trim(c) = "" then c.clearcontents
for cells with spaces within other text
If Not c = Trim(c) Then c.ClearContents
Last edited by westconn1; Aug 2nd, 2010 at 04:41 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 2nd, 2010, 05:04 AM
#3
Thread Starter
Member
Re: Macro Looking for Blank cell or Cell with space(s) & deleting it
Thanks for the reply!
I already revised the code & had a look at yours.
Well basically both codes do not what I am aiming for. As now the clearcontents command seems to be applied to the whole row / range.
However, I wanted to look through the specified range and if one cell is found to have spaces but no text (should be empty) than I want to apply the clearcontents command to that single cell.
Yet, I do not want to go through the work sheet selecting every single cell, as that seems to crash my Excel & takes far too long.
Thanks for further help!
-
Aug 2nd, 2010, 05:28 AM
#4
Re: Macro Looking for Blank cell or Cell with space(s) & deleting it
However the Excel sheet is relatively big and thus I do not want to go through the sheet cell by cell..
Search the forum for .Find
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Aug 2nd, 2010, 07:21 AM
#5
Thread Starter
Member
Re: Macro Looking for Blank cell or Cell with space(s) & deleting it
Thanks for the reply, I found your previous posts regarding the find command. However this does not work in my case.
I even downloaded your example code and changed the look for "Replace me" to " ". Then wrote a simple space into a cell and clicked on the macro. Response was "Not Found".
Besides I wanted to also delete the cells having maybe two or maybe three spaces in them. (I can always loop it once I have something that works, yes but maybe there is a better method)
Best,
-
Aug 2nd, 2010, 07:46 AM
#6
Re: Macro Looking for Blank cell or Cell with space(s) & deleting it
Ok let me give you an example...
EDIT
Do you want to delete the cell or do you want to clear the contents of the cell?
This works for me... for clearcontents 
Code:
Sub FindAndDelete()
Dim ws As Worksheet
Dim aCell As Range
Dim rRange As Range
Dim srchString As String
Dim Exitloop As Boolean
'~~> Set the relevant worksheet
Set ws = Worksheets("Sheet1")
'~~> Working with the 1st Column. Change as applicable...
Set rRange = ws.Columns(1)
'~~> Search Space
srchString = " "
Set aCell = rRange.Find(What:=srchString, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Do While Exitloop = False
If Not aCell Is Nothing Then
aCell.ClearContents
Set aCell = rRange.FindNext(After:=aCell)
Else
'~~> If not found
Exitloop = True
End If
Loop
End Sub
Last edited by Siddharth Rout; Aug 2nd, 2010 at 08:01 AM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Aug 2nd, 2010, 09:18 AM
#7
Re: Macro Looking for Blank cell or Cell with space(s) & deleting it
Need more treatment otherwise some cells that contain text such as "abc xyz" will be cleared.
I am lazy on typing, try this:
Code:
Sub FindSpaceCellsAndClear()
Dim aCell As Range
For Each aCell In Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeConstants)
If Not aCell.Value Like "*[! ]*" Then aCell.ClearContents
Next
End Sub
For easier to understand, the line
If Not aCell.Value Like "*[! ]*" Then
can be written as
If Trim(aCell.Value) = "" Then
or
If Len(Trim(aCell.Value)) = 0 Then
but the last two work 3 times slower.
-
Aug 3rd, 2010, 04:33 AM
#8
Thread Starter
Member
Re: Macro Looking for Blank cell or Cell with space(s) & deleting it
Thanks that works perfectly! Just what I was looking for.
However did you know if you have a combined cell in that worksheet the code outputs a '1004' run-time error? (Works fine just have to remove the combined cell - but still happens)
Thanks again anhn
-
Aug 3rd, 2010, 08:16 AM
#9
Addicted Member
Re: [Solved] Macro Looking for Blank cell or Cell with space(s) & deleting it
by "combined cell" do you mean a merged cell?
-
Aug 4th, 2010, 01:30 AM
#10
Thread Starter
Member
Re: [Solved] Macro Looking for Blank cell or Cell with space(s) & deleting it
Yes my bad, I meant merged cells.
-
Aug 4th, 2010, 05:54 AM
#11
Addicted Member
Re: [Solved] Macro Looking for Blank cell or Cell with space(s) & deleting it
Some ideas to experiment with:
Code:
Dim aCell As Range
For Each aCell In Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeConstants)
If aCell.MergeCells Then ' If cell is merged Then
aCell.MergeCells = False ' unmerge cell
aCell.Interior.ColorIndex = 6'to identify cell had been merged,change its color to Yellow
End If
'Choose which of the next two commands gives you the results you want
'If Not aCell.Value Like "*[! ]*" Then aCell.ClearContents
If Len(Trim(aCell.Value)) <> Len(aCell.Value) Then aCell.ClearContents
Next
Last edited by SQLADOman; Aug 4th, 2010 at 05:59 AM.
Reason: colored code comments green
-
Aug 4th, 2010, 06:01 AM
#12
Thread Starter
Member
Re: [Solved] Macro Looking for Blank cell or Cell with space(s) & deleting it
Thanks for the help!
For me that was not too much of a problem as I just had one cell merged with the next. However I wanted to mention it in case someone else would have a similar problem.
I am not sure how to do it myself, but can you use the .find function or something similar to look for formatted cells? Such as the yellow once to merge them again after the spaces have been removed?
Thanks though.
-
Aug 4th, 2010, 07:35 AM
#13
Addicted Member
Re: [Solved] Macro Looking for Blank cell or Cell with space(s) & deleting it
It doesn't seem worth doing just for the purpose of providing the solution in case someone else might eventually need it.
The coloring only with one color, will not properly identify what was merged under some circumstances. Such as if there might be merged cells that includes more than one row and possibly two consecutive cells in the column are merged but not to each other.
As long as the merged cells are always in the same row and the primary cell is always in column A , I think it would be fairly easy. - But you need to do the coloring differently than how I did it in my previous post.
Something like this:
Code:
Dim aCell As Range
For Each aCell In Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeConstants)
If aCell.MergeCells Then ' If cell is merged Then
aCell.MergeArea.Interior.ColorIndex = 6 'to identify merged area
aCell.MergeCells = False ' unmerge cell
End If
'Choose which of the next two commands gives you the results you want
'If Not aCell.Value Like "*[! ]*" Then aCell.ClearContents
If Len(Trim(aCell.Value)) <> Len(aCell.Value) Then aCell.ClearContents
Next
Last edited by SQLADOman; Aug 4th, 2010 at 07:46 AM.
-
Aug 4th, 2010, 07:46 AM
#14
Thread Starter
Member
Re: [Solved] Macro Looking for Blank cell or Cell with space(s) & deleting it
True it does not seem worth doing.
I appreciate your help anyhow!
Anyway I suppose you could do something like this:
Code:
Dim aCell As Range
For Each aCell In Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeConstants)
If aCell.MergeCells Then ' If cell is merged Then
aCell.MergeCells = False ' unmerge cell
' Well do whatever action required
'If Not aCell.Value Like "*[! ]*" Then aCell.ClearContents
If Len(Trim(aCell.Value)) <> Len(aCell.Value) Then aCell.ClearContents
Next
' Merge again and continue the search - Maybe that would stop the script from crashing
aCell.MergeCells = True ' to merge them again should still be same selection or not?
End If
-
Aug 4th, 2010, 07:55 AM
#15
Addicted Member
Re: [Solved] Macro Looking for Blank cell or Cell with space(s) & deleting it
It would not be quite that easy, as aCell.MergeCells = True does not identify what range should be merged. You would need to give an instruction for that. Either store the merged range in a variable before un-merging, or use the colored range. Doing it right away like you thought of is a good idea though, because if you do use the colored range for the re-merge and then remove the coloring afterwards, that avoids the adjacent cell coloring issues that I mentioned before.
Last edited by SQLADOman; Aug 4th, 2010 at 08:01 AM.
-
Aug 5th, 2010, 06:15 PM
#16
Addicted Member
Re: [Solved] Macro Looking for Blank cell or Cell with space(s) & deleting it
I was bored so....., even though you were not in need, but only curious, here is one way of un-meging and re-merging.
Edit: Added different version below this version.
Code:
Private Sub CommandButton1_Click()
Dim aCell As Range
Dim MergedAdress As String
For Each aCell In Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeConstants)
If aCell.MergeCells Then 'If aCell is merged Then
MergedAdress = aCell.MergeArea.Address 'Load MergedAdress variable
aCell.MergeCells = False 'unmerge range
'If Not aCell.Value Like "*[! ]*" Then
'--OR--
If Len(Trim(aCell.Value)) <> Len(aCell.Value) Then aCell.ClearContents
Range(MergedAdress).MergeCells = True 'merge stored address
GoTo NextLoop
End If
If Len(Trim(aCell.Value)) <> Len(aCell.Value) Then aCell.ClearContents
NextLoop:
Next
End Sub
Version below process's the used range only in Column A
Edit:Changed the section of code that colors each instance of a merged range, to use alternating colors, because incrementing will fail if there are too many.
Code:
Sub Clear_Cell_Contents_If_Space_Before_or_After()
Dim aCell As Range, MergedAdress As String, BolMerged As Boolean
Dim wks As Worksheet, rng As Range, LastRow As Long, intColor As Integer
Set wks = ActiveSheet
With wks
LastRow = .[A65536].End(xlUp).Row
Set rng = .Range("A1:A" & LastRow)
End With
For Each aCell In rng 'rng = Cell A1 to LastRow In Column A
BolMerged = False
If aCell.MergeCells Then 'If aCell is merged Then
BolMerged = True'If aCell is merged Set Boolean Flag for later use in routine
MergedAdress = aCell.MergeArea.Address 'Load MergedAdress variable
aCell.MergeCells = False 'unmerge range
End If
'Depending on the desired result use one of the next two commands
'If Not aCell.Value Like "*[! ]*" Then aCell.ClearContents
'See end of Post#7 for description of what the command above does.
'Command below clears aCell if there is a space before or after it's contents.
If Len(Trim(aCell.Value)) <> Len(aCell.Value) Then aCell.ClearContents
If BolMerged = True Then 'If BolMerged Flag was set to True near the start of the Loop
Range(MergedAdress).MergeCells = True 'merge stored address
If intColor = 7 Then
intColor = 6
Else 'will alternate between two colors, if merged range is within a single row.
intColor = 7'alternating inconstant if there are both single & multi-row merges.
End If
Range(MergedAdress).Interior.ColorIndex = intColor 'Color Variable
End If
Next
End Sub
Last edited by SQLADOman; Aug 7th, 2010 at 06:09 PM.
Reason: I changed code to alternate colors, because Increment color fails if there are a lot merged cells
-
Aug 7th, 2010, 11:17 PM
#17
Re: [Solved] Macro Looking for Blank cell or Cell with space(s) & deleting it
To deal with merged cells, you need just a simple fix as below. Do not unmerge then remerge the cells.
Code:
Sub FindSpaceCellsAndClear()
Dim aCell As Range
For Each aCell In Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeConstants)
If Not aCell.Value Like "*[! ]*" Then aCell.MergeArea.ClearContents
Next
End Sub
-
Aug 26th, 2010, 04:46 AM
#18
Thread Starter
Member
Re: [Solved] Macro Looking for Blank cell or Cell with space(s) & deleting it
Thanks guys!
I havnt been online for a while so here is the thanks!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|