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

时间:2015-02-13 06:06:30

标签: excel vba excel-vba

我有一个大表,其值在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显示错误,表明系统资源不足。

另外我想在结果表中只是值,而不是公式。

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

提前谢谢!

1 个答案:

答案 0 :(得分:0)

快速解决方案对你而言并不复杂,我并不为此感到骄傲(但准备工作最快)。请记住,你要运行24米,这可能需要几分钟,也许一个小时。代码中的一些注释。

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