我正在尝试在 Excel 中格式化一个大文本文件。它看起来与此类似:
Headings
----------
Data
Data
Data
ENDROW
Other Information
我想选择 ---------- 和 ENDROW 之间的单元格,然后在所选单元格上使用文本到列功能。我为文本到列位记录了一个宏,最终我想用它来循环整个文档。
Sub findRange()
Dim nRow As Long
Dim nStart As Long, nEnd As Long
' Figure out where the range should start.
For nRow = 1 To 65536
If Range("A" & nRow).Value = "----------" Then
nStart = nRow
Exit For
End If
Next nRow
' Figure out where the range should end.
For nRow = nStart To 65536
If Range("A" & nRow).Value = "ENDROW" Then
nEnd = nRow
Exit For
End If
Next nRow
nEnd = nEnd - 1
Range("A" & nStart & ":D" & nEnd).Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(16, 1), Array(21, 1), Array(27, 1), Array(56, 1), _
Array(59, 1), Array(60, 1), Array(73, 1)), TrailingMinusNumbers:=True
Selection.ColumnWidth = 16.33
Range("B:B,F:F,D:D,H:H").Select
Range("H1").Activate
Selection.Delete Shift:=xlToLeft
End Sub
它出错了,任何输入都会受到赞赏。我是一个新手,试图学习我的方法。