VBA code to fill in blank cells running really slowly

SimonR

New Member
Joined
Mar 14, 2022
Messages
18
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi,

I have a spreadsheet that originally contained a lot of merged cells in column H and the cells to the left of them were blank. This caused problems with the filter in column D even after I unmerged the whole of column H, so I began filling in the blanks in D with x's. This was really time-consuming because for some reason filtering column D to show only blanks (so I could fill the whole empty space with x's) didn't work, and these many sections with blanks alternated with those with text. So I tried this code to speed things up:

VBA Code:
Sub fillblankcells()
Dim r As Integer
r = 11382
Do Until r = 33453
If Cells(r, 4) = "" Then
Cells(r, 4) = "x"
End If
r = r + 1
Loop

End Sub

However, after a couple of minutes and me stopping the code, the cells in column D have been filled only up to row 11667. Any idea what is wrong with my code?
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Or you can just use this one line of code:
VBA Code:
Sub fillblankcells()
    Range("D11382:D33453").SpecialCells(xlCellTypeBlanks).Value = "x"
End Sub
 
Upvote 0
Solution
Loops are notoriously slow in VBA, especially if you are updating cells.

You can quickly and easily fill all the blank cells in a range with a value without using VBA by using this method here: Microsoft Excel: How to Quickly Fill Blank Cells in a Worksheet

If you need VBA code, you can use VBA to perform the steps shown there.
Thanks, I didn't know about that although I'm familar with the Go To Special function.

So why can blank cells cause problems with filters (not the first time I've experienced this problem)? Is the rule that for everything to be included in a list for filtering, if the list contains blanks, there needs to be at least one column elsewhere with continuous text?
 
Upvote 0
So why can blank cells cause problems with filters (not the first time I've experienced this problem)? Is the rule that for everything to be included in a list for filtering, if the list contains blanks, there needs to be at least one column elsewhere with continuous text?
I am not sure what you mean. Could you show us an example of your data and exactly how you are filtering?
Also, which version of Excel are you using here, 2010 or 365 (you have selected both)?

Lastly, not sure if you saw my last post with the one-line VBA code that would fix it.
 
Upvote 0
I use 365 for work and both 2010 and 365 at home (leaving aside working at home). I probably ought to edit my account details if necessary before I submit a question so people are clear what version I'm using when I have a problem (though it's usually 365 these days).

I played around with various sets of data and I discovered the answer to my own question - you can't properly filter sets of data if you have at least one blank row in them - that was what caused the problem. I did see your second post before I replied and the code did the trick, thanks.
 
Upvote 0
You are welcome.
Glad you got it working!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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