Solved

Skip first row in an Excel routine

Posted on 2014-02-04
4
575 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Assisted Solution

by:Flyster
Flyster earned 125 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 125 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

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.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

617 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