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在这方面是一个怪胎。