Excel Macro Find and fill Blanks Help!@

xxcucumberxx

New Member
Joined
Feb 23, 2016
Messages
7
I cant get my Macro to find and fill blanks in column "m" starting from row 2. It fills the blanks but it also fill the blanks all the way down to the end of the spreadsheet. I just need it to fill to the last row of data. Also to reset the color when i rerun the macro. So i can fix it and rerun it and so it un-colors the ones with data in it


Columns("M:M").Select
With Selection.Font
.ColorIndex = xlAutomatic
.TintAndShade = 0
End With
Range("O6").Select


Dim myRange As Range
Set myRange = Sheet1.Range("M2:M5000")


'clear all color
myRange.Interior.ColorIndex = xlNone


'color only blank cells
myRange.SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 6




Please help me fix this. Thank you so much who does. Maybe there is a different formula i could use or something
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try this:
Code:
Sub MyColor()

    Dim myLastRow As Long
    Dim myRange As Range
    
'   Find last row in column M with data
    myLastRow = Cells(Rows.Count, "M").End(xlUp).Row
    
'   Set range to apply to
    Set myRange = Sheet1.Range("M2:M" & myLastRow)

'   Remove font color from all cells in row M
    With myRange.Font
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With

'   Clear all fill color
    myRange.Interior.ColorIndex = xlNone

'   Color only blank cells
    myRange.SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 6

End Sub
 
Last edited:
Upvote 0
Hi,

Try this:

Code:
Sub FillBlanks()


    Dim myRange As Range
    Columns("M:M").Interior.ColorIndex = xlAutomatic
    Set myRange = Sheet1.Range("M2:M" & Cells(Rows.Count, 13).End(xlUp).Row)


    'clear all color
    myRange.Interior.ColorIndex = xlNone


    'color only blank cells
    On Error Resume Next
    myRange.SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 6


End Sub

Joe, if there are no blank cells your code will halt.

igold
 
Upvote 0
Joe, if there are no blank cells your code will halt.
Its at the very end of the code, and there is nothing left to do at that point, so that error message can be easily rectified, i.e.
Code:
Sub MyColor()

    Dim myLastRow As Long
    Dim myRange As Range
    
'   Find last row in column M with data
    myLastRow = Cells(Rows.Count, "M").End(xlUp).Row
    
'   Set range to apply to
    Set myRange = Sheet1.Range("M2:M" & myLastRow)

'   Remove font color from all cells in row M
    With myRange.Font
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With

'   Clear all fill color
    myRange.Interior.ColorIndex = xlNone


'   Color only blank cells
[COLOR=#ff0000]    On Error Resume Next[/COLOR]
    myRange.SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 6
[COLOR=#ff0000]    On Error GoTo 0[/COLOR]

End Sub
Its also usually good form to add that second line:
On Error GoTo 0
in case more code gets added below it in the future, it doesn't end up ignoring other errors without you realizing it.
 
Upvote 0
Joe,

Good point on the On Error Goto 0. Thank you.

Regards,

igold
 
Last edited:
Upvote 0
Good point on the On Error Goto 0. Thank you.
No problem. Good catch on the error possibility too. It doesn't affect the outcome, but of course no one wants to see error messages!
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,010
Members
449,204
Latest member
tungnmqn90

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top