简体   繁体   English

为什么我会收到此编译错误:类型不匹配

[英]Why am I getting this Compile Error: Type Mismatch

Hello everyone I am getting a compile Error: Type Does not match.大家好,我收到一个编译错误:类型不匹配。 in the code below.在下面的代码中。 I was hoping you folks can help me out.我希望你们能帮助我。 Thank you!谢谢!

Private Sub cmbBusId_AfterUpdate()

With Me

.txtStOdo = Application.WorksheetFunction.MaxIfs(Range("DataTable").ListObject.ListColumns("Ending Odometor"), (Range("DataTable").ListObject.ListColumns("Bus ID")), Me.cmbBusId)

End With

End Sub

ListColumns.DataBodyRange

  • MAXIFS function (Microsoft) MAXIFS function (微软)
  • This code has to be written in the sheet module, where you refer to the worksheet with the Me keyword.此代码必须编写在工作表模块中,您可以在其中使用Me关键字引用工作表。 The worksheet contains a text box and a combo box.工作表包含一个文本框和一个组合框。 The DataTable (named) range consists of at least one cell contained in the ListObject (Excel structured table). DataTable (命名)范围由至少一个包含在ListObject (Excel 结构化表)中的单元格组成。
  • The error is occurring because you did not use just the ' DataBodyRange part' of the list columns.发生错误是因为您没有仅使用列表列的“ DataBodyRange部分”。
  • Both examples use a ListObject variable ( tbl ) to make the code more readable.这两个示例都使用ListObject变量 ( tbl ) 使代码更具可读性。
  • The first example uses line separators, while the second example uses variables to easily distinguish the MaxIfs parameters.第一个示例使用行分隔符,而第二个示例使用变量来轻松区分MaxIfs参数。
Option Explicit

Private Sub cmbBusId_AfterUpdate1()

    With Me
        Dim tbl As ListObject: Set tbl = .Range("DataTable").ListObject
        .txtStodo = Application.WorksheetFunction.MaxIfs( _
            tbl.ListColumns("Ending Odometor").DataBodyRange, _
            tbl.ListColumns("Bus ID").DataBodyRange, _
            .cmbBusId)
    End With

End Sub

Private Sub cmbBusId_AfterUpdate2()

    With Me
        
        Dim tbl As ListObject: Set tbl = .Range("DataTable").ListObject
        Dim mrg As Range ' Max Range
        Set mrg = tbl.ListColumns("Ending Odometor").DataBodyRange
        Dim crg1 As Range ' Criteria Range 1
        Set crg1 = tbl.ListColumns("Bus ID").DataBodyRange
        Dim Criteria1 As Double ' Criteria 1
        Criteria1 = .cmbBusId
        
        .txtStodo = Application.WorksheetFunction.MaxIfs(mrg, crg1, Criteria1)
    
    End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM