Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help for "Range" problem

Status
Not open for further replies.

uszd

Technical User
Nov 8, 2007
8
CA
I need a macro to print a range from cell "A1" to the last non-zero cell in column L. this is my vba:

Sub findprint()

Dim i As Integer
Dim a As Integer
Dim b As Integer

i = WorksheetFunction.CountIf(Columns(12), 0)
a = WorksheetFunction.CountA(Columns(12))
b = a - i - 1
Range(Cells(a, 1), Cells(l, b)).Select
Selection.PrintOut Copies:=1, Collate:=True

End Sub

It shows "appliaction-defined or object-defined error" when I tried to run it. I'm a beginner of VBA and, hopefully someone could help me fix it.

Thanks!
 
Cells(Row, Column)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
'l' is out of scope or should be '1' instead (Cells(l,b)).

combo
 
Hi PHV and combo,

Thanks very much for your guys inputs!

I changed " Range(Cells(a, 1), Cells(l, b)).Select"
to "Range(Cells(1, 1), Cells(b, 12)).Select", and it works now!
However, when I changed it as " Range(Cells(a, A), Cells(l, L)).Select" it stuck again and shows the same prompt as before.

Anyways,it runs properly.

Many thanks for your guys!
 
Range(Cells(a, "A"), Cells(b, "L")).Select

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
If you need A and L as column indicators, use Range(Cells(a, "A"), Cells(l, "L")), otherwise vba assumes them as variables (0, empty, null, nothing, depending on type). You still use 'l' in the second 'Cells' reference, if it has invalid value, will cause error.

combo
 
Hi PHV and combo,

Definitely your guys are right! Learning more from you!

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top