I have a macro that does work, it's just really slow when there is a lot of data and I'm hoping that someone on here can help me to speed it up.
When my VBA does is check the columns of a sheet for the value "NULL" and if it's there it clears that cell. Here's the code:
Sub RemoveNullColumn()
Dim c, count, r, lc, FirstCell
Application.ScreenUpdating = False
count = 0
r = ActiveCell.row 'lets you choose where you want to start even if it is not at "A1"
c = ActiveCell.Column 'lets you choose where you want to start even if it is not at "A1"
c = GetLetterFromNumber(c) 'Gets the column letter from the number provided above
FirstCell = c & r 'sets the cell that you selected to start in so that you will end thereafter removing all the NULL
lc = ActiveSheet.Cells(1, Columns.count).End(xlToLeft).Column 'Finding the last used column
For H = ActiveCell.Column To lc Step 1 'Starts with where you selected a cell and moves right to the last column
For x = 1 To Range(c & Rows.count).End(xlUp).row Step 1 'Starts with the first row and moves through the last row
count = count + 1
If Range(c & x).Value = "NULL" Then 'Checks the contents fo the cell to see if it is "NULL"
Range(c & x).Clear
End If
If count = 1000 Then 'This was used testing but is not seen with the ScreenUpdating set to false
Range(c & x).Select
count = 1
End If
Next x
ActiveCell.Offset(0, 1).Select 'select the next column
c = ActiveCell.Column
c = GetLetterFromNumber(c) 'get the letter of the next column
Next H
Application.ScreenUpdating = True
MsgBox "Finished"
Range(FirstCell).Select
End Sub
Function GetLetterFromNumber(Number)
GetLetterFromNumber = Split(Cells(1, Number).Address(True, False), "$")(0)
End Function
When there are not a lot of rows it is pretty fast, but there are a lot of rows it is slow.
I have a file that I ran it on that has columns from A to AD and 61k+ rows, it took more than 30 minutes to finish and I'm hoping to make that much faster.
Select
, useWith ... End With
, declare data types for your variables.count
is unnecessary. You already havex
counting the rows. Also, why bother converting to column letter? Column numbers work and you are not feeding this information back to the user.Application.Calculation = xlManual
and end it withApplication.Calculation = xlAutomatic
.Range()
You can declare a single range variable and set it once, and then use.Cells(i,j)
in aWith
block to iterate over it. Even better -- find a way to avoid cell by cell iteration (e.g. by using.Find
).