Excel 1004运行时错误

我知道这个问题以前已经回答过很多次了,但是我却弄不清楚什么是错的。

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 。select我没有问题。 但是,当我replaceCells(1, 1)的variablesCells(1, 1)我得到一个错误。 有人知道为什么?

你从Cells(0, 0) ,这个不存在。 将Y_和X_开始于1.您还需要将循环中的X_重置为1,而不是0。