设置新打开的工作簿为wb给出应用程序/对象定义的错误

时间:2017-06-16 05:30:46

标签: vba excel-vba excel

整个代码运行良好,直到我将新打开的工作簿设置为wb并将其引用到受尊重的范围。所以假设我的If和EndIf位置没有任何问题。我尝试了几次不同的方式。使用wb,...对象定义的错误。我从Score中删除了.Value,它给出了错误。使用远程语句而不是分数,也会给出错误。我应该在哪里改变?谢谢

Option Explicit
Sub TryFloo()
Dim year As String, x As String, startdate As String, month As String
Dim first As Integer, second As Integer, addDay As Integer, daterow As Integer
Dim mainbk As Workbook, wb As Workbook
Dim r&, i&, m As Integer
Dim path As String
Dim Score As Range

Set mainbk = Workbooks("kumpul mean.xlsm")
r = 5
With mainbk
  If IsError(.Sheets(2).Range("E" & r)) Then    '<---- Open first If()
     Exit For
  Else
     year = ThisWorkbook.Sheets(2).Range("A" & r).Value
     x = Range("C" & r).Value
     addDay = Range("E" & r).Value - 1
     first = InStr(x, "/")
     second = InStr(first + 1, x, "/")
     startdate = Mid(x, 1, 2)
     month = Mid(x, first + 1, second - first - 1)

     If month = 11 Then m = 1
     If month = 12 Then m = 31
     If month = 1 Then m = 62
     End If

     path = "E:\SouthNorth\1979.xls"
     Set wb = Workbooks.Open(Filename:=path)
     wb.Activate
     daterow = m + startdate

     For i = daterow To daterow + addDay
       With wb
         Score = .Sheets(1).Range(Cells(i, 24))'<--- Application/object-defined error
         If Score.Value > 12 And Score.Value < 14 Then Worksheets("Sheet2").Range("C" & i).Value = 1
         ElseIf Score.Value > 14 And Score.Value < 16 Then Worksheets("Sheet2").Range("C" & i).Value = 2
         ElseIf Score.Value > 16 Then Worksheets("Sheet2").Range("C" & i).Value = 3
         End If
       End With
      Next i
      'wb.Close SaveChanges:=True '<- left open for checking
    End If                                 '<---- close first If()
  End With
  End Sub

1 个答案:

答案 0 :(得分:1)

wb.Range(Cells(i, 24))不正确。 Cells是工作表的属性,而不是工作簿。

尝试这样的事情

wb.Sheets("Sheet1").Range("X" & i)

wb.Sheets("Sheet1").Cells(i,24)