Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Compile error, Variable not defined

I have this code in a file that searches an entire form and is activated to
print preview a file. When it works, it stops printing row at the last entry
in the sheet. I get the Compile error, Variable not defined when I fire the
macro and it appears at the "For Each "cell" .............
I need it to count rows with data in the first column and then when it comes
to the end, to send the document to the printer with only the rows with data
showing.. Any help would be appreciated.
Thanks


Sub findlastrow()
Dim lastrow As Long
Dim r As Range
lastrow = 1
For Each cell In Range(Cells(1, "A"), Cells(Rows.Count, "A").End(xlUp))
If Trim(cell.Text) < "" Then
lastrow = cell.Row
End If
Next
Set r = ActiveSheet.Range("A1:M" & lastrow)
r.PrintPreview
' or
'Activesheet.PageSetup.PrintArea = r.address(external:=true)
'Activesheet.Printout
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Compile error, Variable not defined

Include the following after your existing Dim statements.

Dim Cell As Range


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"RJR" wrote in message
.. .
I have this code in a file that searches an entire form and is activated to
print preview a file. When it works, it stops printing row at the last
entry in the sheet. I get the Compile error, Variable not defined when I
fire the macro and it appears at the "For Each "cell" .............
I need it to count rows with data in the first column and then when it
comes to the end, to send the document to the printer with only the rows
with data showing.. Any help would be appreciated.
Thanks


Sub findlastrow()
Dim lastrow As Long
Dim r As Range
lastrow = 1
For Each cell In Range(Cells(1, "A"), Cells(Rows.Count, "A").End(xlUp))
If Trim(cell.Text) < "" Then
lastrow = cell.Row
End If
Next
Set r = ActiveSheet.Range("A1:M" & lastrow)
r.PrintPreview
' or
'Activesheet.PageSetup.PrintArea = r.address(external:=true)
'Activesheet.Printout
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,117
Default Compile error, Variable not defined

i notice you don't have "cell" declared.
dim cell as range
:)
susan



On Mar 22, 2:55 pm, "RJR" wrote:
I have this code in a file that searches an entire form and is activated to
print preview a file. When it works, it stops printing row at the last entry
in the sheet. I get the Compile error, Variable not defined when I fire the
macro and it appears at the "For Each "cell" .............
I need it to count rows with data in the first column and then when it comes
to the end, to send the document to the printer with only the rows with data
showing.. Any help would be appreciated.
Thanks

Sub findlastrow()
Dim lastrow As Long
Dim r As Range
lastrow = 1
For Each cell In Range(Cells(1, "A"), Cells(Rows.Count, "A").End(xlUp))
If Trim(cell.Text) < "" Then
lastrow = cell.Row
End If
Next
Set r = ActiveSheet.Range("A1:M" & lastrow)
r.PrintPreview
' or
'Activesheet.PageSetup.PrintArea = r.address(external:=true)
'Activesheet.Printout
End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Compile error, Variable not defined

You have
Option Explicit
at the top of the module.

That means that you've told excel that you're going to declare every variable
you use in that module.

And Cell wasn't dimmed.

Add this line near the top:

Dim Cell As Range



RJR wrote:

I have this code in a file that searches an entire form and is activated to
print preview a file. When it works, it stops printing row at the last entry
in the sheet. I get the Compile error, Variable not defined when I fire the
macro and it appears at the "For Each "cell" .............
I need it to count rows with data in the first column and then when it comes
to the end, to send the document to the printer with only the rows with data
showing.. Any help would be appreciated.
Thanks

Sub findlastrow()
Dim lastrow As Long
Dim r As Range
lastrow = 1
For Each cell In Range(Cells(1, "A"), Cells(Rows.Count, "A").End(xlUp))
If Trim(cell.Text) < "" Then
lastrow = cell.Row
End If
Next
Set r = ActiveSheet.Range("A1:M" & lastrow)
r.PrintPreview
' or
'Activesheet.PageSetup.PrintArea = r.address(external:=true)
'Activesheet.Printout
End Sub


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Compile error, Variable not defined

You guys and girls are great, thank you so much
Bob

"RJR" wrote in message
.. .
I have this code in a file that searches an entire form and is activated to
print preview a file. When it works, it stops printing row at the last
entry in the sheet. I get the Compile error, Variable not defined when I
fire the macro and it appears at the "For Each "cell" .............
I need it to count rows with data in the first column and then when it
comes to the end, to send the document to the printer with only the rows
with data showing.. Any help would be appreciated.
Thanks


Sub findlastrow()
Dim lastrow As Long
Dim r As Range
lastrow = 1
For Each cell In Range(Cells(1, "A"), Cells(Rows.Count, "A").End(xlUp))
If Trim(cell.Text) < "" Then
lastrow = cell.Row
End If
Next
Set r = ActiveSheet.Range("A1:M" & lastrow)
r.PrintPreview
' or
'Activesheet.PageSetup.PrintArea = r.address(external:=true)
'Activesheet.Printout
End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help With - Compile Error: Sub or Function Not Defined MWS Excel Programming 2 March 23rd 06 06:51 PM
VBAProject name compile error, not defined at compile time Matthew Dodds Excel Programming 1 December 13th 05 07:17 PM
Incremental Numbering: Compile Error - Variable not defined BEEJAY Excel Programming 9 September 27th 05 08:59 PM
Variable not defined compile error Phil Hageman[_4_] Excel Programming 4 June 17th 05 01:52 PM
Compile error, variable not defined davegb Excel Programming 5 May 19th 05 04:41 PM


All times are GMT +1. The time now is 07:48 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"