Link to home
Start Free TrialLog in
Avatar of W.E.B
W.E.B

asked on

vba to fill cells

Hello,
can you please help with a vba code (I need to sue in excel 2007) that can find last column used / last row used
then fill blank / empty cells with 0

Please see sample attached.
sample.xlsx
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try
Sub Macro()
Application.ScreenUpdating = False
Set myRange = Range(Cells(Rows.Count, 1).End(xlUp), Cells(1, Columns.Count).End(xlToLeft))

For Each c In myRange
    If c.Text = "" Then c.Value = 0
Next
Application.ScreenUpdating = True
End Sub

Open in new window

Or the version with SpecialCells which is quicker
Sub Macro()
Application.ScreenUpdating = False
Set myRange = Range(Cells(Rows.Count, 1).End(xlUp), Cells(1, Columns.Count).End(xlToLeft))

For Each c In myRange.SpecialCells(xlCellTypeBlanks)
    c.Value = 0
Next
Application.ScreenUpdating = True
End Sub

Open in new window

Regards
Avatar of W.E.B

ASKER

Thanks,

I get error

variable not defined
Set myRange =

thakns
Try simply

Sub fillzeros()
ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks).Value = 0
End Sub
SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of W.E.B

ASKER

Thanks Rgonzo1971,
it worked.

I made a mistake with the columns,
I have a gab between 2 sections (see attached sample)
in sample attached, can it stop at Column "Y"

ie, stop at last column used before gab

I tried to use
Set myRange = Range(Cells(Rows.Count, 1).End(xlUp), Cells(1, Columns.Count).End(xlToRight))

but didnt work,

thanks again,
sample.xlsx
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of W.E.B

ASKER

Thank you