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.

123456
QUANTITYQUANTITY2QUANTITY3QUANTITY4QUANTITY5QUANTITY6
105
40 20

<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>
</tbody>

Thanks
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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,214,534
Messages
6,120,084
Members
448,943
Latest member
sharmarick

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