2

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
VBasic2008
  • 44,888
  • 5
  • 17
  • 28

1 Answers1

0

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
VBasic2008
  • 44,888
  • 5
  • 17
  • 28
  • First of all, thank you so much for your detailed response it's more than I could hope for. When I input the code it's giving me an error on .Range is now giving me an error Method or Data member not found. I am relatively new using VBA any help will be great thank you! – Mr_Lemonhead May 09 '21 at 04:00
  • Not sure, but I would guess that `Me` does not refer to a worksheet, so qualify the range appropriately (e.g. `ThisWorkbook.Worksheets("Sheet1").Range("DataTable").ListObject`) or just remove the dot (`.`)(not recommended). – VBasic2008 May 09 '21 at 04:33