我的代码应该打开数据的文本文件,从中记录某些值,记录它们,关闭它,然后打开下一个文件。到目前为止,每年每天每小时都有一个文件,为了跟踪这些工作表,我一直在重命名它们。
我在5月底遇到错误1004"方法'添加'对象'表格'失败",即使它已经工作了大约7000次。
有问题的一行是:
Sheets.Add(Before:=Sheets(1)).Name = lm & " " & j & " Hour " & kk
这里可能出现什么问题?我是VBA的新手,所以它可能很容易成为我忽略的东西,但为什么只是现在它才会被淘汰?
(最后一个半相关的说明:刚刚报名参加专门问这个问题;如果我发布了任何不正确或格式错误,我道歉!)
编辑:如果它有帮助,那么该代码的代码是:
For i = 1 To 12 'for all months
For j = 1 To 31 'for all days
For k = 0 To 23 'for all hours
Select Case i
Case "1"
mmm = "Jan"
lm = "January"
Case "2"
mmm = "Feb"
lm = "February"
Case "3"
mmm = "Mar"
lm = "March"
Case "4"
mmm = "Apr"
lm = "April"
Case "5"
mmm = "May"
lm = "May"
Case "6"
mmm = "Jun"
lm = "June"
Case "7"
mmm = "Jul"
lm = "July"
Case "8"
mmm = "Aug"
lm = "August"
Case "9"
mmm = "Sep"
lm = "September"
Case "10"
mmm = "Oct"
lm = "October"
Case "11"
mmm = "Nov"
lm = "November"
Case "12"
mmm = "Dec"
lm = "December"
End Select
If i < 10 Then
ii = "0" & i
Else
ii = i
End If
If j < 10 Then
jj = "0" & j
Else
jj = j
End If
If k < 10 Then
kk = "0" & k
Else
kk = k
End If
Sheets.Add(Before:=Sheets(1)).Name = lm & " " & j & " Hour " & kk
'the code after this just opens up the text file at a path determined by
'the month, day, and hour, sorts data. then comes:
Sheets(1).Delete
Next k
Next j
Next i