hello,
i'm trying to write a macro that colors in cells in a table if the number in the cell matches with a number in another column, but i get run time error '1004.
any idea? i tried so many things but it seems that i can't re-use range() in a for loop? can someone please help me??????
exl044
Sub colorMyCell() For i = 2 To 31 For j = 6 To 15 MyCellValue = Cells(i, j).Value If (Application.WorksheetFunction.IsNA(Application.WorksheetFunction.Match(MyCellValue, Range("A2:A31"))) <> False) Then Cells(i, j).Interior.ColorIndex = 38 'colors cell with this color if the value matches column A values End If If (Application.WorksheetFunction.IsNA(Application.WorksheetFunction.Match(MyCellValue, Range("B2:B31"))) <> False) Then Cells(i, j).Interior.ColorIndex = 40 'colors cell with this color if the value matches column B values End If If (Application.WorksheetFunction.IsNA(Application.WorksheetFunction.Match(MyCellValue, Range("C2:C31")))) Then Cells(i, j).Interior.ColorIndex = 42 'colors cell with this color if the value matches column C values End If If (Application.WorksheetFunction.IsNA(Application.WorksheetFunction.Match(MyCellValue, Range("D2:D31")))) Then Cells(i, j).Interior.ColorIndex = 44 'colors cell with this color if the value matches column D values End If Next j Next i End Sub
Last edited by davesexcel; 07-08-2010 at 01:05 AM. Reason: Code tags are required when supplying code, check out the forum rules
Hi Exl044, welcome to the forum.
Rather than using Application.WorksheetFunction.Match, just use Application.Match. Below is your code with a few minor adjustments and should work as intended.
Hope that helps!Sub colorMyCell() Dim i As Long, j As Long, MyCellValue As Long For i = 2 To 31 For j = 6 To 15 MyCellValue = Cells(i, j).Value With Application If Not IsError(.Match(MyCellValue, Range("A2:A31"), 0)) Then Cells(i, j).Interior.ColorIndex = 38 'colors cell with this color if the value matches column A values End If If Not IsError(.Match(MyCellValue, Range("B2:B31"), 0)) Then Cells(i, j).Interior.ColorIndex = 40 'colors cell with this color if the value matches column B values End If If Not IsError(.Match(MyCellValue, Range("C2:C31"), 0)) Then Cells(i, j).Interior.ColorIndex = 42 'colors cell with this color if the value matches column C values End If If Not IsError(.Match(MyCellValue, Range("D2:D31"), 0)) Then Cells(i, j).Interior.ColorIndex = 44 'colors cell with this color if the value matches column D values End If End With Next j Next i End Sub
hi paul,
thank you so much for your quick response!! you saved me a lot of time!! everything worked as intended!! i knew what i wanted the macro to do but i was about to give up since i'm a macro newbie and couldn't figure it out. now i can show my boss my new idea. thank you again!!
exl044![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks