-1

你好再次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
4

2 回答 2

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
于 2013-09-16T09:33:33.840 回答
0

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

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

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

希望这对于您要实现的目标来说是一个足够好的答案...

于 2013-09-09T21:47:59.553 回答