Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Conditional print of Excel cells from VB

Posted on 1999-01-11
5
Medium Priority
?
160 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 400 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 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…
Suggested Courses

886 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