Solved

Conditional print of Excel cells from VB

Posted on 1999-01-11
5
150 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now