VBA - clear contents in row

excelninja

New Member
Joined
Apr 19, 2012
Messages
19
Hi

I am trying to run the loop below to find any blank cell in column Q, starting at Row 10 and looping until the lastrow with contents. When there is a blank cell, I want to clear the contents of the cell from column Q to column AM. Any help would be great.

Sub ClearEmptyRows()

Dim xSheet As Worksheet
Dim i As Integer

Set xSheet = Sheets("Sector")

xSheet.Select

For i = 10 To xSheet.Range("Q" & 65000).End(xlUp).Row
If xSheet.Range("Q" & i).Value = "" Then

xSheet.Range("Q" & i & ":" & "AM" & i).Clear

End If
Next

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Nightcrawler.

The code does not seem to be deleting the contents when there is a blank cell in column Q. If I do a normal if = "" in the worksheet it recognizes the cell as blank. It just doesn't seem to be working in VBA.
 
Upvote 0
I'm not sure why but this seems to have done the trick

Private Sub ClearEmptyRows()

Dim xSheet As Worksheet
Dim i As Integer

Set xSheet = Sheets("Sector")

xSheet.Select

For i = 10 To xSheet.Range("Q" & 65000).End(xlUp).Row
If xSheet.Range("Q" & i).Value <> "" Then

' do nothing

Else

xSheet.Range("Q" & i & ":" & "AM" & i).Clear

End If
Next

End Sub
 
Upvote 0
Try This

Code:
Dim xSheet As Worksheet
Dim i As Integer

Set xSheet = Sheets("Sector")

xSheet.Select

For i = 10 To xSheet.Range("Q" & 65000).End(xlUp).Row
    If Trim(xSheet.Range("Q" & i)) = "" Then
        xSheet.Range("Q" & i & ":AM" & i).Clear
    End If
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,552
Messages
6,114,278
Members
448,560
Latest member
Torchwood72

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