Deleting Columns

G

Guest

Need some VB help on deleting columns. I have a VB routine that will delete
entire rows, I just need some help getting it turned around to columns. This
works good and deletes entire blank rows only.

Thanks
Bob

Sub demo()
DeleteBlankDataRows "E", "AI"
End Sub

Sub DeleteBlankDataRows(FromColumn As String, ThruColumn As String)
Dim nFirstRow As Long
Dim nLastRow As Long
Dim nRow As Long
Dim sAddress As String
Dim rng As Range
With ActiveSheet.UsedRange
nLastRow = .Rows.Count + .Row - 1
nFirstRow = .Row
End With
Application.ScreenUpdating = False
For nRow = nLastRow To nFirstRow Step -1
sAddress = FromColumn & nRow & ":" & ThruColumn & nRow
Set rng = Range(sAddress)
If WorksheetFunction.CountBlank(rng) = rng.Count Then
rng.EntireRow.Delete
End If
Next nRow
Application.ScreenUpdating = True
End Sub
 
E

Executor

Hi Robert,

You are close:

Sub DeleteBlankDataColumns(FromColumn As String, ThruColumn As String)
Dim nFirstRow As Long
Dim nLastRow As Long
Dim nFirstCol As Long
Dim nLastCol As Long
Dim nCol As Long
Dim sAddress As String
Dim rng As Range

nFirstCol = Range(FromColumn & "1").Column
nLastCol = Range(ThruColumn & "1").Column
With ActiveSheet.UsedRange
nLastRow = .Rows.Count + .Row - 1
nFirstRow = .Row
End With
Application.ScreenUpdating = False
For nCol = nLastCol To nFirstCol Step -1
Set rng = Range(Cells(nFirstRow, nCol), Cells(nLastRow, nCol))
If WorksheetFunction.CountBlank(rng) = rng.Count Then
rng.EntireColumn.Delete
End If
Next nCol
Application.ScreenUpdating = True
End Sub


Hoop this helps


Executor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top