Code To Delete Columns Containing No Data

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have a massive file, several thousand rows by about 50 columns. Is there a code I can run that will delete all columns that contain no data below header row 2. Otherwise I will have to sort each column individually which will take ages. Thanks.
 
Your code appears to replace a carriage return with a space and then remove the space. Without seeing the contents of the cells, it's difficult for me to see how this is working for you. Again, it would be easier if I could see your actual file. Please refer to my suggestion in Post #7 and if possible upload the file.
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try:
Code:
Sub DelCols()
    Application.ScreenUpdating = False
    Dim lCol As Long
    lCol = ActiveSheet.UsedRange.Columns.Count
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim x As Long
    For x = lCol To 1 Step -1
        If WorksheetFunction.CountA(Range(Cells(3, x), Cells(LastRow + 1, x))) = 0 Then
            Columns(x).EntireColumn.Delete
        End If
    Next x
End Sub


Hi. Would this code be able to be adapted where it deletes rows rather than columns when there is nothing in them apart from column A? Thanks.
 
Upvote 0
namecarcolor
alanfordred
billrenaultblue
davemazdareddata after running macro
ednissanblue
fredfiatgreen
harryfordblue
ianfordgreen
joemaseratiyellow
original data
namecarcolor
alanfordred
billrenaultblue
colin
davemazdared
ednissanblue
fredfiatgreen
george
harryfordblue
ianfordgreen
joemaseratiyellow
this very simple macro was used
Sub Macro4()
'
' Macro4 Macro
' Macro recorded 10/07/2019 by bob
'
'
10 If Count > 11 Then GoTo 20
For j = 2 To 11
If Cells(j, 2) = "" And Cells(j, 3) = "" Then Rows(j).Select: Selection.Delete Shift:=xlUp: Count = Count + 1: GoTo 10
Next j
20 End Sub

<colgroup><col width="64" span="17" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
Thanks but there will be thousands of rows and dozens of columns, how would that affect the code?
 
Upvote 0
namecarcolortemp1temp2temp3temp4temp5temp6temp7temp8temp9temp10temp11
alanfordred0.2528050.6634660.109490.5152890.862460.4348140.5184380.7904450.1765190.8054490.528769
davemazdared0.6108510.3825920.6488010.0815240.8583110.688380.788570.7553160.1705130.8148760.103729
ednissanblue0.9373120.5549320.1945090.4855130.4353440.7255680.0150660.1723350.7155570.693570.764023
harryfordblue0.3485050.8759260.682610.1237730.7833740.731410.3922510.503590.9666880.0546320.753888
ianfordgreen0.1454970.1338180.437820.0447230.4318560.4649790.7662290.3498630.2185820.9066320.248651
joemaseratiyellow0.2164650.1100830.3326060.7343770.5302620.9112610.7020720.2586480.3977240.2631180.296757
Sub Macro4()
'
' Macro4 Macro
' Macro recorded 10/07/2019 by bob
'
'
10 If Count > 510 Then GoTo 20
For j = 2 To 500
For k = 2 To 50
If Cells(j, k) <> "" Then GoTo 18 Else GoTo 15
15 Next k
Rows(j).Select: Selection.Delete Shift:=xlUp: Count = Count + 1: GoTo 10
18 Next j
20 End Sub
the above code found and deleted two names where all columns were blank
set the j limit to a bit above your number of rows
set the k limit to your number of columns
test
then set two helper cells that measure the number of rows and columns
eg set cell BZ1 to =countif(A1:A100000,<>")
then amend to for j=2 to cells(1,53)

<colgroup><col width="64" span="16" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
Try:
Code:
Sub DelCols()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim lCol As Long
    lCol = ActiveSheet.UsedRange.Columns.Count
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim x As Long
    For x = LastRow To 2 Step -1
        If WorksheetFunction.CountA(Cells(x, 2).Resize(, lCol - 1)) = 0 Then
            Rows(x).EntireRow.Delete
        End If
    Next x
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
Code:
Sub DelCols()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim lCol As Long
    lCol = ActiveSheet.UsedRange.Columns.Count
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim x As Long
    For x = LastRow To 2 Step -1
        If WorksheetFunction.CountA(Cells(x, 2).Resize(, lCol - 1)) = 0 Then
            Rows(x).EntireRow.Delete
        End If
    Next x
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub

Works perfect thanks mumps. Thanks oldbrewer also for your efforts.
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,214
Members
449,074
Latest member
cancansova

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