excel - 简单代码上的 VBA 内存不足错误

标签 excel vba

我有一个非常简单的代码,我正在尝试运行,这里:

Sub Highlight()
Dim Diff As Range, cell As Range
Set Diff = Sheets(1).UsedRange.Columns("N:S")
For Each cell In Diff
If cells.Value2 > 0.1 Then
cell.Interior.ColorIndex = 0
End If
Next
End Sub

但是,就在 If 语句处,我得到运行时 7:内存不足。我在一个相当小的数据集(<5,000 个单元格)上运行它,并关闭了所有其他程序/不必要的进程,并且任务管理器显示了足够的内存。我不知道此时可能是什么原因造成的。

有任何想法吗?

最佳答案

您的代码中有错字。您的 For Each变量是 cell ,但您的代码使用 cells.Value2 .

For Each cell In Diff                 ' Using cell (singular)
    If cells.Value2 > 0.1 Then        ' Using cells (plural)
        cell.Interior.ColorIndex = 0
    End If
Next

此外,我怀疑您的范围 ( Diff ) 中的值不是数字或为空。

关于excel - 简单代码上的 VBA 内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39135263/

相关文章:

vba - 以编程方式重命名 activeX 文本框 "(name)"字段

vba - 在 Excel VBA 中,如何在 'state loss' 上保留关键变量(不写入单元格或文件)?

excel - 使用 Excel VBA ADODB 从 CSV 批量导入到 SQL Server

Excel 索引与偏移/间接 - 我不明白为什么在这种情况下偏移/间接更快

xml - vba excel : need to load an xml file and write specific values (got from current) excel then save it

VBA 类型不匹配错误 - 不一致

python - 以高效的内存使用将 pandas 数据写入 Excel

r - XLConnect 包和 xlsx

arrays - 按从小到大排列的元素索引

vba - 通过 VBA excel 程序在 MS Word 标题中的边框线后添加页码和新行