简体   繁体   English

Excel VBA:比较有效值与较大范围的公式的方法

[英]Excel VBA: More efficient way to compare values with formulas for large range

I have large table with values in range H2:PIG2202. 我有一个很大的表,值在H2:PIG2202范围内。 I need to compare the first rows H2:PIG2 values with all other rows values. 我需要将第一行H2:PIG2值与所有其他行值进行比较。 And if there is a match in the result table it pastes just those values which matched. 如果结果表中存在匹配项,则仅粘贴那些匹配的值。 Now I'm using this formula in the result table to display needed values: 现在,我在结果表中使用此公式来显示所需的值:

=IF(sheet!H$2=sheet!H3;IF(AND(sheet!H3;ISBLANK(sheet!H3))=FALSE;sheet!H3;"");"")

The VBA code is: VBA代码是:

Sub find()
Application.ScreenUpdating = False
Range("H2:PIG2202").FormulaR1C1 = _
"=IF('sheet'!R2C='sheet'!R[1]C,IF(AND('sheet'!R[1]C,ISBLANK('sheet'!R[1]C))=FALSE,'sheet'!R[1]C,""""),"""")"
Application.ScreenUpdating = True
End Sub

The problem is that Excel shows an error when I run this macros that there are not enough system resources. 问题是当我运行此宏时,Excel显示错误,表明系统资源不足。

Also I would like that in the result table would be just values, not formulas. 我也希望结果表中的值只是值,而不是公式。

Is that possible to do? 那有可能吗? I have no idea how to achieve this :( 我不知道如何实现这一目标:(

Thank you in advance! 先感谢您!

Quick solution for your which is not sophisticated and I'm rather not proud of it (but it was quickest to prepare). 快速的解决方案虽然不复杂,但我对此并不感到骄傲(但是准备起来最快)。 Keep in mind that you are going to run it 24m times which could take several minutes, maybe an hour. 请记住,您将要运行2400万次,这可能需要几分钟,甚至一个小时。 Some comments inside the code. 代码中的一些注释。

Sub Solution()
Application.ScreenUpdating = False

'-----previous one
'Range("H2:PIG2202").FormulaR1C1 = _
            "=IF('sheet'!R2C='sheet'!R[1]C,IF(AND('sheet'!R[1]C,ISBLANK('sheet'!R[1]C))=FALSE,'sheet'!R[1]C,""""),"""")"

'----- new one- inserting values- first idea, simple code

Dim Cell As Range
'run for one row first to check if it is ok! and check time needed per row
'next change range to one you expect
'next- take a cup of coffee and relax...

For Each Cell In Range("h2:PIG2")
    Cell.FormulaR1C1 = _
            "=IF('sheet'!R2C='sheet'!R[1]C,IF(AND('sheet'!R[1]C,ISBLANK('sheet'!R[1]C))=FALSE,'sheet'!R[1]C,""""),"""")"
    Cell.Value = Cell.Value
    'to trace progress in Excel status bar
    Application.StatusBar = Cell.Address
Next Cell

Application.ScreenUpdating = True
End Sub

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

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