How to Delete all columns after last cell with data in row 1?

Dear All experts,

I already use this link's Mr Patrick Matthews's suggested code for delete all rows below data provided at this link of this site.

https://www.experts-exchange.com/questions/27255636/Delete-All-Rows-after-Last-Row-with-Data.html

but same question for columns...how to delete all columns  after last filled data on row 1.
or how to modify above linked code to delete columns.

Please Help.

Regards,
Chirag Raval
Chirag RavalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try
Sub clear()
   Dim lastCell As Range
   Set lastCell = Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious, searchorder:=xlByColumns)
   If Not lastCell Is Nothing Then Range(Cells(1, lastCell.Column + 1), Cells(Rows.Count, Columns.Count)).Delete
End Sub

Open in new window

EDIT corrected code

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShumsDistinguished Expert - 2017Commented:
Hi Chirag,

Try below, change sheet name accordingly.
Sub DelCols()
Dim Ws As Worksheet
Dim LastC As Long
Dim DelRng As Range
Set Ws = Worksheets("Sheet1") ' Change your sheet name here
LastC = Ws.Cells(1, Columns.Count).End(xlToLeft).Column
Set DelRng = Ws.Range(Ws.Cells(1, LastC + 1), Ws.Cells(1, Columns.Count))
Application.ScreenUpdating = False
DelRng.EntireColumn.Delete
Application.ScreenUpdating = True
End Sub

Open in new window

0
Chirag RavalAuthor Commented:
Dear All Experts,

Great, Amazing Both works like a charm..
Thank you very much

Regards,
Chirag Raval
0
Rgonzo1971Commented:
Solutions according to author's comment
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.