使用VBA从文本文件中的行尾删除逗号

时间:2015-06-29 15:57:31

标签: vba csv

我编写了一个VBA代码,用于将逗号分隔的数据元素输出到文本文件,然后将其导入到单独的应用程序中。但是,我被困在如何删除文本文件中生成的行尾逗号 以下是我的数据示例:

Scenario,Capital,AFE
Capital Array,Drilling Capex 80/20,Replace,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

Capital Array,Facilities Tangible,Replace,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,250,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

Capital Array,Recompletion 80/20,Replace,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

2 个答案:

答案 0 :(得分:4)

最好的解决方案是不首先添加尾随逗号。为此,您需要修改未向我们展示的代码。

或者,如果CSV创建代码是您不想触摸的黑盒子,您可以手动删除字符串中的最后一个字符:

myString = "a,b,c,d,"

If Right(myString, 1) = "," Then
    myString = Left(myString, Len(myString) - 1)
End If

' myString is now "a,b,c,d"

答案 1 :(得分:2)

如上所述计算字符串后,使用此字符删除最后一个字符:myString = Left(myString, Len(myString) - 1)其中myString是字符串的名称。如果您不知道每次最后一个char是否都是逗号,请首先使用if语句检查Right(1,myString)是否为逗号。