Solved

Conditional print of Excel cells from VB

Posted on 1999-01-11
5
158 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
[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
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month7 days, 15 hours left to enroll

617 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