VBA Loop

fari1

Active Member
Joined
May 29, 2011
Messages
362
hi,i need some immediate help on my following code, my below code works for just one column, i want to make it work from column A:I, in short want to create a loop out of it.

Code:
Sub Checkblank()
    Dim mycount As Long
    mycount = Application.WorksheetFunction.CountBlank(Range("B:B"))
    If mycount < 1 Then
    Exit Sub
    Else
    Range("B:B").Select
    Selection.Delete Shift:=xlToLeft
    End If
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Maybe this?

Code:
Sub CheckBlank()
Dim i As Long
For i = 9 To 1 Step -1
    If WorksheetFunction.CountBlank(Columns(i)) > 0 Then Columns(i).Delete
Next i
End Sub
 
Upvote 0
Thanks for quicki response, when i run the code, it deleted all the columns, the whole sheet just turned blank
 
Upvote 0
i just saw, the code which i posted has problem,
i want this code to check if the whole column e.g A:A is blank then delete, its not just about one cell, the whole column is blank then delete, if not blank then find the next column, if none of the columns in range(A:J) are blank then exit
 
Upvote 0
Try this with a copy of your sheet

Code:
Sub CheckBlank()
Dim i As Long
For i = 9 To 1 Step -1
    If WorksheetFunction.CountA(Columns(i)) = 0 Then Columns(i).Delete
Next i
End Sub
 
Upvote 0
you are the best, its working like a charm........
if u have time, can i ask some more questions in this thread, its about loop but in a different scenerio, as i'm working on a project and novice to vba, so need some help
 
Upvote 0
If it is a related question please continue in this thread.

If it is a different question (even if it involves loops) please start a new thread.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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