有没有办法从多张Excel中拉出单独的行

时间:2016-01-14 21:09:03

标签: excel vba excel-vba

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

img http://i67.tinypic.com/2s6rkma.png

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

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

我可以将表格格式化为我希望它的外观,除非有办法做到这一点!打开输入那个!

请注意,某些部分可能包含多个主要项目。但是,它们将始终为粗体并保持缩进并位于“材质”部分。

1 个答案:

答案 0 :(得分:1)

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

修改

循环的范围包括几个部分:

'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