[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Excel 2007 Macro to Add Zeros To Blank Row

Posted on 2011-10-24
Medium Priority
278 Views
Hello,

Can someone assist in providing a macro that will look at sheet1, row A2:U2, and if that row is blank, add a zero in each of the cells within that range, then do the same for sheet2, and sheet 3?

Thanks!
0
Question by:Escanaba

LVL 10

Accepted Solution

etech0 earned 2000 total points
ID: 37018291
Do you want a zero in every blank cell, or zeros if the whole row is blank?

(option 1:)

for each cell in sheets("sheet1").range("A2:U2")
if cell.value = "" then cell value = "0"
next
for each cell in sheets("sheet2").range("A2:U2")
if cell.value = "" then cell value = "0"
next
for each cell in sheets("sheet3").range("A2:U2")
if cell.value = "" then cell value = "0"
next
End Sub

(option 2:)

for each cell in sheets("sheet1").range("A2:U2")
if cell.value <> "" then addz = false
next
for each cell in sheets("sheet1").range("A2:U2")
if addz then cell.value = "0"
next
for each cell in sheets("sheet2").range("A2:U2")
if cell.value <> "" then addz = false
next
for each cell in sheets("sheet2").range("A2:U2")
if addz then cell.value = "0"
next
for each cell in sheets("sheet3").range("A2:U2")
if cell.value <> "" then addz = false
next
for each cell in sheets("sheet3").range("A2:U2")
if addz then cell.value = "0"
next
End Sub
0

LVL 17

Expert Comment

ID: 37018304
Try this code in VBA:
``````Sub zero()

For Each cell In Range("A2:U2")
If cell.Value = "" Then
cell.Value = 0
End If
Next

End Sub
``````
0

LVL 24

Expert Comment

ID: 37018315
Perhaps this?
``````Sub x()

Dim ws As Worksheet

For Each ws In Worksheets
Select Case ws.Name
Case "Sheet1", "Sheet2", "Sheet3"
If WorksheetFunction.CountBlank(ws.Range("A2:U2")) = ws.Range("A2:U2").Count Then
ws.Range("A2:U2").Value = 0
End If
End Select
Next ws

End Sub
``````
0

LVL 1

Author Closing Comment

ID: 37018478
Option 2 did the trick.  Thank you for the quick response.
0

LVL 10

Expert Comment

ID: 37018489
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
###### Suggested Courses
Course of the Month20 days, 11 hours left to enroll