我在.txt文件中獲得了多個不以任何分隔符分隔的數據。它只是一串字符串。到目前爲止,我做到了這一點,但我現在卡住了。在這之後我應該在哪裏繼續?以下是我迄今爲止所做的示例。有人能澄清我下一步該做什麼嗎?如何將不帶分隔符的.txt文件轉換爲Excel
Dim objFSO
Dim TextFile
Dim TextRead
Dim Line, Line1, Line2, Line3
Dim Count
Const ForReading = 1 'Constant declared so that can be used throughout the script
'Name of the text file that need to be convert
TextFile = "C:\Documents and Settings\Administrator\Desktop\2_12_2015\HRILOANDIC20170601.txt"
'Create File system object
set objFSO = CreateObject("Scripting.FileSystemObject")
'set the text file to read and open it in read-only mode
set TextRead = objFSO.OpenTextFile(TextFile,ForReading)
Do Until TextRead.AtEndOfStream
Line = TextRead.ReadLine
If Left(Line, 1) = "H" Then
HeaderRecordType = Mid(Line, 1, 1)
HeaderSequenceNo = Mid(Line, 2, 9)
HeaderContractNo = Mid(Line, 11, 19)
HeaderCreationBy = Mid(Line, 30, 1)
HeaderTransactionDate = Mid(Line, 31, 8)
HeaderTotalRecord = Mid(Line, 39, 9)
HeaderTotalAmount = Mid(Line, 48, 17)
HeaderSource = Mid(Line, 65, 2)
HeaderFiller = Mid(Line, 67, 334)
ElseIf Left(Line, 1) = "D" Then
DetailRecordType = Mid(Line, 1, 1)
DetailSequenceNo = Mid(Line, 2, 9)
DetailContractNo = Mid(Line, 11, 19)
DetailPaymentType = Mid(Line, 30, 10)
DetailSettlementType = Mid(Line, 40, 1)
DetailEffectiveDate = Mid(Line, 41, 8)
DetailCreditAccountNo = Mid(Line, 49, 19)
DetailCreditAccountType = Mid(Line, 68, 1)
DetailCrTransactionAmount = Mid(Line, 69, 17)
DetailLoanType = Mid(Line, 86, 10)
DetailBankEmployeeID = Mid(Line, 96, 40)
DetailIDNumber = Mid(Line, 136, 40)
DetailIDTypeCode = Mid(Line, 176, 3)
DetailBankEmployeeName = Mid(Line, 179, 200)
DetailHRISProcessStatus = Mid(Line, 379, 1)
DetailCIFnumber = Mid(Line, 380, 19)
DetailAccountBranch = Mid(Line, 399, 5)
ElseIf Left(Line, 1) = "T" Then
TrailerRecordType = Mid(Line, 1, 1)
TrailerSequenceNo = Mid(Line, 2, 9)
TrailerTotalRecord = Mid(Line, 30, 9)
TrailerTotalAmount = Mid(Line, 39, 17)
TrailerFiller = Mid(Line, 56, 345)
Else
'Error Handling
End If
Loop
ExcelFilePath = "C:\Documents and Settings\Administrator\Desktop\2_12_2015\Output.xlsx"
'Open the spreadsheet using the excel application object
Set objExcel = CreateObject("Excel.Application")
'ExcelObject.WorkBooks.Open ExcelObject
Set SheetObject = ExcelObject.ActiveWorkbook.Worksheets(1)
'Modify data in a cell (in this case we are adding data to c2)
'First value in the brackets = Column number
'2nd value = Cell number
SheetObject.Cells(3, 2).Value = "Test"
'Save and quit
ExcelObject.ActiveWorkbook.Save
ExcelObject.ActiveWorkbook.Close
ExcelObject.Application.Quit
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2
objExcel.Cells(1, 1).Value="?" 'Name of a column
一些建議 - 1)將字符串文件中的相同位置分配給一個變量即 - (RecordType = Mid(Line,1,1)'2)創建你的excel對象,並將表單設置爲Do循環3)在設置Do循環內的所有變量後,將它們寫入Excel列。 –
你只是在尋找如何將它們寫入電子表格? – MatthewD