ExcelVBA运行-时间错误'1004':"Application-defined or object-defined error"赋值时Range.Value属性

Excel VBA Run-time error '1004': "Application-defined or object-defined error" when assigning Range.Value Property

好的,我在这里看到了很多类似的问题,但在我找到的所有问题中,要么没有答案,要么与我的情况不一样,不适用,或者我尝试了该解决方案但它对我不起作用。

问题是,在我下面的代码片段中,我收到“运行-时间错误‘1004’:应用程序定义或对象定义错误分配 Range.Value 属性”

我确实设置了 Option Explicit

RNRRungTopRow 在代码的前面都声明为整数类型。 CommentRow 也是一个整数(只等于 0 或 1,声明为整数而不是布尔值,因为我在方程式中使用 0 或 1 作为乘数)。 CurrentCell 声明为 Range 类型。 LadderSheet 的类型 Worksheet 设置为我的 Excel 文件中的特定工作表。我在我的程序中多次使用 LadderSheet 没有任何问题。 RowSize 是定义的常数,等于 6。thatRung 是我定义的 Rung 类型的 class 变量。 CommentText 是一个 public 变量,声明为 Rung Class 的一部分。我尝试将 CommentText 声明为字符串并将其定义为变体。无论哪种方式,我都会遇到同样的错误。 Option Explicit也在RungClass模块中设置。

NR 是我正在使用的梯形逻辑源中 运行gs 的数量。这只是我的代码的一部分,此外,如果 RungTopRow 曾经 等于或超过 通过比较 excel 允许的最大行数,我还有退出程序的代码定义为 Const LastXLRow = 1048576.

无论如何,当我插入一个断点时,我发现我在 RungTopRow = 3 的值上得到错误。所以,我绝对没有超过最大行数。

当我注释掉下面的部分时,我捆绑到{Other Code Here} 中的所有内容都可以 运行 而不会出错。实际上,如果我注释掉 .value 赋值语句,所有 运行 都符合预期。

    For R = 0 To NR
        
    If R = 0 And CommentRow Then
        With LadderSheet.Range("C" & CStr(RungTopRow + 1) & ":P" & CStr(RungTopRow + RowSize - 1))
            .MergeCells = True
            .ShrinkToFit = False
            .WrapText = True
        End With
         Set CurrentCell = LadderSheet.Cells(RungTopRow + 1, 3)
        'this line keeps giving an application defined error
        CurrentCell.Value = thatRung.CommentText

    End If

    ... {Other Code Here} ...

    Next R

第一次尝试

LadderSheet.Cells(RungTopRow + 1, 3).Value = thatRung.CommentText

我用 CurrentCell 替换了 LadderSheet.Cells(运行gTopRow, 3),因为它在另一个类似的情况下对我有用,但由于某种原因在这里不起作用。

我也试过了

currentComment = thatRung.CommentText
currentCell.Value = currentComment

currentCommentthatRung.CommentText 声明为字符串。我得到同样的错误,在 currentCell.Value 的赋值语句上触发,所以这不是由于 thatRung.CommentText

的定义

注意如果我直接赋值给文字串如下:

CurrentCell.Value = "hello"

或如下:

currentComment = "hello"
currentCell.Value = currentComment

它执行没有问题。当然,在那种情况下我没有得到我真正想要的文本。我需要将存储在 thatRung.CommentText 中的文本放入范围 CurrentCell.

定义的单元格中

更新 运行g Class 太大了,不能在这里 post,但唯一提到 CommentText 的地方是声明如下:

Option Explicit
'These values are directly defined
Public Number As Integer
Public NumBranches As Integer
Public RungType As String
Public CommentText As String 'Neither string nor variant working
Public RungText As String

'These values will need to be calculated
Private Branches() As Branch
Private Items() As Item
Private ItemsEmpty As Boolean
Private CommandsEmpty As Boolean
Private InputsEmpty As Boolean
Private OutputsEmpty As Boolean
Private BranchesEmpty As Boolean

Const maxItemsperRow = 4 'Make sure other modules have same value in constant

也许也相关的是 CommentText 在我 post 在我的主模块代码 (ImportL5x) 的以下行中编辑的代码之前被操作的地方:

thisRung.CommentText = thisRung.CommentText & ThatXMLTag.Contents

我 post 编辑的代码在一个名为 DrawRung 的子例程中,thisRung 作为参数传递给 DrawRung

DrawRung thisRung

并且 Draw运行g 定义为 运行g:

Public Sub DrawRung(thatRung As Rung)

ThatXMLTag 是我定义的另一个自定义 class 叫做 XMLTagContents也是一个字符串。

这是 XMLTag class 的所有代码(现在只是声明,所有内容都在另一个模块中从外部分配):

Option Explicit

Public Enum XMLType
   XMLStart = 0 'No starting /, no ! data
   XMLContent = 1 'Starting !
   XMLStop = 2 'Starting /
   XMLUnknown = 3 'Anything else
End Enum
Public Name As String
Public Contents As String
Public TagType As XMLType

将某些内容分配给 Excel 单元格时 - 即使明确使用 Value 而不是 Formula/FormulaR1C1/etc 属性 - Excel 将尝试将单元格内容解析为公式,如果它“看起来”像是一个公式。

例如,如果单元格内容以“=”开头,Excel 将尝试将内容解析为公式,如果失败将生成 运行 类型的错误正在描述。