2014-09-30 58 views
-1

我有一個文本文件,我試圖用excel VB來計算文件第一行中逗號的數量,然後在有3個時執行一個動作 - 但出了問題。當我使用替換法(在下面這個例子中註釋掉)宏失敗,當我使用它總是這樣操作的拆分方法,無論什麼樣的價值我想補充到位的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

.value似乎是問題所在。 VBA沒有與.NET相同的屬性。

更換你註釋掉了如果與這樣的說法:

If Len(dataArray(0)) - Len(Replace(dataArray(0), ",", "")) = 3 Then 
+0

謝謝 - 我也嘗試沒有.Values並沒有奏效 - 然後我終於意識到,我有一個名爲子「替換」在我的模塊之一 - 不知道如何,但這顯然搞砸了東西 - 當我重命名,一切正常。 – user2725402 2014-10-02 05:27:47