find last value in a column and clear contents up VBA

pook_666

Board Regular
Joined
Aug 16, 2018
Messages
80
Hi magicians,

I have the below code that finds the last cell in L and clears its contents.

VBA Code:
Range("L" & Cells(Rows.Count, "L").End(xlUp).Row).ClearContents

What I want to do is find the last cell in L and clear contents upwards to the next gap.

For example, on the below, I want to clear contents 14:18 only. However, the values can be changeable and not always in the cells as listed below. Hence why I need to find the last active cell and then looking upwards (in the below case to 14) and only clearing that last batch of values.

1618986598182.png


I hope I've explained myself clearly enough - please let me know if you need any other info! Thanks!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,140
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
If the cells are truly blank you can just do something like

VBA Code:
Dim myRng As Range
Set myRng = Range("L" & Cells(Rows.Count, "L").End(xlUp).Row)
Range(myRng.End(xlUp), myRng).ClearContents
 
Solution

pook_666

Board Regular
Joined
Aug 16, 2018
Messages
80
Thanks for the help @MARK858, however, this clears the top section instead of the last section of values. E.g using my picture snapshot above, this deletes 1:4 instead of 14:18?
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,140
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Doesn't for me, it clears the bottom section. Are you sure that your "blank" cells are actually blank i.e. no formulas, no spaces etc.

What does the formula
Excel Formula:
=ISBLANK(L13)
give you?
 

pook_666

Board Regular
Joined
Aug 16, 2018
Messages
80
Aha worked it out....I put the set myRng in the wrong spot (put it at the beginning of the long VBA I have instead of right before the clear contents line). All working now - thanks a lot for your help!
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,140
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Happy you got it working (even though it shouldn't make any difference the "Set" being at the start of the code unless you have something else you haven't stated affecting it between it and the ClearContents [as long as you have the lines in the same order])
 

Forum statistics

Threads
1,136,303
Messages
5,674,966
Members
419,537
Latest member
ucatchy

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
Top