0

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

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

十分感谢

4

1 回答 1

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. 正确的方法是像我在上面所做的那样声明它。

高温高压

于 2012-04-12T15:29:43.557 回答