字典循环和应用程序定义或对象定义的错误

时间:2012-04-20 20:43:15

标签: excel vba

我在循环通过字典试图转到指定的单元格时遇到错误,因为字典包含列地址。这是一个小背景 -

  1. 遍历任何有评论的单元格(仅限顶行!!)
  2. 将该列和注释文本存储在字典对象中
  3. 循环显示剩余的行,并且仅转到与第一行的注释相同的列中的单元格。然后,此信息将发送到另一个工作表
  4. 如,

    |CELL1|CELL2 (W COMMENT)|CELL3|
    

    - 存储Cell2列地址(即2)和注释,在列位置2处选择下一个单元格。

    我已经发布了我的全班,但你需要注意的部分是注释代码的最底层。我意识到代码可能有点大,所以如果你看到一些快捷方式,请指教!

    Option Explicit
    Public buildWs As Worksheet
    Public targWs As Worksheet
    Public CommentVal As Comment
    Public FirstRow As Range
    Public targLastrow As Range
    Public firstRecordRow As Range
    Public commRange As Range
    Public commCell As Range
    Public recordRow As Range
    Public LastRow As Long
    Public vertRange As Range
    Public vertCell As Range
    
    '//---------------------------------------
    '//getter and setter methods of the class
    Public Property Get ItemNum() As Comment
        ItemNum = CommentVal
    End Property
    Public Property Let ItemNum(Value As Comment)
        CommentVal = ItemNum
    End Property
    
    Public Property Get Start() As Range
        Start = FirstRow
    End Property
    Public Property Let Start(Value As Range)
        Set FirstRow = Value
    End Property
    
    Public Property Get affBuild() As Worksheet
        affBuild = buildWs
    End Property
    Public Property Let affBuild(Value As Worksheet)
        Set buildWs = Value
    End Property
    
    Public Property Get allinaBuid() As Worksheet
        allinaBuild = targWs
    End Property
    Public Property Let allinaBuild(Value As Worksheet)
        Set targWs = allinaBuild
    End Property
    
    Public Sub setId()
        Dim rowCount As Integer
        Dim element As Variant
        Dim n As Integer
        Dim z As Integer
        Dim keyValue As New Dictionary
        Dim col As String
    
        On Error Resume Next
        Set commRange = buildWs.Cells _
          .SpecialCells(xlCellTypeComments)
        On Error GoTo 0
        If commRange Is Nothing Then
            MsgBox "No Comments Found"
            Exit Sub
        End If
        buildWs.Activate
        For Each commCell In commRange
            commCell.Select
            '// do something here with the cell since it meets our criteria of having a comment
            keyValue.Add ActiveCell.Column, ActiveCell.Comment.Text
        Next commCell
        '// loop through the vertical range for the 1st column to get a count of rows
        '//--------------------------------------------------------------------------
        Set vertRange = Range("A:A")
        vertRange.Select
        For Each vertCell In vertRange
            If ActiveCell.Value = "" Then
                Exit For
            Else
                rowCount = rowCount + 1
                ActiveCell.Offset(1, 0).Select
            End If
        Next vertCell
        '// row count is off by one since we don't care about the first row
        rowCount = rowCount - 1
    
        '// select the first real row we care about as row1 is only header info
        FirstRow.Select
        For Each element In keyValue
            col = element
            MsgBox col '<-- this displays the correct dictionary information -->
            Cells("2", col).Select '<-- this fails and gives me an error! -->
        Next element
    End Sub
    

1 个答案:

答案 0 :(得分:0)

发生错误是因为您需要将整数传递给Cells("2", col),而不是字符串。您应该传递包含VariantLong的{​​{1}}。

Long

Dim col As Variant

然后

Dim col As Long

你这里也有错误

Cells(2, col).Select

(我将Public Property Let allinaBuild(Value As Worksheet) Set targWs = Value ' <== was Set targWs = allinaBuild End Property 替换为allinaBuild

Value

中的相同错误