Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Skip first row in an Excel routine

Posted on 2014-02-04
4
Medium Priority
?
600 Views
Last Modified: 2014-02-04
The first row in my worksheet contains headers, so I don't want to apply the logic below to it.  How do I modify the code to skip the first row?  Thanks.

For Each oCell In Intersect(Range("B:Z"), ActiveSheet.UsedRange)
       'Set value of blank cells to 0
       If oCell.Value = "" Then
          oCell.Value = 0
       End If
Next
0
Comment
Question by:dbfromnewjersey
4 Comments
 
LVL 22

Assisted Solution

by:Flyster
Flyster earned 500 total points
ID: 39833187
Set the range to start at row 2. You'll need to add a ending row, just make sure it's at or beyond the used range:

Dim oCell As Range
For Each oCell In Intersect(Range("B2:Z100"), ActiveSheet.UsedRange)
       'Set value of blank cells to 0
       If oCell.Value = "" Then
          oCell.Value = 0
       End If
Next

Flyster
0
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 39833205
You could also just add another argument to the Intersect statement so you could keep the same "only used area" logic.  It would also be a good idea to check to make sure the intersect area exists, to avoid possible future errors.
Dim oCell As Range
If Not Intersect(Range("B2:Z100"), ActiveSheet.UsedRange, Rows("2:" & Rows.Count)) Is Nothing Then
    For Each oCell In Intersect(Range("B2:Z100"), ActiveSheet.UsedRange)
       'Set value of blank cells to 0
       If oCell.Value = "" Then
          oCell.Value = 0
       End If
    Next
End If

Open in new window

Alternately, you could just set all blank cells to 0 using SpecialCells:
Dim oCell As Range
On Error Resume Next
Set oCell = Intersect(Range("B:Z"), ActiveSheet.UsedRange, Rows("2:" & Rows.Count)).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not oCell Is Nothing Then oCell.Value = 0

Open in new window

Matt
0
 

Author Comment

by:dbfromnewjersey
ID: 39833210
Thanks.
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39833511
or even


For Each oCell In Intersect(Range("B:Z"), ActiveSheet.UsedRange)
if oCell.row >1 then
       'Set value of blank cells to 0
       If oCell.Value = "" Then
          oCell.Value = 0
       End If
endif
Next
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

916 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