Vba Excel宏循环浏览所有行

Vba Excel宏循环浏览所有行,vba,excel,Vba,Excel,我接收Excel行中的数据,如下所示: INVOICE NO. 1000001 Plan AAAAA 17371 22.00 Plan BBBBB 31782 0.00 Plan CCCCC 13918 44.00 Total for 1000001 66.00 INVOICE NO. 1000002 Plan AAAAA 31385 0.00 Plan CCCCC 15981 44.00 Total for 1000002 44.00 INVOICE NO. 1000003 Plan BBBB

我接收Excel行中的数据,如下所示:

INVOICE NO. 1000001
Plan AAAAA 17371 22.00
Plan BBBBB 31782 0.00
Plan CCCCC 13918 44.00
Total for 1000001 66.00
INVOICE NO. 1000002
Plan AAAAA 31385 0.00
Plan CCCCC 15981 44.00
Total for 1000002 44.00
INVOICE NO. 1000003
Plan BBBBB 13181 0.00
Plan CCCCC 01828 16.00
GARBAGE TEXT
Total for 1000003 16.00
我需要将此数据显示在单独的工作表上(甚至替换当前工作表),如下所示:

需要注意的是:

  • 我无法更改接收此数据的方式。我希望我能
  • 每套发票以发票号XXXXXXX开始,以XXXXXXX的总额结束,然后是其总成本
  • 只有三种可能的计划AAAAA/BBBBB/CCCCC,但并非所有三种都出现在发票中
  • 有时在输出中间有垃圾文本,需要忽略。 我想创建一个Excel宏,它将遍历Excel工作表的每一行,伪代码是

  • 创建具有所需标题的新表
  • 查找发票编号的第一个实例,并将以下编号添加到表中
  • 检查下一行,当该行不包含总计时,检查是否包含计划AAAA,如果包含,则在拆分最后一部分作为成本时将行添加到表中,如果不保留黑色,保持在线并检查计划BBBBB,重复BBBBB和CCCCC

  • 如何在每一行中循环执行上述操作(或者是否有更好的方法来完成此操作)?

    您可以在此处使用For Each循环。它会变成s.th。像这样:

    For Each rw In sheet.Rows
        If rw.Cells(1, 1).Value = "<YOUR STRING HERE>" Then 
        ...
    Next rw
    
    表.行中每个rw的
    
    如果rw.Cells(1,1).Value=”“,则
    ...
    下一个rw
    

    如果您按照自己的伪代码中的解释这样做,我看不出有什么理由不起作用。然而,这看起来像是一个向上工作的问题;)

    代码不是很灵活,但它基于上述标准工作:

    Sub test()
    
    
    Sheets.Add After:=ActiveSheet
    
    Sheets(2).Range("a1").Value = "Invoice No"
    Sheets(2).Range("b1").Value = "Plan AAAAA"
    Sheets(2).Range("c1").Value = "Plan AAAAA Cost"
    Sheets(2).Range("d1").Value = "Plan BBBBB"
    Sheets(2).Range("e1").Value = "Plan BBBBB Cost"
    Sheets(2).Range("f1").Value = "Plan CCCCC"
    Sheets(2).Range("g1").Value = "Plan CCCCC Cost"
    Sheets(2).Range("h1").Value = "Total"
    
    sheet2y = 1
    
    For y = 1 To 10000
        If Len(Sheets(1).Cells(y, 1).Value) > 0 Then
                If LCase(Sheets(1).Cells(y, 1).Value) Like "*invoice*" Then
                    If sheet2y > 1 Then
                        Sheets(2).Cells(sheet2y, 8).Value = Sheets(2).Cells(sheet2y, 3).Value + Sheets(2).Cells(sheet2y, 5).Value + Sheets(2).Cells(sheet2y, 7).Value
                    End If
                    sheet2y = sheet2y + 1
                    Sheets(2).Cells(sheet2y, 1).Value = Trim(Split(LCase(Sheets(1).Cells(y, 1).Value), "no.")(1))
    
                End If
    
    
            If LCase(Sheets(1).Cells(y, 1).Value) Like "*plan*" Then
                For sheet2x = 2 To 6 Step 2
                    If LCase(Sheets(1).Cells(y, 1).Value) Like "*" & LCase(Sheets(2).Cells(1, sheet2x).Value) & "*" Then
                        Sheets(2).Cells(sheet2y, sheet2x).Value = Sheets(2).Cells(1, sheet2x).Value & " " & Split(Sheets(1).Cells(y, 1).Value, " ")(2)
                        Sheets(2).Cells(sheet2y, sheet2x + 1).Value = Trim(Split(Sheets(1).Cells(y, 1).Value, " ")(3))
                    End If
                Next sheet2x
            End If
    
        Else
            Sheets(2).Cells(sheet2y, 8).Value = Sheets(2).Cells(sheet2y, 3).Value + Sheets(2).Cells(sheet2y, 5).Value + Sheets(2).Cells(sheet2y, 7).Value
            Exit For
        End If
    Next y
    
    End Sub
    

    正如代码注释中所述,我估计您的数据存储在以
    A1
    单元格开头的列A中。然后,它将被所需的输出所取代(您说过这是允许的)

    请尝试以下代码:

    Sub ParseInvoices()
    Dim lsatRow As Long, i As Long, invoiceData As Variant, currentRow As Long, currentData As String
    currentRow = 2
    'get last row of A column - I assumed that there you store your data
    lastrow = Cells(Rows.Count, 1).End(xlUp).Row
    'read data
    invoiceData = Range("A1:A" & lastrow).Value
    'clear data from sheet
    Columns(1).Clear
    'set up table headers
    Cells(1, 1) = "Invoice No"
    Cells(1, 2) = "Plan AAAAA"
    Cells(1, 3) = "Plan AAAAA Cost"
    Cells(1, 4) = "Plan BBBBB"
    Cells(1, 5) = "Plan BBBBB Cost"
    Cells(1, 6) = "Plan CCCCC"
    Cells(1, 7) = "Plan CCCCC Cost"
    Cells(1, 8) = "Total"
    
    For i = 1 To lastrow
        currentData = RTrim(LTrim(invoiceData(i, 1)))
        Select Case UCase(Left(currentData, 10))
            Case "INVOICE NO"
                Cells(currentRow, 1).Value = Mid(currentData, InStrRev(currentData, " "))
            Case "PLAN AAAAA"
                Cells(currentRow, 2).Value = Left(currentData, InStrRev(currentData, " ") - 1)
                Cells(currentRow, 3).Value = Mid(currentData, InStrRev(currentData, " "))
            Case "PLAN BBBBB"
                Cells(currentRow, 4).Value = Left(currentData, InStrRev(currentData, " ") - 1)
                Cells(currentRow, 5).Value = Mid(currentData, InStrRev(currentData, " "))
            Case "PLAN CCCCC"
                Cells(currentRow, 6).Value = Left(currentData, InStrRev(currentData, " ") - 1)
                Cells(currentRow, 7).Value = Mid(currentData, InStrRev(currentData, " "))
            Case "TOTAL FOR "
                Cells(currentRow, 8).Value = Mid(currentData, InStrRev(currentData, " "))
                currentRow = currentRow + 1
        End Select
    Next
    End Sub
    

    很好的解释。到目前为止你做了什么?这是一个学习VBA的简单项目,指针:从第1行循环到a列的最后一行(或循环到IsEmpty单元格),如果。。。然后为**发票编号*和**总计设置块,然后为指定列的计划选择案例。请正确设置插入数据的格式。也许可以用一个。事实上,不可能确定数据是如何跨列分布的。
    Sub ParseInvoices()
    Dim lsatRow As Long, i As Long, invoiceData As Variant, currentRow As Long, currentData As String
    currentRow = 2
    'get last row of A column - I assumed that there you store your data
    lastrow = Cells(Rows.Count, 1).End(xlUp).Row
    'read data
    invoiceData = Range("A1:A" & lastrow).Value
    'clear data from sheet
    Columns(1).Clear
    'set up table headers
    Cells(1, 1) = "Invoice No"
    Cells(1, 2) = "Plan AAAAA"
    Cells(1, 3) = "Plan AAAAA Cost"
    Cells(1, 4) = "Plan BBBBB"
    Cells(1, 5) = "Plan BBBBB Cost"
    Cells(1, 6) = "Plan CCCCC"
    Cells(1, 7) = "Plan CCCCC Cost"
    Cells(1, 8) = "Total"
    
    For i = 1 To lastrow
        currentData = RTrim(LTrim(invoiceData(i, 1)))
        Select Case UCase(Left(currentData, 10))
            Case "INVOICE NO"
                Cells(currentRow, 1).Value = Mid(currentData, InStrRev(currentData, " "))
            Case "PLAN AAAAA"
                Cells(currentRow, 2).Value = Left(currentData, InStrRev(currentData, " ") - 1)
                Cells(currentRow, 3).Value = Mid(currentData, InStrRev(currentData, " "))
            Case "PLAN BBBBB"
                Cells(currentRow, 4).Value = Left(currentData, InStrRev(currentData, " ") - 1)
                Cells(currentRow, 5).Value = Mid(currentData, InStrRev(currentData, " "))
            Case "PLAN CCCCC"
                Cells(currentRow, 6).Value = Left(currentData, InStrRev(currentData, " ") - 1)
                Cells(currentRow, 7).Value = Mid(currentData, InStrRev(currentData, " "))
            Case "TOTAL FOR "
                Cells(currentRow, 8).Value = Mid(currentData, InStrRev(currentData, " "))
                currentRow = currentRow + 1
        End Select
    Next
    End Sub