VBA Code to delete empty columns after a certain row

NSegna

New Member
Joined
May 30, 2019
Messages
11
Hello,
I was wondering if there was a VBA code that can delete all columns that are blank after a certain row.
For example, the code would delete the last 3 columns but keep the first 3 even though Rows 1 and 2 have a value in them.

[TABLE="width: 301"]
<colgroup><col width="61" style="width: 46pt; mso-width-source: userset; mso-width-alt: 2230;"><colgroup><col width="68" style="width: 51pt; mso-width-source: userset; mso-width-alt: 2486;" span="5"><tbody>[TR]
[TD="width: 61, bgcolor: transparent"]1[/TD]
[TD="width: 68, bgcolor: transparent"]2[/TD]
[TD="width: 68, bgcolor: transparent"]3[/TD]
[TD="width: 68, bgcolor: transparent"]4[/TD]
[TD="width: 68, bgcolor: transparent"]5[/TD]
[TD="width: 68, bgcolor: transparent"]6[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]QUANTITY[/TD]
[TD="bgcolor: transparent"]QUANTITY2[/TD]
[TD="bgcolor: transparent"]QUANTITY3[/TD]
[TD="bgcolor: transparent"]QUANTITY4[/TD]
[TD="bgcolor: transparent"]QUANTITY5[/TD]
[TD="bgcolor: transparent"]QUANTITY6[/TD]
[/TR]
[TR]
[TD="bgcolor: #C6E0B4"]10[/TD]
[TD="bgcolor: #C6E0B4"]5[/TD]
[TD="bgcolor: #C6E0B4"] [/TD]
[TD="bgcolor: #C6E0B4"] [/TD]
[TD="bgcolor: #C6E0B4"] [/TD]
[TD="bgcolor: #C6E0B4"] [/TD]
[/TR]
[TR]
[TD="bgcolor: #FFF2CC"]40[/TD]
[TD="bgcolor: #FFF2CC"] [/TD]
[TD="bgcolor: #FFF2CC"]20[/TD]
[TD="bgcolor: #FFF2CC"] [/TD]
[TD="bgcolor: #FFF2CC"] [/TD]
[TD="bgcolor: #FFF2CC"] [/TD]
[/TR]
</tbody>[/TABLE]

Thanks
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
In your example you are deleting after 3 rows which is the last row. In your actual file, will you identify the row or will it be the last row? Please clarify how the row is determined.
 
Upvote 0
See the comment at the top of the code.

Code:
Option Explicit


Sub delcol()
[COLOR=#ff0000]'This code assumes that all columns to the right of the last[/COLOR]
[COLOR=#ff0000]'column in the last row will be deleted[/COLOR]
Dim i As Long, lr As Long, lc As Long
Dim last As Long
last = Cells(1, Columns.Count).End(xlUp).Column
lr = Range("A" & Rows.Count).End(xlUp).Row
lc = Cells(lr, Columns.Count).End(xlToLeft).Column + 1
Range(Cells(1, lc), Cells(1, last)).EntireColumn.Delete


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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