Excel 使用VBA插入文本顶部的x行
我想插入文本文件中顶部的x行。我可以给出StartRow的编号,但是否有这样的参数,我可以给出“EndRow”编号,以给出顶部x行我想要插入的内容Excel 使用VBA插入文本顶部的x行,excel,text,insert,vba,Excel,Text,Insert,Vba,我想插入文本文件中顶部的x行。我可以给出StartRow的编号,但是否有这样的参数,我可以给出“EndRow”编号,以给出顶部x行我想要插入的内容 Sub insertTopX() With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\Users\HarrsionDavid\Desktop\AnswerForEveryQuestions" _ ,Destination:=Cells(1,1))
Sub insertTopX()
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\HarrsionDavid\Desktop\AnswerForEveryQuestions" _
,Destination:=Cells(1,1))
.Name = "test_file.txt"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
您可以读取txt文件并将其按新行拆分。然后您将拥有一个易于使用的数组 示例文件:
- 读取文件并将其解析为变量李>
- 传递
和startRow
李>endRow
结果是:
这是
ReadFileLineByLineOstring
:
Public Function ReadFileLineByLineToString(path As String) As String
Dim fileNo As Long
fileNo = FreeFile
Open path For Input As #fileNo
Do While Not EOF(fileNo)
Dim textRowInput As String
Line Input #fileNo, textRowInput
ReadFileLineByLineToString = ReadFileLineByLineToString & textRowInput
If Not EOF(fileNo) Then
ReadFileLineByLineToString = ReadFileLineByLineToString & vbCrLf
End If
Loop
Close #fileNo
End Function
打错了,已经更正了。谢谢
Public Function ReadFileLineByLineToString(path As String) As String
Dim fileNo As Long
fileNo = FreeFile
Open path For Input As #fileNo
Do While Not EOF(fileNo)
Dim textRowInput As String
Line Input #fileNo, textRowInput
ReadFileLineByLineToString = ReadFileLineByLineToString & textRowInput
If Not EOF(fileNo) Then
ReadFileLineByLineToString = ReadFileLineByLineToString & vbCrLf
End If
Loop
Close #fileNo
End Function