I have a little problem, I occassionally bump into this kind of problem but I haven’t found a fast solution so far.
So, imagine we have an Excel worksheet and let's suppose that we have a couple of numbers in column ’A’ with some empty cells in it. Altogether (just to make it simple) we have the first 10 cells in column 'A' to observe. For example:
3
(empty cell)
(empty cell)
6
(empty cell)
4
(empty cell)
23
(empty cell)
2
Now in the next step I would like to collect these numbers into another column (eg. column ’B’) using VBA. Obviously I just want to collect those cells which contain a number and I want to ignore the empty cells. So I would like to get a column something like this:
3
6
4
23
2
I have already written the following code but I’m stuck at this point.
Sub collect()
For i = 1 To 10
if cells(i,1)<>"" then...
Next i
End Sub
Is there an easy way to solve this problem?
Thanks in advance,
Laszlo
Probably the quickest and easiest way is to use Excel's Advanced Filter - the only ammendment you'll need to make is it add a field name and criteria. You can even list unique items only![enter image description here](https://i.stack.imgur.com/vMVxQ.png)
The VBA equivalent is
If you wish to loop manually & don't mind specifying the maximum row limit;
You should be able to use the method in the post int the comments, but you could also use
SpecialCells
likeRange("A:A").SpecialCells(xlCellTypeConstants,xlNumbers).Copy
to get all of the filled cells.Edit: needed constants not formulas.
This will work for any number of rows that you select. It will always output in the next column at the start of your selection e.g. if data starts in B10 it will ooutput in C10