Clear table depending on cell and column headers VBA

Akira181

Board Regular
Joined
Mar 23, 2010
Messages
67
Office Version
  1. 365
Platform
  1. Windows
I have a number in cell L1 (copied from a different sheet as I can't get vba to reference a cell on a different sheet) and my data is in a table from F to BE, starting at row 4 with headers in row 3.

Everytime the worksheet activates, I need it to clear F4 to the last row of data to whatever column header matches the number in L1.

I've been trying for ages but I cannot get my head around VBA's. Can someone help?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try:
VBA Code:
Sub ClearData()
    Dim LastRow As Long, fnd As Range
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Set fnd = Rows(3).Find(Range("L1").Value, LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd Is Nothing Then
        Range("F4").Resize(LastRow - 3, fnd.Column - 5).ClearContents
    End If
End Sub
 
Upvote 0
Solution
It was throwing up an error because I had a locked row at the end of the table with subtotals that needed to stay. Changed "LastRow -3" to "-4" and it worked a treat.

Must have spent 4 hours yesterday trying to write something similar and didn't come close. I'll learn VBA this year even if it's the only thing I achieve this year. Thanks a lot!
 
Upvote 0

Forum statistics

Threads
1,214,527
Messages
6,120,054
Members
448,940
Latest member
mdusw

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