Excel VBA,错误438“对象不支持此属性或方法

时间:2012-09-05 19:40:24

标签: excel excel-vba vba

我有这个代码,我已经得到了一些帮助,但我遇到了一个问题,或者我认为是一个问题。最后一次查询,我被告知该对象不支持此属性或方法。我知道这可能很容易,但我的大脑是闷烧的。如果有人知道为什么会这样,我会得到一些帮助。

感谢。

Option Explicit

Sub Update_Dakota()

    Dim wsDAO As Worksheet              'Dakota OOR
    Dim wsDAD As Worksheet              'Dakota Data
    Dim wsDAR As Worksheet              'Dakota Archive
    Dim wsPOR As Workbook               'New Workbook
    Dim lastrow As Long, fstcell As Long
    Dim strFile As String, NewFileType As String, filename As String

    Set wsDAO = Sheets("Dakota OOR")
    Set wsDAD = Sheets("Dakota Data")
    Set wsDAR = Sheets("Dakota Archive")


    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .EnableEvents = False
    End With

    lastrow = wsDAD.Range("B" & Rows.Count).End(xlUp).Row + 1

    With wsDAD
        .Range("I2").Formula = "=COUNTIFS('Dakota OOR'!$B:$B,$A2,'Dakota OOR'!$D:$D,$C2, 'Dakota OOR'!$G:$G,$F2)"
        .Range("J2").Formula = "=IF(I2,""Same"",""Different"")"
        wsDAD.Range("I2:J2").Copy wsDAD.Range("I3:J" & lastrow)
        wsDAD.Range("I:J").Calculate
    End With


    strFile = Application.GetOpenFilename()
    NewFileType = "Excel Files 2007 (*.xls)"
    Set wsPOR = Application.Workbooks.Open(strFile)
    lastrow = wsPOR.Range("A" & Rows.Count).End(xlUp).Row + 1

    wsPOR.Range("A2:G" & lastrow).Select


End Sub

1 个答案:

答案 0 :(得分:6)

错误就在这里

lastrow = wsPOR.Range("A" & Rows.Count).End(xlUp).Row + 1

wsPOR是一本工作簿而不是工作表。如果您正在使用该工作簿的“Sheet1”,请尝试此

lastrow = wsPOR.Sheets("Sheet1").Range("A" & _
          wsPOR.Sheets("Sheet1").Rows.Count).End(xlUp).Row + 1

类似地

wsPOR.Range("A2:G" & lastrow).Select

应该是

wsPOR.Sheets("Sheet1").Range("A2:G" & lastrow).Select