iT邦幫忙

0

vba 陣列索引超出範圍

原本要輸出的資料有輸出到.csv檔,但電腦卻show 這個訊息?
請問有哪位高手知道是哪個部份的問題?感激不盡

outFilePath = "C:\temp\myFileUP.txt"

Open outFilePath For Output As #2

For nRow = LBound(dataArray) To UBound(dataArray)
    arrLine = Split(dataArray(nRow), ",")
    
    oneLine = arrLine(LBound(arrLine))  'The first column
    
    For nCol = LBound(arrLine) + 1 To UBound(arrLine)
        oneLine = oneLine & "," & Range("A1").Offset(nRow, nCol).Value
    Next
    
    Print #2, oneLine
Next nRow

Close #2

End Sub

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-11-26 10:57:43
最佳解答

將 arrLine 的索引值範圍顯示出來
就可以自己查原因

For nRow = LBound(dataArray) To UBound(dataArray)
    Debug.Print nRow & " " & dataArray(nRow)   '顯示每列文字
    
    arrLine = Split(dataArray(nRow), ",")
    
    oneLine = arrLine(LBound(arrLine))  'The first column
    Debug.Print nRow & " " & LBound(arrLine) & ":" & UBound(arrLine) & " " & oneLine   '顯示 arrLine 的 索引值範圍 及 第一欄文字
    
    For nCol = LBound(arrLine) + 1 To UBound(arrLine)
        oneLine = oneLine & "," & Range("A1").Offset(nRow, nCol).Value
    Next
    
    Print #2, oneLine
Next nRow

有您真好/images/emoticon/emoticon51.gif

For nRow = LBound(dataArray) To UBound(dataArray)
    arrLine = Split(dataArray(nRow), ",")
    
    if UBound(arrLine) = -1 Then   '空陣列,即是空白列,不用處理
        oneLine = ""
    Else                 '不是空陣列,進行原本的處理
        oneLine = arrLine(LBound(arrLine))  'The first column
    
        For nCol = LBound(arrLine) + 1 To UBound(arrLine)
            oneLine = oneLine & "," & Range("A1").Offset(nRow, nCol).Value
        Next
    End If
    
    Print #2, oneLine
Next nRow

謝大神大大關照/images/emoticon/emoticon41.gif

我要發表回答

立即登入回答