Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 1004运行时错误_Excel - Fatal编程技术网

Excel 1004运行时错误

Excel 1004运行时错误,excel,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.

我知道这个问题以前已经被回答过很多次了,但我就是不知道是什么错了

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)时,选择
I没有问题。但是当我替换
单元格(1,1)
的变量时,我得到一个错误。有人知道为什么吗?

您是从
单元格(0,0)
开始的,而这并不存在。从1开始Y和X。你还需要在循环中将X重置为1,而不是0。

有趣的是,实际上有一个dup答案:生活有趣的另一个原因。为什么不能基于0进行excel。可以改用
Offset()
。。但是,是的,Excel在这方面是一个怪胎。