Consolidating Blank Columns grr

ellison

Active Member
Joined
Aug 1, 2012
Messages
343
Office Version
  1. 365
Platform
  1. Windows
We are using an excel file to tidy up data and the file is getting bigger and bigger (understatement!)
To help consolidate the data, we would like to copy it into a fresh sheet & keep all of the rows, but only keep columns of data which contain any info
Also each Column has a header in the first row, which has got us totally scratching our heads about how to do this without some fairly precarious copy and pasting…!
Here is some sample data, hope this makes this a bit clearer


Line IDColour1Colour2Colour3Colour4Colour5
Data1whiteyellow
Data2
Data3greyred
Data4purplewhite


So in this instance, neither of the columns on Colour 2 nor Colour 4 have any entries, so when we copy it over, we'd like to see something like this:
(Oops, just to confirm: despite the fact that "Data2" has no info on that "row", we would still like to keep Data2 in the results.)

LineIDColour1Colour3Colour5
Data1whiteyellow
Data2
Data3greyred
Data4purplewhite



And just to confirm, I have tried using the skip blank feature, but it doesn't seem to want to play ball - especially with me!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this on your data sheet for results on sheet2
VBA Code:
Sub nCopy()
Dim Ray As Variant, nRay As Variant, nStr As String, Ac As Long, Sp As Variant
 Ray = ActiveSheet.Cells(1).CurrentRegion
 For Ac = 1 To UBound(Ray, 2)
    If Application.CountA(Cells(2, Ac).Resize(UBound(Ray, 1) - 1)) > 0 Then
        nStr = nStr & IIf(nStr = "", Ac, "," & Ac)
    End If
 Next Ac
  Sp = Split(nStr, ",")
  nRay = Application.Index(Ray, Evaluate("row(" & 1 & ":" & UBound(Ray) & " )"), Sp)

  With Sheets("Sheet2")
      .UsedRange.Clear
      .Range("A1").Resize(UBound(Ray, 1), UBound(Sp) + 1) = nRay
  End With
End Sub
 
Upvote 0
Hi there. Once you have copied the data into a new sheet, run this macro on that new sheet:
VBA Code:
Sub remover()
    lastcol = ActiveSheet.UsedRange.Columns.Count
    For tstcol = lastcol To 1 Step -1
        If Application.WorksheetFunction.CountA(Cells(1, tstcol)) = 1 And Application.WorksheetFunction.CountA(Columns(tstcol)) = 1 Then
            ActiveSheet.Columns(tstcol).EntireColumn.Delete

        End If
    Next
End Sub
It will remove any columns that have something in row 1 but nowhere else.
 
Upvote 0
Just another way:
VBA Code:
Sub a1116434a()
Dim i As Long
Application.ScreenUpdating = False
Cells.Copy Sheets("Sheet2").Range("A1")
Sheets("Sheet2").Activate
For i = Cells(1, Columns.count).End(xlToLeft).Column To 1 Step -1
    If Columns(i).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row = 1 Then Columns(i).Delete
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Works beautifully ………… HUGE thanks to you both!!!!
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,575
Members
449,089
Latest member
Motoracer88

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