简体   繁体   English

VBA用户表单中的MS Excel 2010 MAXIFS等效项

[英]MS Excel 2010 MAXIFS equivalent in VBA Userform

I've created an Excel Userform to facilitate the data entry of new lines into a contract register. 我创建了一个Excel用户窗体,以方便将新行的数据输入到合同注册簿中。 I have a field that auto-generates a new unique contract number by looking for the largest number in the list of contract numbers (Column A) and then adds 1. This formula works perfectly: 我有一个字段,可以通过在合同编号列表(A列)中查找最大编号来自动生成一个新的唯一合同编号,然后添加1。此公式非常适用:

Me.tbContractNumber = Application.WorksheetFunction.Max(Sheet1.UsedRange.Columns(1)) + 1

I now have to add an IF criteria to filter out any contract numbers LESS Than "2018000". 我现在必须添加一个IF标准,以过滤出少于“ 2018000”的任何合同号。 I have worked out how to do this in a normal Excel workbook using MAXIFS but apparently MAXIFS is not an available function in VBA? 我已经在使用MAXIFS的普通Excel工作簿中解决了该问题,但是显然MAXIFS在VBA中不是可用的功能吗?

Can someone suggest an equivalent VBA code to the below Excel formula? 有人可以为下面的Excel公式建议等效的VBA代码吗? Thanking you in advance! 预先感谢您!

=MAXIFS(A2:A500,A2:A500,"<2018000")+1

EDIT Our work computers run 2010 and won't allow me to add the MS Office 16.0 Object Library so MAXIFS function will not work. 编辑我们的工作计算机运行2010,并且不允许我添加MS Office 16.0对象库,因此MAXIFS功能将无法使用。 I can get the following array formula to work but I have never used an array formula in VBA. 我可以使以下数组公式起作用,但是我从未在VBA中使用过数组公式。 Could someone please suggest an equivalent VBA code to the below Excel formula? 有人可以为下面的Excel公式建议等效的VBA代码吗? Thanking you in advance! 预先感谢您!

{=MAX(IF(A:A<2018000,A:A)) +1}

If you're looking for the largest number anyway, do you need to filter out anything lower than 2018000? 无论如何,如果您正在寻找最大的数目,是否需要筛选出低于2018000的内容? If you have at least one entry equal to/higher than 2018000, your end result will be higher regardless of the other entries. 如果您至少有一个等于或高于2018000的条目,则无论其他条目如何,最终结果都会更高。

I'm sure there are more efficient ways of doing it, but if you are happy with: 我敢肯定有更有效的方法,但是如果您满意:

Me.tbContractNumber = Application.WorksheetFunction.Max(Sheet1.UsedRange.Columns(1)) + 1

then try: 然后尝试:

me.tbContractNumber = Application.WorksheetFunction.MaxIfs(Sheet1.UsedRange.Columns(1), Sheet1.UsedRange.Columns(1), ">" & 2018000) + 1

...but apparently MAXIFS is not an available function in VBA . ...但是显然MAXIFS在VBA中不是可用的功能 In VBA it is not present, but if you add the Excel 16.0 Object Library (second on the screenshot) to your project, you would be able to access it as follows: 在VBA中,它不存在,但是如果您将Excel 16.0对象库(屏幕截图中的第二个)添加到项目中,则可以按以下方式访问它:

Application.WorksheetFunction.MaxIfs 'Only in Excel
Excel.WorksheetFunction.MaxIfs       'Any host of VBA - Excel, Access, Word

在此处输入图片说明

The library is added by default, if you work in Excel. 如果您在Excel中工作,则默认情况下会添加该库。 Concerning "translation" the working formula from Excel to VBA, check this: 关于将工作公式从Excel“转换”为VBA,请检查以下内容:

https://stackoverflow.com/a/49363501/5448626 https://stackoverflow.com/a/49363501/5448626

I just learned the Evaluate function! 我刚刚学习了评估功能! So equivalent of the array formula I want to use converts in VBA to: 因此,等效于我要使用的数组公式在VBA中将转换为:

Me.tbContractNumber = Evaluate("=MAX(IF(" & "A:A" & "<2018000," & "A:A" & "))+1")

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

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