-
Nov 24th, 2021, 03:41 PM
#1
Thread Starter
Fanatic Member
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! And recently, I've been promoted to a 'Finatic Member,' whatever that means!
-
Nov 24th, 2021, 07:24 PM
#2
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.
-
Nov 25th, 2021, 03:20 AM
#3
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
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
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
-
Nov 25th, 2021, 04:17 AM
#4
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
-
Nov 29th, 2021, 09:41 AM
#5
Thread Starter
Fanatic Member
Re: Delete blank columns with VB.NET
- A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|