仅返回带有数字和文本的列中的数字

时间:2013-09-09 21:32:30

标签: excel excel-formula

再次问好stackoverflow。我的问题应该相当简单;如何从混合的数字和文本列中返回一系列数字?

我有类似的东西;

Text
5
Text
1
Text
Text
4

我想从这个,在另一个工作表中,是;

5
1
0     (if two rows of text are next to each other, I'd like to return 0)
4

感谢您的帮助。

此电子表格列出了容器及其内容。该系列的第一个文字是识别标记。第二个是容器中有多少项,第三个是第一个中的较小容器(我不关心那个中有多少项,只要它存在与否)。

Column A  (Sheet1)
Box
5
Bag
Box
1
Bag
Box
Bag
Box
4
Bag

输入是从程序中复制/粘贴的,不能更改。我想要做的是,在另一个电子表格中,说;盒子有5件,1袋。代表为;

Row 1 (Sheet2)
 Box 5 1

2 个答案:

答案 0 :(得分:0)

我能想到的最简单的方法是使用NEXT列到此列,并使用以下公式(假设您的数据在单元格A1中开始):

=IF(ISNUMBER(A1),A1,IF(NOT(ISNUMBER(A2)),0,""))

然后将该公式拖动到“辅助列”的长度,然后将其过滤为非空白。

希望这对你想要实现的目标是一个足够好的答案......

答案 1 :(得分:0)

Sub Boxes_and_stuff()

Dim j As Byte
Dim ThisPage As Worksheet, TargetPage As Worksheet
Dim NewBook As Workbook

Set ThisPage = ThisWorkbook.ActiveSheet
Set NewBook = Workbooks.Add   ' or you can address an existing file with workbooks.open(filename)
Set TargetPage = NewBook.Worksheets(1) ' you can also decide the sheet where you want to write your values (here by default is the first excel's "tab")

j = 1 ' cells' scanner

Do Until IsEmpty(ThisPage.Cells(j, 1))

    If ThisPage.Cells(j, 1).Text = "Box" Then   
    ' if "Box" is found scan cell below to find bags or numbers

            If ThisPage.Cells(j + 1, 1).Text = "Bag" Then TargetPage.Cells(1 + j, 1) = "Box 0 1" ' if only a bag is found
            If ThisPage.Cells(j + 1, 1).Text = "Box" Then TargetPage.Cells(1 + j, 1) = "Box 0 0" ' if nothing is inside a box and another box comes next (I'm assuming that "0" items will never show)

            If IsNumeric(ThisPage.Cells(j + 1, 1)) Then ' if a number shows...

                If (ThisPage.Cells(j + 2, 1)).text = "Bag" then '... the next can be bag
                    TargetPage.Cells(1 + j, 1) = "Box " & ThisPage.Cells(j + 1, 1) & " 1"
                Else '...or not
                    TargetPage.Cells(1 + j, 1) = "Box " & ThisPage.Cells(j + 1, 1) & " 0"
                End If

            End If

    End If

j = j + 1

Loop

End Sub