Excel中是否有可以将此银行对账单信息解析为5列的公式?
我肯定有人已经弄明白了,有人知道一种有效的方法来将银行对账单数据解析为5列(PostDate、TransDate、Description、Reference、Amount)Excel中是否有可以将此银行对账单信息解析为5列的公式?,excel,parsing,Excel,Parsing,我肯定有人已经弄明白了,有人知道一种有效的方法来将银行对账单数据解析为5列(PostDate、TransDate、Description、Reference、Amount) Shane如果你想学习vba,我为你写了一些代码。如果将光标放在电子表格中的一行上,它将为您粘贴值。如果您有任何问题,请告诉我们 Sub SplitTransaction() Dim trans As String Dim lastSpace As Integer Dim PostDate, TransDate, Desc
Shane如果你想学习vba,我为你写了一些代码。如果将光标放在电子表格中的一行上,它将为您粘贴值。如果您有任何问题,请告诉我们
Sub SplitTransaction()
Dim trans As String
Dim lastSpace As Integer
Dim PostDate, TransDate, Description, Reference, Amount
Dim rng As Range
Set rng = ActiveSheet.Range(ActiveCell.Address & ":" & ActiveCell.End(xlDown).Address)
For Each cell In rng
'set the transactiond data to the active cell
trans = cell.Value
'parse out the post date and trim off
PostDate = Left(trans, 5)
trans = Mid(trans, 7)
'parse out the trans date and trim off
TransDate = Left(trans, 5)
trans = Mid(trans, 7)
'get the amount and trim off
lastSpace = InStrRev(trans, " ") + 1
Amount = Mid(trans, lastSpace)
trans = Left(trans, lastSpace - 2)
'check for a negative and apply and trim
If Right(trans, 1) = "-" Then
Amount = Amount * -1
trans = Left(trans, Len(trans) - 2)
End If
'get the reference, if the reference is missing then exclude
lastSpace = InStrRev(trans, " ") + 1
Reference = Mid(trans, lastSpace)
If IsNumeric(Reference) And Len(Reference) > 10 Then 'we have a valid reference
Description = Left(trans, lastSpace - 2)
Else
Reference = ""
Description = trans
End If
'PostDate, TransDate, Description, Reference, Amount
'paste the values
cell.Offset(, 1).Value = PostDate
cell.Offset(, 2).Value = TransDate
cell.Offset(, 3).Value = Description
cell.Offset(, 4).NumberFormat = "@"
cell.Offset(, 4).Value = Reference
cell.Offset(, 5).Value = Amount
Next cell
End Sub
这可以在Power Query(获取和转换)中轻松完成,无需编写任何代码。使用多个单独的功能区命令按前两列的空间分隔符分割数据。由于数据格式不幸地用空格分隔负号,因此需要使用一些IF条件来计算数值,但所有这些都可以通过功能区命令和简单公式来完成 它是可重复的,数据可以直接从CSV文件加载
Power Query的妙处在于,如果需要更改,很容易理解应用了哪些步骤。VBA代码要复杂得多,也更难维护。您知道实际数据中是否只有空格,或者可能是制表符分隔的吗?如果是,您可以使用导入功能解析它,如果不是,您可能需要编写一个脚本来解析它。感谢您的回复Kevin。。。是的,它有空格,但由于列之间有空格,而且在描述和引用列中也有空格,我不能单独使用。对,这就是问题所在。你有编写vba代码的经验吗?这可以很容易地完成。你知道一个简单的函数来抓取正确的文本直到第一个空格吗?(除非下一个字符是一个连字符,表示负数,并且在负数和连字符之间有一个空格。你知道怎么做吗?不幸的是,不精通VBA。这里是新手。但是,我想我会没事的。我可以想出如何抓住右边的空格,然后抽查负数。这仍然节省了我一个小时s!哇,凯文!我不知道该说什么,非常感谢你的时间和努力。而且,现在你给了我需要深入了解VBA一点的提示。这太酷了……西雅图凌晨4点,我还在努力,我知道这段代码会节省我很多时间,谢谢,谢谢!顺便说一句-它工作得很好!甚至没有一个错误尽管我在处理看似完全不同的数据。令人惊讶的工作,非常干净,再次感谢你!而且,在我弄明白如何进行循环之前,请检查我的黑客行为……我创建了一个巨大的按钮,这样我就可以下箭头、单击、下箭头、单击、冲洗并重复。我不知道如何使Excel模块“激活”,以便我只需在字段中单击即可。如果你可以让我知道,否则我现在就用按钮。你可以做几件事。这取决于你处理的数据。我将添加一些代码,告诉你如何从当前单元格开始,直到遇到空白单元格为止。干得好(如果你喜欢,也请接受这个答案)!谢谢Teylyn,我一定也会看一看。谢天谢地,Kevin已经完成VBA,所以这可能会得到解决,但我喜欢直接从CSV文件加载的想法。谢谢,我会查看此建议,我感谢您的回复-Shane@teylyn-完成事情的方法总是不止一种。这可能也可以只使用公式,但我会发现这很难控制。我喜欢VBA方法,它可以很容易地从CSV读取。@Kevin你听起来有点防御性。我只是指出编写代码的替代方法。很多人要求或编写VBA,因为他们不知道Power Query只需单击几下就可以做什么。
Sub SplitTransaction()
Dim trans As String
Dim lastSpace As Integer
Dim PostDate, TransDate, Description, Reference, Amount
Dim rng As Range
Set rng = ActiveSheet.Range(ActiveCell.Address & ":" & ActiveCell.End(xlDown).Address)
For Each cell In rng
'set the transactiond data to the active cell
trans = cell.Value
'parse out the post date and trim off
PostDate = Left(trans, 5)
trans = Mid(trans, 7)
'parse out the trans date and trim off
TransDate = Left(trans, 5)
trans = Mid(trans, 7)
'get the amount and trim off
lastSpace = InStrRev(trans, " ") + 1
Amount = Mid(trans, lastSpace)
trans = Left(trans, lastSpace - 2)
'check for a negative and apply and trim
If Right(trans, 1) = "-" Then
Amount = Amount * -1
trans = Left(trans, Len(trans) - 2)
End If
'get the reference, if the reference is missing then exclude
lastSpace = InStrRev(trans, " ") + 1
Reference = Mid(trans, lastSpace)
If IsNumeric(Reference) And Len(Reference) > 10 Then 'we have a valid reference
Description = Left(trans, lastSpace - 2)
Else
Reference = ""
Description = trans
End If
'PostDate, TransDate, Description, Reference, Amount
'paste the values
cell.Offset(, 1).Value = PostDate
cell.Offset(, 2).Value = TransDate
cell.Offset(, 3).Value = Description
cell.Offset(, 4).NumberFormat = "@"
cell.Offset(, 4).Value = Reference
cell.Offset(, 5).Value = Amount
Next cell
End Sub