简体   繁体   English

对象'_Worksheet'的方法'Range'失败

[英]Method 'Range' of object '_Worksheet' failed

I'm trying to use the following code to generate data on one sheet by taking data from another. 我正在尝试使用以下代码通过从另一个表中获取数据来生成一张表上的数据。 I've beed doing similar things with VBA for a while and haven't encountered this problem until now. 我已经花了一段时间与VBA做类似的事情,直到现在才遇到这个问题。 I've looked through quite a few other questions with the same error but haven't found what I need. 我已经查看了很多其他问题但是没有找到我需要的相同错误。

My code started crashing with only the message "400", so I added an error hander and now get the message: 我的代码开始崩溃只有消息“400”,所以我添加了错误处理程序,现在得到消息:

Method 'Range' of object '_Worksheet' failed. 对象'_Worksheet'的方法'Range'失败。

My code (on sheet "Buyers"): 我的代码(在表“买家”):

Public Sub MakeSheets(Optional ByVal SortSource As Boolean = False, Optional ByVal ExportSheets As Boolean = False)

    On Error GoTo ErrorMsg

    Dim r, n As Integer 'for looping through [r]ows and iterating [n]ew lines added to sheets

    If SortSource Then 'This code never executes because I'm only using the default boolean values so far
        Sheets("Buyers").UsedRange.Sort key1:=FullRange("NAME"), Header:=xlYes
        Sheets("Properties").UsedRange.Sort key1:=Sheets("Properties").FullRange("STREET NAME"), key2:=Sheets("Properties").FullRange("NUMBER"), Header:=xlYes
    End If

    Sheets("Ownership").Rows("3:" & Sheets("Ownership").UsedRange.Rows.Count).Delete
    n = 2 'last title row of target worksheet
    With Sheets("Properties")
    MsgBox .Cells(3, .FullRange("STREET NAME").Column).Value 'Successfully displays the appropriate cell value
        For r = 2 To .PropertyRows()
            If Not IsEmpty(.Cells(r, .FullRange("OWNER").Column).Value) Then
                n = n + 1
                MsgBox n 'displays "3", confirming this point is successfully reached once.
                'Problem appears to be next line
                Sheets("Ownership").Range("A" & n & ":L" & n).Value = Array( _
                    .Cells(r, .FullRange("MLS#").Column).Value, _
                    .Cells(r, .FullRange("LIST PRICE").Column).Value, _
                    .Cells(r, .FullRange("NUMBER").Column).Value & " " & .Cells(r, .FullRange("STREET NAME").Column).Value, _
                    .Cells(r, .FullRange("OWNER").Column).Value & " " & .Cells(r, .FullRange("OWNED").Column).Value, _
                    .Cells(r, .FullRange("SUBDIVISION").Column).Value, _
                    .Cells(r, .FullRange("COUNTY").Column).Value, _
                    .Cells(r, .FullRange("BED").Column).Value, _
                    .Cells(r, .FullRange("BATH").Column).Value, _
                    .Cells(r, .FullRange("YEAR BUILT").Column).Value, _
                    .Cells(r, .FullRange("TAXES").Column).Value, _
                    .Cells(r, .FullRange("TAX YEAR").Column).Value, _
                    .Cells(r, .FullRange("ACQUISITION DATE").Column).Value)
            End If
        Next r
    End With
    Exit Sub

ErrorMsg:
    MsgBox Err.Description
End Sub

I can post the code for FullRange if requested, but it calls a couple of other custom functions, and I think the MsgBox I threw in before the loop starts ably demonstrates that it isn't the problem. 如果需要,我可以发布FullRange的代码,但它调用了几个其他自定义函数,我认为在循环开始之前我插入的MsgBox表明它不是问题。 (There is a public FullRange function on both "Buyers" and "Properties", each referring to their own sheets. Both are used extensively elsewhere by code in both sheets without issue.) (“买家”和“属性”都有一个公共的FullRange功能,每个功能都指他们自己的工作表。两者都被其他地方的代码广泛使用,没有问题。)

I've also tried completely removing the With block in case that was creating some strange conflict, but the results were identical. 我也尝试完全删除With块,以防产生一些奇怪的冲突,但结果是相同的。

It turns out something fishy was going on under the hood of Excel or my computer. 事实证明,在Excel或我的计算机引擎盖下发生了一些腥味。 I had already Quit and reopened Excel before posting, but then I started getting some quirky behavior (such as Macro's disappearing from the run macro menu) so I rebooted the whole machine. 在发布之前我已经退出并重新打开了Excel,但后来我开始得到一些古怪的行为(例如Macro从运行宏菜单中消失)所以我重新启动整个机器。 My original code is working fine now. 我原来的代码现在工作正常。 Thanks for looking with me guys. 谢谢你和我一起看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM