Macro to clear contents based on a cell value

rjacmuto32

Board Regular
Joined
Jul 14, 2004
Messages
98
searched but wasn't able to find anything similar to this. Not sure If maybe my subject might have been wrong. Anyway I have data in rows.

I want a macro to go through each row and if column B for that row is blank, then clear the contents in columns C-L

So if B1 has a number nothing, If B2 is blank then clear the contents in cells C2-L2.

The other problem is I can have a differnt last row number each time. 1 day there is 500 rows another day there is 700 rows. There always is a value in Column A which can be used to locate the last row function. For that I use LastRow = Range("A" & Rows.Count).End(xlUp).Row I'm just not sure how to clear the contents is column B is blank of other cells.

Thanks,
--Robert
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You mean like this?

Code:
Sub Clearcontents()
Dim LastRow as Long, n as Long

Application.ScreenUpdating = False

LastRow = Range("A" & Rows.Count).End(xlUp).Row 

For n = 1 to LastRow

If Cells(n,2)="" Then Range(Cells(n,3),(Cells(n,12).ClearContents
Next n

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Try:

Code:
Sub Test()
    Dim LastRow As Long
    Dim r As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    For r = 1 To LastRow
        If Range("B" & r).Value = "" Then
            Range("C" & r & ":L" & r).ClearContents
        End If
    Next r
End Sub
 
Upvote 0
Try this macro:
Code:
Sub Macro1()
Dim Limit As Long
Dim c As Long
Limit = Cells(Rows.Count, 1).End(xlUp).Row
For c = Limit To 1 Step -1
    If Cells(c, 2) = "" Then
        Range("C" & c & ":L" & c).ClearContents
    End If
Next c
End Sub
 
Upvote 0
Code:
Dim rng As Range, area As Range
On Error Resume Next
Set rng = Range([B2], [A65536].End(xlUp)(1, 2)).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If rng Is Nothing Then Exit Sub
For Each area In rng.Areas
    area.Resize(, 11).ClearContents
Next
 
Upvote 0
Thanks. Those worked. I see some different parts of the code that I don't currently understand. I have something to research now can learn about some more things from this.

--Robert
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,664
Members
448,976
Latest member
sweeberry

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