title: Aggregate function in VBA Excel
body: <p>I have table with Product name and Batch No. The product name is selected from a combo box frmmaster.cmbproduct.
  Each product has a unique combination for batch number and the last digit of the batch number changes. 
  Sample table </p>
  
  <p><a href="https://i.stack.imgur.com/fJfTb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fJfTb.png" alt="enter image description here"></a></p>
  
  <p>The next batch number of a particular product is the last batch number +1. I have used aggregate function in excel to find the highest batch number. But the same aggregate function is throwing error in VBA.</p>
  
  <p>Aggregate function for excel is =AGGREGATE(14,4,($B$2:$B$2000 =N12)*$C$2:$C$2000,1) where in N12 I am putting product name.  Column B contains product name and column C contains batch number</p>
  
  <p>Dim res As Long</p>
  
  <pre><code>Dim sh As Worksheet
  Set sh = ThisWorkbook.Sheets("Database")
  With sh
  'On Error Resume Next
  
  res = WorksheetFunction.Aggregate(14, 4, ("R2C2:R2000C2" = FrmMaster.CmbProduct.Value) * "R2C3:R2000C3", 1)
  MsgBox (res)
  
  End With
  </code></pre>
  
  <p>The above code is throwing type mismatch error</p>
  
  <p>I have changed the variable to hold value to variant  and  string but no result.</p>