Deleting empty columns

rabbits

New Member
Joined
Aug 24, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I'm a VBA novice, and I'm trying to write a macro that deletes all the empty columns between my data. The number and location of the empty columns changes every time.

This is what I have so far from cobbling together previous advice from here and online, but when I run it nothing seems to happen.

Rich (BB code):
Sub FindLastColumn()
'   Find last column used in row 9
    Dim lc As Long
    lc = ActiveSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
   
'   Loop through all columns backwards
    For c = lc To 1 Step -1
        If Cells(11, c).Value = " " Then Cells(11, c).EntireColumn.Delete
    Next c
End Sub

Thanks
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hello Rabbits,

Assuming that the blank columns are entirely blank(all the way down your column data, inc. Row1), then this may work:-

VBA Code:
Sub Test()

    Application.ScreenUpdating = False
    
    On Error Resume Next
        With Sheet1.Rows(1)
               .SpecialCells(4).EntireColumn.Delete
        End With
    On Error GoTo 0

    Application.ScreenUpdating = True
    
End Sub

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Hello Rabbits,

Assuming that the blank columns are entirely blank(all the way down your column data, inc. Row1), then this may work:-

VBA Code:
Sub Test()

    Application.ScreenUpdating = False
   
    On Error Resume Next
        With Sheet1.Rows(1)
               .SpecialCells(4).EntireColumn.Delete
        End With
    On Error GoTo 0

    Application.ScreenUpdating = True
   
End Sub

I hope that this helps.

Cheerio,
vcoolio.
Thanks for the suggestion, but this didn't work unfortunately - the code ran but nothing seemed to happen (no errors or anything)

Also, how would I update 'Sheet1' name in the code? I've tried doing it with and without quotation marks but only get syntax errors.

Thanks
 
Upvote 0
Hello Rabbits,

If Sheet1 is not the correct worksheet then nothing will happen.

Not knowing your worksheet name, I used a Sheet code (Sheet1) but your actual sheet code may be different. In the VB Editor, over to the left in the Project Explorer, you'll see your worksheets listed by name in parentheses and the Sheet Codes immediately to the left (Sheet1, Sheet2 etc...). You can change the sheet code to suit or you can simply use ActiveSheet instead. You could also declare a worksheet variable and use that instead. For example:-

VBA Code:
Dim wsR As Worksheet: Set wsR = Sheets("Rabbitts")

If you want to use the worksheet variable, place it just above:

VBA Code:
Application.ScreenUpdating = False

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,215,737
Messages
6,126,558
Members
449,318
Latest member
Son Raphon

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