[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
5
Medium Priority
?
278 Views
Last Modified: 2012-05-12
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
Comment
Question by:Escanaba
5 Comments
 
LVL 10

Accepted Solution

by:
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:)

Sub AddZeros()
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:)

Sub AddZerosRow()
dim addz as boolean
addz = true
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
addz = true
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
addz = true
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

by:gtgloner
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

Open in new window

0
 
LVL 24

Expert Comment

by:StephenJR
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

Open in new window

0
 
LVL 1

Author Closing Comment

by:Escanaba
ID: 37018478
Option 2 did the trick.  Thank you for the quick response.
0
 
LVL 10

Expert Comment

by:etech0
ID: 37018489
Glad to help!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question