简体   繁体   中英

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)
  • This code has to be written in the sheet module, where you refer to the worksheet with the Me keyword. 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).
  • The error is occurring because you did not use just the ' DataBodyRange part' of the list columns.
  • Both examples use a ListObject variable ( tbl ) to make the code more readable.
  • The first example uses line separators, while the second example uses variables to easily distinguish the MaxIfs parameters.
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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