我有一个文本数据集,需要重新格式化后才能使用。目前,这是我已导入Excel的文本文件。每条记录当前跨越三行,但在一列中。我需要对其进行转换,因此它是一行三列。
下面的示例是我的数据当前的结构。它显示了2,000多个记录中的三个记录。“行”列仅供参考,实际上不在我的数据中。
Row | Column
1 | File Number: 001
2 | File Code: ABC
3 | File Description: Text file
4 | File Number: 002
5 | File Code: DEF
6 | File Description: Text file
7 | File Number: 003
8 | File Code: GHI
9 | File Description: Text file
为了澄清起见,第1到3行将是一个记录。第4至6行将是第二条记录。第三条记录是从第7到9行。我的数据中的每条记录当前都分为三行。
我想重新格式化它,所以它看起来像这样:
Row | File Number | File Code | File Description
1 | 001 | ABC | Text
2 | 002 | DEF | Text
3 | 003 | GHI | Text
同样,该行列仅供参考,在重新格式化的数据中我不需要它。复制和粘贴似乎不是一个好的选择。
有没有一种快速的方法可以改变这种情况?
您可以使用VBA来执行此操作。这样的代码可能会在这种情况下为您提供帮助。
Option Explicit
Sub Test()
' Let's make the tabular structure in column C, D and E
' C D E
' File Number Code Description
Dim CurrentRow As Integer
CurrentRow = 2 ' Read from A2
Dim WriteRow As Integer
WriteRow = 2 ' Write to C2
Do
' if we see empty data in column A, then we are done with our work
If Len(Trim(Range("A" & CurrentRow))) = 0 Then Exit Do
' make 3 rows of data into 3 columns in a single row
Range("C" & WriteRow).Value = Trim(Replace(Range("A" & CurrentRow).Text, "File Number:", ""))
Range("D" & WriteRow).Value = Trim(Replace(Range("A" & CurrentRow + 1).Text, "File Code:", ""))
Range("E" & WriteRow).Value = Trim(Replace(Range("A" & CurrentRow + 2).Text, "File Description:", ""))
' increment our reading and writing markers
CurrentRow = CurrentRow + 3
WriteRow = WriteRow + 1
Loop
End Sub
随时进行测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句