Delete Columns if multiple specific cells are empty in column

johnmpc

Board Regular
Joined
Oct 19, 2020
Messages
108
Office Version
  1. 365
Platform
  1. Windows
HI have the following spreadsheet.

Customer NameJoanna KennyEmail:
CompanyDynamic ConstructionPhone No:
Address
Mobile No:
Order GP%
37.84%​
Total Profit
£51.27​
Total Not Inc VAT
£135.50​
Total including VAT
£162.60​
CodeSupplierProduct DescriptionColourCost (Min)DecorationSellGP%QTYSizing ChartOne SizeSMLXL2XL3XL4XL5XLOTHERDecoration 1Position 1Decoration 2Position 2Decoration 3Postition 3
PEN881MPencarrieRussell Zip Neck Micro FleeceBlack
£11.65​
£19.75​
41.01%​
1​
Unisex
1​
Dynamic ConstructionLeft Chest
CodeSupplierProduct DescriptionColourCost (Min)DecorationSellGP%QTYSizing ChartOne SizeSMLXL2XL3XL4XL5XLOTHERDecoration 1Position 1Decoration 2Position 2Decoration 3Postition 3
embroideryMPCEmbroideryNone
£1.25​
£1.75​
28.57%​
1​
Unisex
1​
Dynamic ConstructionLeft Chest
CodeSupplierProduct DescriptionColourCost (Min)DecorationSellGP%QTYSizing ChartTrousers
30​
32​
34​
36​
38​
40​
42​
44​
OTHERDecoration 1Position 1Decoration 2Position 2Decoration 3Postition 3
PENRX601PencarriePRO RTX Pro Workwear Trouserblack
£6.99​
£12.00​
41.75%​
2​
Trousers
2​
Dynamic ConstructionLeft Chest
CodeSupplierProduct DescriptionColourCost (Min)DecorationSellGP%QTYSizing ChartLadies
6​
8​
10​
12​
14​
16​
18​
20​
OTHERDecoration 1Position 1Decoration 2Position 2Decoration 3Postition 3
PEN539FPencarrieRussell Lds 65/35 Pique PoloFrench Navy
£6.20​
£1.40​
£12.00​
36.67%​
4​
Ladies
2​
2​
CodeSupplierProduct DescriptionColourCost (Min)DecorationSellGP%QTYSizing ChartShoe Size678910111213OTHERDecoration 1Position 1Decoration 2Position 2Decoration 3Postition 3
PENRs348MPencarrieResult W-G Lightweight Trainersblack/grey
£26.95​
£42.00​
35.83%​
1​
Shoes
1​
I was previously only using 1 sizing header from Column K and could delete the columns if it was empty from a certain point down.

Now i would like to delete the column if all the cells 16,19,22,25,28,31,34,37,40,43,46,49,52 in each column are empty.

Thanks in advance
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi johnmpc,

Try this (though initially on a copy of your data as the results cannot be undone if they are not as expected):

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsSrc As Worksheet
    Dim lngLastCol As Long, lngMyCol As Long
    Dim varRow As Variant
    Dim blnDeleteCol As Boolean
    
    Set wsSrc = ThisWorkbook.Sheets("Sheet1") '<- Sheet name with data. Change to suit if necessary.
    
    If WorksheetFunction.CountA(wsSrc.Cells) = 0 Then
        MsgBox "There is no data on & """ & wsSrc.Name & """ to work with.", vbExclamation
        Exit Sub
    Else
        lngLastCol = wsSrc.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    End If
    
    Application.ScreenUpdating = False
    
    For lngMyCol = lngLastCol To 1 Step -1
        blnDeleteCol = True
        For Each varRow In Array(16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52)
            If Len(wsSrc.Cells(CLng(varRow), lngMyCol)) > 0 Then
               blnDeleteCol = False
               Exit For
            End If
        Next varRow
        If blnDeleteCol = True Then
            wsSrc.Columns(lngMyCol).Delete
        End If
    Next lngMyCol
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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