(EXCEL VBA) 如何从 txt 输出中删除逗号和空格?
(EXCEL VBA) How to remove commas and spaces from txt output?
我有一个 excel 文件,如下所示:
3001 T81 90300010 001
3001 T81 90300011 001
问题是有些数字是存储为 txt 的数字。
还有字符串:"T81"
我需要输出的 txt 文件如下所示:
3001T8190300010001
没有空格、引号等
我能够运行这个脚本来完成大部分的跑腿工作:
Dim myFile As String, rng As Range, cellValue As Variant, I As Integer, j As Integer
myFile = Application.DefaultFilePath & "\test_data_output.txt"
Set rng = Selection
Open myFile For Output As #1
For I = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(I, j).Value
If j = rng.Columns.Count Then
Write #1, cellValue
Else
Write #1, cellValue,
End If
Next j
Next I
Close #1
现在它的输出不是很完美:
3001,"T81",90300010,"001"
3001,"T81","90300011","001"
在设置 cellValue 的行中,继续将字符串相加(将值转换为字符串):
cellValue = cellValue + Cstr(rng.Cells(I, j).Value)
然后在你的下一个 j 和下一个 I 之间,重置 cellValue:
Next j
cellValue = ""
Next I
Dim v as Variant
v = rng.Value
For i = LBound(v,1) to UBound(v,1)
Write #1, Replace(Replace(Join(WorksheetFunction.Index(v,i,0),""),",","")," ","")
Next
我有一个 excel 文件,如下所示:
3001 T81 90300010 001
3001 T81 90300011 001
问题是有些数字是存储为 txt 的数字。
还有字符串:"T81"
我需要输出的 txt 文件如下所示:
3001T8190300010001
没有空格、引号等
我能够运行这个脚本来完成大部分的跑腿工作:
Dim myFile As String, rng As Range, cellValue As Variant, I As Integer, j As Integer
myFile = Application.DefaultFilePath & "\test_data_output.txt"
Set rng = Selection
Open myFile For Output As #1
For I = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(I, j).Value
If j = rng.Columns.Count Then
Write #1, cellValue
Else
Write #1, cellValue,
End If
Next j
Next I
Close #1
现在它的输出不是很完美:
3001,"T81",90300010,"001"
3001,"T81","90300011","001"
在设置 cellValue 的行中,继续将字符串相加(将值转换为字符串):
cellValue = cellValue + Cstr(rng.Cells(I, j).Value)
然后在你的下一个 j 和下一个 I 之间,重置 cellValue:
Next j
cellValue = ""
Next I
Dim v as Variant
v = rng.Value
For i = LBound(v,1) to UBound(v,1)
Write #1, Replace(Replace(Join(WorksheetFunction.Index(v,i,0),""),",","")," ","")
Next