excel 处理列表对象时属性使用无效

iaqfqrcu  于 2023-04-13  发布在  其他
关注(0)|答案(2)|浏览(282)

我得到了一个无效的使用属性错误的行,我试图分配格式化的表名表。我假设这是一个语法问题。但是,我不知道我想做的是可能的,没有更多的代码或不同的代码。我想抓住表的表名,以便在下一行它可以添加到范围内创建格式化的表语法(即MyTable[EntityID])

Option Explicit

Private Sub Workbook_Open()

Dim ws As Worksheet
Dim wbkCurBook As Workbook
Dim searchValue As String
Dim searchSheet As String
Dim tableMatch As Range
Dim cell As Range
Dim combined As String
Dim x As Long
Dim LastColumn As Long
Dim tables As ListObject

Application.ScreenUpdating = False

Set wbkCurBook = ActiveWorkbook

'highlight all entityIDs with missing definitions
    For x = 3 To Sheets.Count
        Sheets(x).Activate
        ws = ActiveSheet
        tables = Sheets(x).ListObjects
                For Each cell In Sheets(x).Range(tables & "[EntityID]")
                    searchValue = cell.Value
                    searchSheet = Sheets(x).Name
                    combined = searchSheet & " " & searchValue
                    
                    With wbkCurBook.Sheets("Data Dictionary").Range("Dictionary[CombinedName]")
                            Set tableMatch = .Find(What:=combined, _
                                            After:=.Cells(.Cells.Count), _
                                            LookIn:=xlValues, _
                                            LookAt:=xlWhole, _
                                            SearchOrder:=xlByRows, _
                                            SearchDirection:=xlNext, _
                                            MatchCase:=False)

                        If tableMatch Is Nothing Then
                                cell.Interior.Color = RGB(255, 0, 0)
                        End If
                    End With
                Next cell
    Next x
    
    Application.ScreenUpdating = True
        
 End Sub
qmb5sa22

qmb5sa221#

因为你已经声明了Dim tables As ListObject,所以你必须使用SET,而且你还必须告诉你要使用哪个表。
变更

tables = Sheets(x).ListObjects

Set tables = Sheets(x).ListObjects(1)

然后像tables.Name一样使用它
举个例子

Dim tables As ListObject

Set tables = Sheets(1).ListObjects(1)

Debug.Print tables.Name & "[EntityID]"
vyswwuz2

vyswwuz22#

高亮显示缺失

  • 代码应该被复制到一个标准的模块中,例如Module1,但是你可以将它复制到ThisWorkbook模块中。然后,在你的Open事件过程中(在ThisWorkbook module中),只使用HighlightMissingDefinitions行。
Option Explicit

Sub HighlightMissingDefinitions()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code

    ' Source
    Dim sws As Worksheet: Set sws = wb.Worksheets("Data Dictionary")
    Dim stbl As ListObject: Set stbl = sws.ListObjects("Dictionary")
    Dim srg As Range
    Set srg = stbl.ListColumns("CombinedStringName").DataBodyRange
    Dim sCell As Range
    
    ' Destination
    Dim dws As Worksheet
    Dim dtbl As ListObject
    Dim drg As Range
    Dim dCell As Range
    Dim d As Long
    Dim dWorksheetName As String
    Dim dSearchString As String
    Dim dCombinedString As String
    
    Application.ScreenUpdating = False
    
    ' Highlight
    For d = 3 To wb.Worksheets.Count
        Set drg = Nothing
        On Error Resume Next
        Set dws = wb.Worksheets(d)
        Set dtbl = dws.ListObjects(1)
        Set drg = dtbl.ListColumns("EntityID").DataBodyRange
        On Error GoTo 0
        If Not drg Is Nothing Then
            drg.Interior.Color = xlNone
            For Each dCell In drg.Cells
                dSearchString = CStr(dCell.Value)
                dWorksheetName = dws.Name
                dCombinedString = dWorksheetName & " " & dSearchString
                ' You could just do:
                'dCombinedString = dws.Name & " " & CStr(dCell.Value)
                ' No need for the extra two variables.
                Set sCell = srg.Find( _
                    What:=dCombinedString, _
                    After:=srg.Cells(srg.Cells.Count), _
                    LookIn:=xlFormulas, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows) ' 'xlNext' and 'False' are default
                If sCell Is Nothing Then
                    dCell.Interior.Color = vbRed ' vbYellow, vbGreen
                End If
            Next dCell
        End If
    Next d
    
    Application.ScreenUpdating = True
        
End Sub

可选

Sub MatchInsteadOfFind()
    ' Instead of 'Dim sCell As Range' use:
    Dim srIndex As Variant
    ' Instead of 'Set sCell...' use:
    srIndex = Application.Match(dCombinedString, srg, 0)
    ' Instead of 'If sCell...' use:
    If IsError(srIndex) Then
        dCell.Interior.Color = vbRed ' vbYellow, vbGreen
    End If
End Sub

相关问题