Consulting

Results 1 to 9 of 9

Thread: Solved: Delete Column if row is blank

  1. #1
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location

    Solved: Delete Column if row is blank

    I have multple spreadsheets that have data with column headers.
    I would like for a script to look at row 2 of each column from A:W, and if it's empty to delete the entire column.

    Now that is the first part.
    After that is done I should just have a few columns with data in row 2 and possibly more rows.

    I need to look at the second column (B) and cut from row 1 to the last row of data and paste it in column (A) leaving a row empty. And continue to do this with all columns with data in row 2.

    So I should have all the data from the columns now in Column A


    Can this be done?

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    does this help?

    [vba]
    Sub DelColsBlnkCll()

    Dim ws As Worksheet
    Dim Col As Long, LastCol As Long

    With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    .EnableEvents = False
    Calc = .Calculation
    .Calculation = xlCalculationManual
    End With

    For Each ws In Worksheets
    With ws
    LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
    For Col = LastCol To 1 Step -1
    If Trim(.Cells(2, Col).Value) = "" Then .Columns(Col).EntireColumn.Delete
    Next
    .Columns(1).EntireColumn.Delete
    'column B cut from row 1 to the last row of data and paste it in column A
    .Rows(1).EntireRow.Insert
    'leaving a row empty
    End With
    Next

    With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
    .EnableEvents = True
    .Calculation = Calc
    End With

    End Sub
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    hi mancubus, thanks for the quick reply.
    Your script does delete the columns without a value in row 2 with exception to the last column.
    Also it does not put the values in column A instead it's just moving all the values down 1 row so row 1 is all blank and headers are in row 2

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you're wellcome.

    and i'm sorry, i completely misunderstood the requirement.

    give this a try:
    [VBA]
    Sub DelColsBlnkCllThenMove()

    Dim ws As Worksheet
    Dim moveRng As Range, destRng As Range
    Dim Calc as Long, Col As Long, LastCol As Long

    With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    .EnableEvents = False
    Calc = .Calculation
    .Calculation = xlCalculationManual
    End With

    For Each ws In Worksheets
    With ws
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    For Col = LastCol To 1 Step -1
    If Trim(.Cells(2, Col).Value) = "" Then .Columns(Col).EntireColumn.Delete
    Next
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    If LastCol = 1 Then Exit Sub
    For Col = 2 To LastCol
    Set destRng = .Cells(.Rows.Count, "A").End(xlUp).Offset(2, 0)
    Set moveRng = .Range(.Cells(1, Col), .Cells(.Cells(.Rows.Count, Col).End(xlUp).Row, Col))
    moveRng.Cut Destination:=destRng
    Next
    End With
    Next

    With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
    .EnableEvents = True
    .Calculation = Calc
    End With

    End Sub
    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    looks good, but I noticed it does it for all the spreadsheets, can i have just do this on active sheet?

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hey.
    this was not a part of the misunderstanding.
    Quote Originally Posted by Emoncada
    I have multple spreadsheets that have data with column headers.


    [vba]
    Sub DelColsBlnkCllThenMove()

    Dim moveRng As Range, destRng As Range
    Dim Calc As Long, Col As Long, LastCol As Long

    With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    .EnableEvents = False
    Calc = .Calculation
    .Calculation = xlCalculationManual
    End With

    With ActiveSheet
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    For Col = LastCol To 1 Step -1
    If Trim(.Cells(2, Col).Value) = "" Then .Columns(Col).EntireColumn.Delete
    Next
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    If LastCol = 1 Then Exit Sub
    For Col = 2 To LastCol
    Set destRng = .Cells(.Rows.Count, "A").End(xlUp).Offset(2, 0)
    Set moveRng = .Range(.Cells(1, Col), .Cells(.Cells(.Rows.Count, Col).End(xlUp).Row, Col))
    moveRng.Cut Destination:=destRng
    Next
    End With

    With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
    .EnableEvents = True
    .Calculation = Calc
    End With

    End Sub
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    yes your right, only thing is this is a spreadsheet that gets added to on a weekly basis. so it would be better to select which one we need at that time and not redo one's that have previously been done.

  8. #8
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    This looks good mancubus Thanks

  9. #9
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you're wellcome.
    glad it helped...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •