在文本文件中计算逗号

时间:2014-09-30 09:10:15

标签: excel-vba vba excel

我有一个文本文件,我正在尝试使用excel VB计算文件第一行中的逗号数,然后如果有3则执行操作 - 但是有些错误。当我使用替换方法(在下面的这个例子中注释掉)时,宏失败,当我使用Split方法时,它总是执行操作,无论我添加什么值代替3。

   'Load txt file into array
    Open FilePath For Input As #1
    dataArray = Split(Input$(LOF(1), #1), vbLf)
    Close #1

    'Test first line if it has three commas
    If Len(dataArray(0).value) - Len(Replace(dataArray(0).value, ",", "")) = 3 Then
    'If dataArray(0).Split(",").Length = 3 Then

   'Add comma to start of strings
        For i = LBound(dataArray) To UBound(dataArray)

            dataArray(i) = "," & dataArray(i)
        Next i

1 个答案:

答案 0 :(得分:1)

.value似乎是个问题。 VBA没有与.NET相同的属性。

用以下内容替换未注释的IF语句:

If Len(dataArray(0)) - Len(Replace(dataArray(0), ",", "")) = 3 Then
相关问题