我一直在尝试编写一个打开文本文件的程序,并在以C_PIN
开头的每一行的第5,6和7 字后插入空格。
我觉得我几乎已经完成但是我得到了运行时错误5和438.
正在阅读的示例文本:
COMP C48 66250110810 cap sc_cap.0603_H9 43.3959 74.3331 1 0
C_PROP(PB_FREE,“Y”)(VALUE,“10nF”)(TOLER,“10%”)(PART_NAME,“06035C103K4T2A”)
C_PIN C48-1 43.3959 75.0951 1 1 0 sp.0603_H9.1 / N $ 1567
C_PIN C48-2 43.3959 73.5711 1 1 0 sp.0603_H9.2 GN
1 1 0
以下是我认为代码中的问题所在:
x = " "
Do While Not EOF(infilenum%)
Line Input #infilenum%, a$
If Left$(a$, 5) = "C_PIN" Then
For Each x In InStr
'If InStr(strText, " ") Then
w = w + 1
'w = strText.Split
For w = 5 To w = 7
My.Computer.FileSystem.WriteAllText (infilename$)
strText = My.Computer.FileSystem.ReadAllText(infilename$).Replace(w, x + w)
vb.newline
非常感谢任何帮助!
答案 0 :(得分:3)
For Each x In InStr
无论如何都无效!!!
您可以使用拆分和加入:
If Left(a, 5) = "C_PIN" Then
va = Split(a, " ")
va(4) = va(4) & " " 'Add extra space
va(5) = va(4) & " "
va(6) = va(4) & " "
a = Join(va, " ") 'Join with extra spaces added
End If
现在你可以写字符串了。
答案 1 :(得分:1)
我玩了一个替代版本,使用regexp进行单次替换
虽然它看起来有点复杂,但除了单次替换之外,加号是仅会改变
行C_PIN
此示例将获取您的初始文件,并使用填充间距保存第二个版本。
根据其他要求提升,使用两个单独的正则表达式替换
Sub ReDo()
Dim objFso As Object
Dim objFil As Object
Dim objFil2 As Object
Dim objRegex As Object
Dim strFile As String
Dim strAll As String
strFil = "c:\temp\REnglish.txt"
strFil2 = "c:\temp\REnglish2.txt"
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "(\nC_PIN\s)((\b[^\s]+\b\s){3})(\b[^\s]+\b\s)(\b[^\s]+\b\s)(\b\d\b\s)"
.Global = True
Set objFil = objFso.OpenTextFile(strFil)
strAll = objFil.ReadAll
Set objFil2 = objFso.createtextfile(strFil2)
strAll = .Replace(strAll, "$1$2$3$4 $5 $6 ")
.Pattern = "(\nC_PIN\s)((\b[^\s]+\b\s){3})(\b[^\s]+\b\s)(\b[^\s]+\b\s)(\b\d{2,}\b\s)"
objFil2.write .Replace(strAll, "$1$2$3$4 $5 $6")
End With
objFil.Close
objFil2.Close
End Sub