繁体   English   中英

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

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

我有一个很大的表,值在H2:PIG2202范围内。 我需要将第一行H2:PIG2值与所有其他行值进行比较。 如果结果表中存在匹配项,则仅粘贴那些匹配的值。 现在,我在结果表中使用此公式来显示所需的值:

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

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

问题是当我运行此宏时,Excel显示错误,表明系统资源不足。

我也希望结果表中的值只是值,而不是公式。

那有可能吗? 我不知道如何实现这一目标:(

先感谢您!

快速的解决方案虽然不复杂,但我对此并不感到骄傲(但是准备起来最快)。 请记住,您将要运行2400万次,这可能需要几分钟,甚至一个小时。 代码中的一些注释。

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