?
Solved

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

Posted on 2013-01-19
3
Medium Priority
?
3,057 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: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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

850 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