这是我第一次在VBA中编码任何内容-我正在尝试编写一个简短的宏,该宏读取文件并将位和片段分成西列或东列。
所有这些都可能是错误的,也可能只是简单的事情,但此刻甚至无法识别我的do循环。任何帮助深表感谢。
Private Sub seperateTextFile()
Dim file As String
Dim text As String
Dim textLine As String
Dim west As Boolean
west = True
file = ".alltxt"
Open file For Input As #1
Do Until EOF(1)
Line Input #1, textLine
text = textLine
If InStr(text, "HMW") <> 0 Then
west = True
If InStr(text, "other") <> 0 Then
west = False
If west = True Then
Sheets("Sheet2").Range("West").End(xlUp) = text
If west = False Then
Sheets("Sheet2").Range("East").End(xlUp) = text
Loop
Close #1
End Sub
我认为您想念自己的elesif
地方了。你需要一个End if
Dim file As String
Dim text As String
Dim textLine As String
Dim west As Boolean
west = True
file = "c:\temp\a.alltxt"
Open file For Input As #1
Do Until EOF(1)
Line Input #1, textLine
text = textLine
If InStr(text, "HMW") <> 0 Then
west = True
ElseIf InStr(text, "other") <> 0 Then
west = False
ElseIf west = True Then
'Sheets("Sheet2").Range("West").End(xlUp) = text
ElseIf west = False Then
'Sheets("Sheet2").Range("East").End(xlUp) = text
End If
Loop
Close #1
而file = ".alltxt"
对我来说,您需要有一个实际的路径和名称,例如file = "c:\temp\a.alltxt"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句