Clear Contents below a dynamic Range

Keewi82

New Member
Joined
Jun 29, 2016
Messages
9
Hi everyone

hopefully this is a simple one but my limited VBA experience has me stumped. I want to clear the contents of columns a and b when a is blank. Below is the table of sample data i want to clear cells a4:b7, however this range need to be dynamic based off if column A is blank and b contains something.
ab
11011111500
21011157500
3
1010257500
4500
5500
6500
7500

<tbody>
</tbody>
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi everyone

hopefully this is a simple one but my limited VBA experience has me stumped. I want to clear the contents of columns a and b when a is blank. Below is the table of sample data i want to clear cells a4:b7, however this range need to be dynamic based off if column A is blank and b contains something.
ab
11011111500
21011157500
3
1010257500
4500
5500
6500
7500

<tbody>
</tbody>

What is in Column A that needs to be cleared... formulas displaying the empty text string ("")?

Will the blanks always be located after the last cell in Column A displaying a number?
 
Upvote 0
Hi Rick
I probably wasn't as clear as i should have been. It column B that need be be cleared if a is blank. Column A is not formula driven.
 
Upvote 0
Something like this?


Code:
Sub test()

Dim lastrw As Long
lastrw = Cells(Rows.Count, 2).End(xlUp).Row


For Each cell In Range("A1:A" & lastrw)
    If IsEmpty(cell) = True Then
        cell.Offset(0, 1).ClearContents
    End If
Next
    

End Sub


Note that the lastrw variable is determining the extent of the range of cells to be examined. Also, lastrw is based on Column B.
 
Last edited:
Upvote 0
Hi Rick
I probably wasn't as clear as i should have been. It column B that need be be cleared if a is blank. Column A is not formula driven.
You did not answer my second question, so here are two macros... the first should be used if all the blank cells always occur after the last data cell in Column A (like you example shows) and the second should be used if blank cells could occur in between data cells in Column A.
Code:
[table="width: 500"]
[tr]
	[td]Sub DeleteIfBlanksAtEnd()
  Range(Cells(Rows.Count, "A").End(xlUp).Offset(1, 1), Cells(Rows.Count, "B").End(xlUp)).ClearContents
End Sub[/td]
[/tr]
[/table]
Code:
[table="width: 500"]
[tr]
	[td]Sub DeleteIfBlanksScatteredAbout()
  Range("A1", Cells(Rows.Count, "B").End(xlUp).Offset(, -1)).SpecialCells(xlBlanks).Offset(, 1).ClearContents
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,215
Members
448,874
Latest member
b1step2far

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