How to remove blank space

Vishaal

Well-known Member
Joined
Mar 16, 2019
Messages
543
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
  2. Web
Hi,

I have the following sheet

[TABLE="width: 274"]
<colgroup><col><col><col><col></colgroup><tbody>[TR]
[TD]Col A[/TD]
[TD]Col B[/TD]
[TD]Col C[/TD]
[TD]Col D[/TD]
[/TR]
[TR]
[TD]S.No.[/TD]
[TD]Name[/TD]
[TD]Attendence[/TD]
[TD]Hour[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]John[/TD]
[TD]P[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Nick[/TD]
[TD]P[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD]P[/TD]
[TD]7[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD]P[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Raghu[/TD]
[TD]P[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Ravi[/TD]
[TD]P[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Rozi[/TD]
[TD]P[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD][/TD]
[TD]P[/TD]
[TD]9[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]Matty[/TD]
[TD]P[/TD]
[TD]8[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD][/TD]
[TD]P[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]Saurabh[/TD]
[TD]P[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Hanery[/TD]
[TD]P[/TD]
[TD]5

[/TD]
[/TR]
</tbody>[/TABLE]

Now i want to remove all the entries in row 3 & 4 & 8 & 10 from the Col C to Col D

Any formula for VB Code
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Do you want to physically delete the row, so all the other rows move up, or just blank out columns C and D, leaving gaps in your data?
 
Upvote 0
just blank out columns C and D

Pls also advice if coloumn are more than 15
 
Upvote 0
Try:
Code:
Sub MyClearColumns()

    Dim lr As Long
    Dim r As Long
    
    Application.ScreenUpdating = False

'   Find last row with data in column A
     lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows
    For r = 2 To lr
'       Check to see in column B is empty
        If Cells(r, "B") = "" Then
'           Clear columns C through D
            Range(Cells(r, "C"), Cells(r, "D")).ClearContents
        End If
    Next r

    Application.ScreenUpdating = True
    
    MsgBox "Process complete"

End Sub
To change the number of columns to clear, just change the "D" here to the last column you want cleared:
Code:
Range(Cells(r, "C"), Cells(r, [COLOR=#ff0000]"D"[/COLOR])).ClearContents
 
Last edited:
Upvote 0
Try:
Code:
Sub MyClearColumns()

    Dim lr As Long
    Dim r As Long
    
    Application.ScreenUpdating = False

'   Find last row with data in column A
    [B][COLOR="#FF0000"][SIZE=3]lr = [/SIZE][/COLOR][/B]Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows
    For r = 2 To lr
'       Check to see in column B is empty
        If Cells(r, "B") = "" Then
'           Clear columns C through D
            Range(Cells(r, "C"), Cells(r, "D")).ClearContents
        End If
    Next r

    Application.ScreenUpdating = True
    
    MsgBox "Process complete"

End Sub

TYPO ALERT (see missing part of code highlighted in red above).
 
Upvote 0
Here is another macro that you can consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub ExtendBlanksToRightForBlankNames()
  Intersect(Columns("C:[B][COLOR="#FF0000"]D[/COLOR][/B]"), Range("B1:B" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlBlanks).EntireRow).ClearContents
End Sub[/td]
[/tr]
[/table]
To change the number of columns to clear, just change the red highlighted "D" to the column letter desired.
 
Upvote 0
TYPO ALERT (see missing part of code highlighted in red above).
Thanks for catching that Rick!

I was copying similar code I was using on a different one, and missed that part.
I will amend my original code.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top