Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

code efficiency in vba Excel

Posted on 2007-08-09
2
Medium Priority
?
300 Views
Last Modified: 2008-03-10
Experts,

I have a question on vba code efficiency in Excel. For a certain range of cells, we want to not to display anything if the value of the cells is zero. So, here the clumsy code I have:

For counter = 5 To j
    For counter1 = 2 To 20
    If Cells(counter, counter1).Value = 0 Then
    With DestWs
    .Cells(counter, counter1).ClearContents
    End With
    End If
    Next counter1
    Next counter

It ran reasonably well until at certain point giving out a message as "type mismatch". More importantly, it is very slow, please help to improve it.

Thanks,
RDB
0
Comment
Question by:ResourcefulDB
  • 2
2 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 19662237
are you not happy with the
Tools -> Options -> VIEW tab -> uncheck the "Zero values" ?


anyhow, what about this:

dim c as Range
dim f as Range

set f = Range(Cell(5,2), Cell(j,20))
foreach c in f.Cells
  if c.Value = 0 then
    c.ClearContents
  end if
next

   
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 19662239

anyhow, what about this:

dim c as Range
dim f as Range

set f = Range(Cells(5,2), Cells(j,20))
foreach c in f.Cells
  if c.Value = 0 then
    c.ClearContents
  end if
next
0

Featured Post

Independent Software Vendors: 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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

810 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