Excel 1004运行时错误

时间:2014-05-19 20:50:57

标签: excel

我知道这个问题之前已经多次回答,但我无法弄清楚出了什么问题。

Option Explicit
Private Sub CommandButton1_Click()

    Dim FileNum As Integer ' file num
    Dim DataLine As String ' line of file

    Dim entry() As String ' split line

    Sheets("Sheet1").Select
    ActiveSheet.Unprotect

    Dim X_ As Integer ' cell x
    X_ = 0
    Dim Y_ As Integer ' cell y
    Y_ = 0


    Dim s As Variant

    FileNum = FreeFile() ' create a free file
    Open "C:\Users\vittorio\PycharmProjects\Fiddleheads\data.csv" For Input As #FileNum ' open the file
    While Not EOF(FileNum)
        Line Input #FileNum, DataLine ' read in data 1 line at a time
        'MsgBox (DataLine)
        entry = Split(DataLine, ",")

        For Each s In entry
            ActiveSheet.Cells(X_, Y_).Select
            X_ = X_ + 1
        Next s
        X_ = 0
        Y_ = Y_ + 1
    Wend

End Sub

使用ActiveSheet.Cells(1, 1).Select时我没有问题。但是当我为Cells(1, 1)替换变量时,我得到一个错误。有谁知道为什么?

1 个答案:

答案 0 :(得分:2)

您从Cells(0, 0)开始,但这不存在。在1开始Y_和X_。您还需要在循环内将X_重置为1,而不是0。