运行时错误13

时间:2012-04-12 14:40:22

标签: excel-vba vba excel

我遇到了一些非常简单的代码问题,我已阅读多个帖子和论坛,但似乎无法找到问题:

Sub ownership()
 Dim row, column, x, y As Integer
 For row = 2 To 42
  For column = 4 To 18
   For x = 2 To 16
    For y = 5 To 45
     If ActiveWorkbook.Sheets("orka").Cells(3, row).Text = Left(ActiveWorkbook.Sheets("values").Cells(x, y).Text, ActiveWorkbook.Sheets("orka").Cells(2, row).Value) Then ActiveWorkbook.Sheets("orka").Cells(column, row).Text = "X"
    Next
   Next
  Next
 Next
End Sub

非常感谢 小号

1 个答案:

答案 0 :(得分:0)

这是因为.Text是一个只读属性。请改用.Value。你不能说

Range("A1").Text = "Blah Blah"

你可以这样做

MsgBox Range("A1").Text

试试这个

Option Explicit

Sub ownership()
    Dim row  As Integer, column  As Integer, x  As Integer, y As Integer

    For row = 2 To 42
        For column = 4 To 18
            For x = 2 To 16
                For y = 5 To 45
                    If ActiveWorkbook.Sheets("orka").Cells(3, row).Value = _
                    Left(ActiveWorkbook.Sheets("values").Cells(x, y).Text, _
                    ActiveWorkbook.Sheets("orka").Cells(2, row).Value) Then _
                    ActiveWorkbook.Sheets("orka").Cells(column, row).Value = "X"
                Next
            Next
        Next
    Next
End Sub

当您将变量声明为

时,也在VBA中
Dim row, column, x, y As Integer

然后,在这种情况下,只有最后一个变量y将被声明为Integer,其余变量将被声明为Variant。正确的方法是宣布它就像我上面做的那样。

HTH