我在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
我需要将这些数据显示在单独的工作表上(甚至替换当前工作表),如下所示:
Invoice No | Plan AAAAA | Plan AAAAA Cost | Plan BBBBB | Plan BBBBB Cost | Plan CCCCC | Plan CCCCC Cost | Total
1000001 | Plan AAAAA 17371 | 22.00 | Plan BBBBB 31782 | 0.00 | Plan CCCCC 13918 | 44.00 | 66.00
1000002 | Plan AAAAA 31385 | 0.00 | | | Plan CCCCC 15981 | 44.00 | 44.00
1000003 | | | Plan BBBBB 13181 | 0.00 | Plan CCCCC 01828 | 16.00 | 16.00
需要注意的一些事项是:
INVOICE NO. XXXXXXX
开头,以Total for XXXXXXX
结尾,后跟其总费用。我正在考虑创建一个Excel宏,它将遍历Excel工作表的每一行,伪代码
INVOICE NO
的第一个实例,并将以下编号添加到表格Total for
,请检查是否包含Plan AAAAA
,如果是,请在将最后一部分拆分为成本时添加行,如果不是留下黑色,请保持在线并检查对于计划BBBBB,重复BBBBB和CCCCC 如何循环执行上述操作的每一行(或者有更好的方法来完成此操作)?
答案 0 :(得分:2)
正如代码中的注释所写,我假设您的数据存储在以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
答案 1 :(得分:1)
代码不是那么灵活,但它的工作原理基于以上标准:
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
答案 2 :(得分:0)
您可以在此处使用For-Each循环。它已经过去了。像这样:
function getCookie(name)
{
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}
如果您按照自己的伪代码中的说明执行此操作,我认为没有理由不应该这样做。但是,这种看起来像是上班的问题;)