Solved

Conditional print of Excel cells from VB

Posted on 1999-01-11
5
154 Views
Last Modified: 2010-05-03
I have a VB program that accesses an Excel worksheet.

What I want to do is when I finish to send data, print ONLY the rows of cells that don't have blank or zero. For example, I make a statistic of the employees' laboral absences this year and as result I have:

Employee 1     10
Employee 2      3
Employee 3      0
Employee 4      1

How can I make to print the worksheet WITHOUT printing Employee 3?

I have VB 5.0 Enterprise ad Office 97 Pro. Thanks in advance.

As you see, I have the basics on VB, but I don't know much about Excel and VBA. Please send a simple example code.
0
Comment
Question by:BETTY
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:ameba
ID: 1455504
Can you make to print the worksheet WITH printing Employee 3?
0
 
LVL 15

Expert Comment

by:ameba
ID: 1455505
If you can, to hide a range or selection, use
    Selection.Font.ColorIndex = 2 ' white

Use Macro Recorder in Excel to generate your code.
0
 
LVL 3

Accepted Solution

by:
sduckett earned 100 total points
ID: 1455506
Try the following code which will filter out your unwanted records, print the table, then cancel the filter.

Assumptions:
Your worksheet is called "Sheet1".
The absence info is in column 2.
Cell A1 is part of your table. (If not, change 'Cells(1,1)' with the topmost left cell of your table)



Sub ExcludeBlanks()

Const ThisWorksheet = "Sheet1"
Const AbsencesColumn = 2

With Worksheets(ThisWorksheet).Cells(1, 1).CurrentRegion
    .AutoFilter field:=AbsencesColumn, Criteria1:=">0"
    .PrintOut
    .AutoFilter 'This with no parameters turns off Autofilter
End With

End Sub


PS Ameba's advice to use the Macro-Recorder is priceless for beginners. Don't expect the best code but it is a fast way to learn. Record a macro then improve it...
All the best.
0
 
LVL 1

Author Comment

by:BETTY
ID: 1455507
After checking sduckett's answer, I've found it's working well, although I had to retouch a little the code to fit my interest (Blame on me, I didn't explain all in detail in my question).

So I'm going to give the points him/her.

Despite this, ameba's trick is as easy as genial, so, please, go to question titled "From Betty to Ameba" and send any answer, I'll give you the 10 points.
0
 
LVL 1

Author Comment

by:BETTY
ID: 1455508
After checking sduckett's answer, I've found it's working well, although I had to retouch a little the code to fit my interest (Blame on me, I didn't explain all in detail in my question).

So I'm going to give the points him/her.

Despite this, ameba's trick is as easy as genial, so, please, go to question titled "From Betty to Ameba" and send any answer, I'll give you the 10 points.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

828 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