Solved

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

Posted on 2013-01-19
3
2,250 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
3 Comments
 
LVL 50

Assisted Solution

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
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.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now