Solved

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

Posted on 2013-01-19
3
2,480 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
Ingeborg Hawighorst earned 250 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 250 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

Industry Leaders: 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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

740 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