Solved

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

Posted on 2013-01-19
3
2,528 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

738 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