1

I have a very simple code I'm trying to run, here:

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

However, right at the If statement, I get runtime 7: Out of Memory. I'm running this on a fairly small data set (<5,000 cells), and have closed all other programs/ unnecessary processes, and task manager shows plenty of memory. I have no idea what could be causing this at this point.

Any ideas?

5
  • do you have any cells in Columns N:S that have text instead of numbers or errors? Commented Aug 25, 2016 at 1:17
  • 4
    Either you've got a typo (cells.Value2 instead of cell that you're using in the for each - you don't have a variable named cells declared anywhere) or you've got erroneous data in one or more cells. Commented Aug 25, 2016 at 1:23
  • 3
    I think @KenWhite has found the source of your error but on a related note, why not conditional formatting? Commented Aug 25, 2016 at 1:27
  • @KenWhite You were right on both counts; thanks for the help! Commented Aug 25, 2016 at 1:55
  • @Jeeped Because I want to run this from Powershell, macros are really easy to deal with in that environment Commented Aug 25, 2016 at 1:56

1 Answer 1

3

You have a typo in your code. Your For Each variable is cell, but your code uses 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

In addition, I suspect that you have values in your range (Diff) that aren't numeric or are empty.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.