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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12288588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:57:35  来源:igfitidea点击:

Excel VBA, error 438 "object doesn't support this property or method

excelexcel-vbavba

提问by Matt Ridge

I have this code in which I've been getting help with a bit, but I've run into an issue, or what I think is an issue. The last lookup, I am being told that the object doesn't support this property or method. I know it's probably something easy, but my brain is smoldering. I'd like some help if someone knows the answer of why this is happening.

我有这段代码,我在其中得到了一些帮助,但我遇到了一个问题,或者我认为是一个问题。最后一次查找时,我被告知该对象不支持此属性或方法。我知道这可能很简单,但我的大脑正在冒烟。如果有人知道为什么会发生这种情况,我需要一些帮助。

Thanks.

谢谢。

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

回答by Siddharth Rout

The Error is here

错误在这里

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

wsPOR is a workbook and not a worksheet. If you are working with "Sheet1" of that workbook then try this

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

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

Similarly

相似地

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

should be

应该

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