如何拆分逗号分隔值?
How to split comma delimited values?
我有代码可以将多个文本文件导入 Excel,每个文件都在一个新列中。
每列中的导入值以逗号分隔,我想修改代码以将以逗号分隔的值分隔到相邻的列中。
Sub LoopThroughTextFiles()
' Defines variables
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim Text As String
Dim Textline As String
Dim Result() As String
Dim DisplayText As String
Dim LastCol As Long
Dim RowCount As Long
' Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
' Defines LastCol as the last column of data based on row 1
LastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
' Sets the folder containing the text files
myPath = "C:\Users\ashra_ua\OneDrive - Stichting Deltares\Desktop\Excel2" & "\"
' Target File Extension (must include wildcard "*")
myExtension = "*.txt"
' Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
' Loop through each text file in folder
Do While myFile <> ""
' Sets variable "RowCount" To 1
RowCount = 1
' Sets variable "Text" as blank
Text = ""
' Set variable equal to opened text file
Open myPath & myFile For Input As #1
' Do until the last line of the text file
Do Until EOF(1)
' Add each line of the text file to variable "Text"
Line Input #1, Textline
Text = Textline
' Update RowCount row of the current last column with the content of variable "Text"
Cells(RowCount, LastCol).Value = Text
' Increase RowCount by 1
RowCount = RowCount + 1
Loop
' Close the text file
Close #1
' Splitting the results
Result = Split(Text, ",")
For i = LBound(Result()) To UBound(Result())
DisplayText = DisplayText & Result(i)
Next i
' Increase LastCol by 1 to account for the new data
LastCol = LastCol + 2
' Get next text file name
myFile = Dir
Loop
ResetSettings:
' Reset Macro Optimization Settings
Application.EnableEvents = True
Application.ScreenUpdating = True
' Message Box when tasks are completed
MsgBox "Task Complete!"
End Sub
此代码导入所有文本文件,然后在我要用于分隔值的每一列旁边添加一个空白列。
此外,在分离值之后,我想删除所有奇数列。
请尝试下一个简化的循环。它应该做你需要的,而不需要删除任何列。循环之后的任何内容都必须保留在您的代码中:
Do While myFile <> ""
RowCount = 1
Open myPath & myFile For Input As #1
' Do until the last line of the text file
Do Until EOF(1)
Line Input #1, Textline
If UBound(Split(Textline, ",")) = 0 Then
cells(RowCount, LastCol).Value = Textline
Else
cells(RowCount, LastCol).Value = Split(Textline, ",")(1)
End If
RowCount = RowCount + 1
Loop
Close #1
LastCol = LastCol + 1
myFile = Dir
Loop
我有代码可以将多个文本文件导入 Excel,每个文件都在一个新列中。
每列中的导入值以逗号分隔,我想修改代码以将以逗号分隔的值分隔到相邻的列中。
Sub LoopThroughTextFiles()
' Defines variables
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim Text As String
Dim Textline As String
Dim Result() As String
Dim DisplayText As String
Dim LastCol As Long
Dim RowCount As Long
' Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
' Defines LastCol as the last column of data based on row 1
LastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
' Sets the folder containing the text files
myPath = "C:\Users\ashra_ua\OneDrive - Stichting Deltares\Desktop\Excel2" & "\"
' Target File Extension (must include wildcard "*")
myExtension = "*.txt"
' Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
' Loop through each text file in folder
Do While myFile <> ""
' Sets variable "RowCount" To 1
RowCount = 1
' Sets variable "Text" as blank
Text = ""
' Set variable equal to opened text file
Open myPath & myFile For Input As #1
' Do until the last line of the text file
Do Until EOF(1)
' Add each line of the text file to variable "Text"
Line Input #1, Textline
Text = Textline
' Update RowCount row of the current last column with the content of variable "Text"
Cells(RowCount, LastCol).Value = Text
' Increase RowCount by 1
RowCount = RowCount + 1
Loop
' Close the text file
Close #1
' Splitting the results
Result = Split(Text, ",")
For i = LBound(Result()) To UBound(Result())
DisplayText = DisplayText & Result(i)
Next i
' Increase LastCol by 1 to account for the new data
LastCol = LastCol + 2
' Get next text file name
myFile = Dir
Loop
ResetSettings:
' Reset Macro Optimization Settings
Application.EnableEvents = True
Application.ScreenUpdating = True
' Message Box when tasks are completed
MsgBox "Task Complete!"
End Sub
此代码导入所有文本文件,然后在我要用于分隔值的每一列旁边添加一个空白列。
此外,在分离值之后,我想删除所有奇数列。
请尝试下一个简化的循环。它应该做你需要的,而不需要删除任何列。循环之后的任何内容都必须保留在您的代码中:
Do While myFile <> ""
RowCount = 1
Open myPath & myFile For Input As #1
' Do until the last line of the text file
Do Until EOF(1)
Line Input #1, Textline
If UBound(Split(Textline, ",")) = 0 Then
cells(RowCount, LastCol).Value = Textline
Else
cells(RowCount, LastCol).Value = Split(Textline, ",")(1)
End If
RowCount = RowCount + 1
Loop
Close #1
LastCol = LastCol + 1
myFile = Dir
Loop