使用Excel VBA从包含空单元格的列收集数字 [英] collect numbers from a column containing empty cells using Excel VBA

查看:135
本文介绍了使用Excel VBA从包含空单元格的列收集数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点问题,我偶尔会碰到这种问题,但到目前为止我还没有找到一个快速的解决方案。



所以,想象一下Excel工作表,我们假设我们在列A中有几个数字,其中有一些空单元格。总而言之(只是为了简单起见),我们在列A中有前10个单元格来观察。例如:

  3 
(空单元格)
(空单元格)
6
(空单元格)
4
(空单元格)
23
(空单元格)
2
/ pre>

现在在下一步中,我想使用VBA将这些数字收集到另一列(例如列'B')。显然我只想收集那些包含数字的单元格,我想忽略空单元格。所以我想得到一个这样的列:

  3 
6
4
23
2

我已经写了以下代码,但我坚持点数

  Sub collect()
对于i = 1至10
如果单元格(i,1 )然后...
Next i
End Sub

有没有办法解决这个问题?



提前感谢



Laszlo

解决方案

可能最快最简单的方法是使用Excel的高级过滤器 - 您需要做的唯一的修改就是添加字段名称和标准。您甚至可以列出唯一的项目



VBA相当于

  Sub test()

With Sheet1
.Range(B1:B8)。AdvancedFilter Action:= xlFilterCopy,CriteriaRange:=。Range(_
D1:D2),CopyToRange:=。Range(F1),Unique:=假
结束

结束子


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

The VBA equivalent is

Sub test()

With Sheet1
    .Range("B1:B8").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=.Range( _
        "D1:D2"), CopyToRange:=.Range("F1"), Unique:=False
End With

End Sub

这篇关于使用Excel VBA从包含空单元格的列收集数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆