0
我有一個腳本來將文本文件導入到Excel中。使用製表符作爲分隔符,並在一個空行開始一個新的工作表
目標是使用Tab作爲分隔符,並在一個空行開始一個新的工作表。
問題當前腳本爲每一行開始新的工作表。
我是否需要用不同的方式來解釋空行?解釋和處理工作表的其他嘗試會成功更改,從導入數據中刪除空格,然後刪除分隔符無效。
Public Sub ImportTextFile(FName As String, Sep As String)
Dim RowNdx As Long
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer
Dim WS As Worksheet
Dim SheetNumber As Long
Const C_START_SHEET_NAME = "Sheet1"
SheetNumber = 1
RowNdx = C_START_ROW_FIRST_PAGE
Set WS = ActiveWorkbook.Worksheets(C_START_SHEET_NAME)
Application.ScreenUpdating = False
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
Open FName For Input Access Read As #1
While Not EOF(1)
Line Input #1, WholeLine
'This section added to create new sheets for empty lines
If InputLine = "" Then
SheetNumber = SheetNumber + 1
Set WS = ActiveWorkbook.Worksheets.Add(after:=WS)
RowNdx = 1
End If
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
SheetNumber = SheetNumber + 1
Wend
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1
End Sub