下面是從文本文件中複製數據並將其粘貼到Excel中的代碼。我已將文字分割爲vbNewline
,但我還需要按空格分割。將文本文件中的數據複製到Excel中
有沒有其他方法可以通過空間分割數據,以及如何通過分割數據vbNewLine
和空間?
On Error Resume Next
Dim objFSO, strTextFile, strData, strLine, arrLines
Const ForReading = 1
path = InputBox("Enter the path :","Select Your Path !","Type your path here")
'name of the text file
strTextFile = path
'Create a File System Object
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(strTextFile, ForReading).ReadAll
'typesplit=inputbox("Enter the type ","Type to split(VbnewLine (or) <Space>)","vbnewline/space")
'Split the text file into lines
arrLines = Split(strData, vbNewLine)
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2
k = 0
align = InputBox("Enter the type of alignment...(default/horizontal/vertical)", _
"Press h (or) v (or) d")
If (align = "v") Then
rowlimit = InputBox("Enter the row limit")
For i=1 To 10 Step 1
For j=1 To rowlimit Step 1
objExcel.Cells(j, i).Value = arrLines(k)
k = k+1
Next
Next
End If
If (align = "h") Then
collimit = InputBox("Enter the col limit")
For i=1 To 10 Step 1
For j=1 To collimit Step 1
objExcel.Cells(i, j).Value = arrLines(k)
k = k+1
Next
Next
End If
MsgBox("Conversion Finished !!")
Set objFSO = Nothing
謝謝Ansgar Wiechers –
爲什麼用戶必須輸入行限制和列限制等內容?難道不能從數據中推斷出來嗎?您可以考慮在文本文件中用逗號替換空格並將其保存爲csv文件 - Excel知道如何打開(併爲其他對齊運行轉置)。另外,請看看文本到列如何在Excel中工作。 –
您也可以嘗試自動化[文本到列](https://msdn.microsoft.com/en-us/library/office/ff193593.aspx)功能 –