Results 1 to 5 of 5

Thread: Delete blank columns with VB.NET

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    492

    Delete blank columns with VB.NET

    Hi:

    Code:
            For j = 1 To xlSheet.Columns.Count
                ' Delete Blank Columns
    
            Next
    All I want to do is delete columns with no data on xlSheet.

    I've looked at many examples online, and nothing works.

    Thanks!
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world!

  2. #2
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,934

    Re: Delete blank columns with VB.NET

    Have you seen this link?

    https://stackoverflow.com/questions/...s-in-excel-vba

    If yes, post the code you actually tried, and what results, if any, that you got.

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,721

    Re: Delete blank columns with VB.NET

    Go backwards through the columns
    You're going forward from 1 to Count, but by deleting a column you change the count (Deleting column B means Column C shifts one to the left and becomes Column B)
    So by stepping 1 forward after a delete you actually jumped a column, nevermind overshooting the end
    Your step brings you to Column B, you check if B is empty, yes, it's empty, Delete Column B, every column right of it shifts one to the Left (C becomes B, D becomes C etc).
    Delete is done, your For-Loop steps one Forward, now you're at C, completely missing that the "old" C is now B, and your current C is the old D = jumped one column
    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,469

    Re: Delete blank columns with VB.NET

    also you do not need to loop all columns, just the columns within the usedrange

    if the usedrange contains a large number of columns it may well be much faster to add the empty columns to a non contiguous range, then delete that range when all the columns have been tested

    you can test for and delete empty columns like
    Code:
    Dim col, rng As Range, sht As Worksheet
    Set sht = Sheets("sheet1")
    With sht
        Set rng = .Cells(1, Columns.Count).EntireColumn        '  assumes last column is never used
        lcol = .UsedRange.Columns.Count + .UsedRange.Column
        For col = 1 To lcol
            If .Columns(col).Rows.Count = WorksheetFunction.CountBlank(Columns(col)) Then    ' column is empty
                Set rng = Union(rng, .Columns(col))
            End If
        Next
    End With
    rng.Delete
    you will need to change the sheet name and convert the VBA to .net objects, no set in .net and some type conversions required
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    492

    Re: Delete blank columns with VB.NET

    Beautiful, Thanks!
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world!

Posting Permissions

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



Click Here to Expand Forum to Full Width