How to remove blank space

Vishaal

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

I have the following sheet

Col ACol BCol CCol D
S.No.NameAttendenceHour
1JohnP5
2NickP6
3P7
4P2
5RaghuP4
6RaviP4
7RoziP5
8P9
9MattyP8
10P5
11SaurabhP5
12HaneryP5


<colgroup><col><col><col><col></colgroup><tbody>
</tbody>

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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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,214,947
Messages
6,122,413
Members
449,082
Latest member
tish101

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