[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Excel VBA - If cell is 0 then clear contents of range...

Posted on 2013-01-19
3
Medium Priority
?
2,890 Views
Last Modified: 2013-01-19
Hi,

I have a table on "Sheet2" within the range "B5:BH73".

I need a macro loop to iterate over each column.

It needs to check if row 10 = 0 then clear contents of cells 5 to 73 of that column.

I have the following so far:

For Each rng In Sheet("Sheet2").Range("B5:BH73").Columns
  'If cell 10 = 0 Then clear Cells 5 to 73
Next rng

Open in new window


How do I refer to cell 10 inside the For Loop so I can check its contents?

thanks
0
Comment
Question by:mhdi
[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
3 Comments
 
LVL 50

Assisted Solution

by:Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Ingeborg Hawighorst (Microsoft MVP / EE MVE) earned 1000 total points
ID: 38797667
Hello,

maybe like this:

Option Explicit

Sub ttest()
Dim Rng As Range
For Each Rng In ThisWorkbook.Sheets("Sheet2").Range("B10:BH10")
    If Rng.Value = 0 Then Range(Cells(5, Rng.Column), Cells(73, Rng.Column)).ClearContents
Next Rng
End Sub

Open in new window


cheers, teylyn
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 1000 total points
ID: 38797704
mhdi, although teylyn's code works just fine, if you want to know the syntax for your way then it could be something like this

For Each Rng In Sheets("Sheet2").Range("B5:BH73").Columns
  If Rng.Cells(6) = 0 Then Rng.ClearContents
Next Rng
0
 

Author Comment

by:mhdi
ID: 38797763
Thankyou. I tried both versions but ended up using ssaqibh's code.

@teylyn your code seemed to always delete the contents on the first sheet of the workbook.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 …
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 use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

649 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