简体   繁体   English

使用 Excel VBA 从包含空单元格的列中收集数字

[英]Collect numbers from a column containing empty cells using Excel VBA

I have a little problem, I occasionally 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.因此,假设我们有一个 Excel 工作表,假设我们在“A”列中有几个数字,其中有一些空单元格。 Altogether (just to make it simple) we have the first 10 cells in column 'A' to observe.总而言之(只是为了简单起见)我们要观察“A”列中的前 10 个单元格。 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 (for example, column 'B') using VBA.现在,在下一步中,我想使用 VBA 将这些数字收集到另一列(例如,“B”列)中。 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?有解决这个问题的简单方法吗?

Probably the quickest and easiest way is to use Excel's Advanced Filter - the only amendment you'll need to make is it add a field name and criteria.可能最快和最简单的方法是使用 Excel 的高级过滤器——您需要做的唯一修改是添加字段名称和条件。 You can even list unique items only:您甚至可以只列出独特的项目:

在此处输入图像描述

The VBA equivalent is VBA 等价物是

Sub test()

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

End Sub

You should be able to use the method in the post int the comments, but you could also use SpecialCells like Range("A:A").SpecialCells(xlCellTypeConstants,xlNumbers).Copy to get all of the filled cells.您应该能够在评论后使用该方法,但您也可以使用像 Range("A:A"). SpecialCells Range("A:A").SpecialCells(xlCellTypeConstants,xlNumbers).Copy这样的 SpecialCells 来获取所有填充的单元格。

Edit: needed constants not formulas.编辑:需要常量而不是公式。

If you wish to loop manually and don't mind specifying the maximum row limit;如果您希望手动循环并且不介意指定最大行限制;

Dim i As long, values As long

For i = 1 To 10
    If cells(i, 1).Value <> "" Then
        values = (values + 1)
        ' // Adjacent column target
        cells(values, 2).value = cells(i, 1).value
    End If
Next i

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 eg if data starts in B10 it will ooutput in C10它将始终在您选择开始时的下一列中输出,例如,如果数据从 B10 开始,它将在 C10 中输出

Sub RemoveBlanks()
    Dim cl As Range, cnt As Long
    cnt = 0

    For Each cl In Selection
        If Not cl = vbNullString Then
            Cells(Selection.Cells(1, 1).Row, Selection.Cells(1, 1).Column).Offset(cnt, 1) = cl
            cnt = cnt + 1
        End If
    Next cl
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM