我有一个看起来像的文本文件:
ROOT: are DOBJ: money DOBJ: deal
DOBJ: products ROOT: count DOBJ: me DOBJ: deal
POBJ: amp ROOT: dads. POBJ: diapers
ROOT: get DOBJ: ecard DOBJ: it
ROOT: hutang
每个单词都由一个标签分隔。该文件有大约50,000行像这样。我希望以这样的方式格式化文件:每行以ROOT开头,后跟DOBJ,然后是POBJ。每行只有一个ROOT,DOBJ / POBJ的数量是未知的,可以在0-5之间变化。我试图将文件导入excel表并尝试执行HLOOKUP但我没有得到我想要的东西。我想在VBA中编写以下逻辑(我之前从未使用过VBA):
Dim sh As Worksheet
Dim rw As Range
Dim RowCount As Integer
Set sh = ActiveSheet
For Each rw In sh.Rows
if cellnumber(ROOT) != A
swap content(A), content of cell containing ROOT
有人可以帮我解决这个问题,或者告诉我是否有更好的方法可以使用Python或Excel?
答案 0 :(得分:0)
在VBA中,这应该可以帮助您入门。
Sub test()
Dim InputString As String
Dim RowToPerform As Long
Dim TextPath As Variant
Dim ArrayString() As String
Dim CounterArray As Long
TextPath = Application.GetOpenFilename("Txt Files,*.txt", Title:="Select Txt")
Open TextPath For Input As #1
Do Until EOF(1)
Line Input #1, InputString
RowToPerform = Cells(Rows.Count, 1).End(xlUp).Row + 1
'ArrayString = Split(InputString, "|")
ArrayString = Split(InputString, "ROOT:")
Cells(RowToPerform, 1).Value = "ROOT: "
Cells(RowToPerform - 1, 1).Value = Cells(RowToPerform - 1, 1).Value & ArrayString(0)
For CounterArray = LBound(ArrayString) To UBound(ArrayString)
If CounterArray > 0 Then Cells(RowToPerform, 1).Value = Cells(RowToPerform, 1).Value & ArrayString(CounterArray)
Next CounterArray
Loop
Close #1
End Sub