PC Review


Reply
Thread Tools Rate Thread

Copy to Next Blank Row

 
 
Jeff Gross
Guest
Posts: n/a
 
      8th Jun 2009
I need to copy from one worksheet to the next blank row on another worksheet
but I keep getting an error on the below code at the line "Lr =
lastrow(DestSheet)". The error is a compile error (sub or function not
defined). Can anyone help?

Thanks ahead.

Sub copy_1()
Dim SourceRange As Range
Dim DestRange As Range
Dim DestSheet As Worksheet
Dim Lr As Long

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set SourceRange = Sheets("Data Sort 1b").Range("A2300")
Set DestSheet = Sheets("Data Sort 1c")
Lr = lastrow(DestSheet)

Set DestRange = DestSheet.Range("A" & Lr + 1)
SourceRange.Copy DestRange

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      8th Jun 2009
Go back to Ron's site (where you got this code) and look for the function.
Either it's on the same page or there's a link to it on the page where it was
used.



Jeff Gross wrote:
>
> I need to copy from one worksheet to the next blank row on another worksheet
> but I keep getting an error on the below code at the line "Lr =
> lastrow(DestSheet)". The error is a compile error (sub or function not
> defined). Can anyone help?
>
> Thanks ahead.
>
> Sub copy_1()
> Dim SourceRange As Range
> Dim DestRange As Range
> Dim DestSheet As Worksheet
> Dim Lr As Long
>
> With Application
> .ScreenUpdating = False
> .EnableEvents = False
> End With
>
> Set SourceRange = Sheets("Data Sort 1b").Range("A2300")
> Set DestSheet = Sheets("Data Sort 1c")
> Lr = lastrow(DestSheet)
>
> Set DestRange = DestSheet.Range("A" & Lr + 1)
> SourceRange.Copy DestRange
>
> With Application
> .ScreenUpdating = True
> .EnableEvents = True
> End With
>
> End Sub


--

Dave Peterson
 
Reply With Quote
 
Jim Thomlinson
Guest
Posts: n/a
 
      8th Jun 2009
LastRow is not a defined function in VBA. You need to write your own
function. Here is one that I use to find the last cell in a worksheet. Try
this...

Sub copy_1()
Dim SourceRange As Range
Dim DestRange As Range
Dim DestSheet As Worksheet
Dim Lr As Long

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set SourceRange = Sheets("Data Sort 1b").Range("A2300")
Set DestSheet = Sheets("Data Sort 1c")
Lr = LastCell(DestSheet).row

Set DestRange = DestSheet.Range("A" & Lr + 1)
SourceRange.Copy DestRange

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub

Public Function LastCell(Optional ByVal wks As Worksheet) As Range
Dim lngLastRow As Long
Dim lngLastColumn As Long

If wks Is Nothing Then Set wks = ActiveSheet
On Error Resume Next
lngLastRow = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
lngLastColumn = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
If lngLastRow = 0 Then
lngLastRow = 1
lngLastColumn = 1
End If
Set LastCell = wks.Cells(lngLastRow, lngLastColumn)
End Function
--
HTH...

Jim Thomlinson


"Jeff Gross" wrote:

> I need to copy from one worksheet to the next blank row on another worksheet
> but I keep getting an error on the below code at the line "Lr =
> lastrow(DestSheet)". The error is a compile error (sub or function not
> defined). Can anyone help?
>
> Thanks ahead.
>
> Sub copy_1()
> Dim SourceRange As Range
> Dim DestRange As Range
> Dim DestSheet As Worksheet
> Dim Lr As Long
>
> With Application
> .ScreenUpdating = False
> .EnableEvents = False
> End With
>
> Set SourceRange = Sheets("Data Sort 1b").Range("A2300")
> Set DestSheet = Sheets("Data Sort 1c")
> Lr = lastrow(DestSheet)
>
> Set DestRange = DestSheet.Range("A" & Lr + 1)
> SourceRange.Copy DestRange
>
> With Application
> .ScreenUpdating = True
> .EnableEvents = True
> End With
>
> End Sub

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      8th Jun 2009
I have found that these simpler Find methods work just as well for finding
the last row and column with data...

LastUsedRow = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious).Row

LastUsedCol = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column

You can, of course, change the ActiveSheet reference to a specific worksheet
reference; so, in your LastCell function code, these statements would
become...

lngLastRow = wks.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious).Row

lngLastColumn = wks.Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column

--
Rick (MVP - Excel)


"Jim Thomlinson" <James_Thomlinson@owfg-Re-Move-This-.com> wrote in message
news:25F9A623-B1DC-43C9-91FD-(E-Mail Removed)...
> LastRow is not a defined function in VBA. You need to write your own
> function. Here is one that I use to find the last cell in a worksheet. Try
> this...
>
> Sub copy_1()
> Dim SourceRange As Range
> Dim DestRange As Range
> Dim DestSheet As Worksheet
> Dim Lr As Long
>
> With Application
> .ScreenUpdating = False
> .EnableEvents = False
> End With
>
> Set SourceRange = Sheets("Data Sort 1b").Range("A2300")
> Set DestSheet = Sheets("Data Sort 1c")
> Lr = LastCell(DestSheet).row
>
> Set DestRange = DestSheet.Range("A" & Lr + 1)
> SourceRange.Copy DestRange
>
> With Application
> .ScreenUpdating = True
> .EnableEvents = True
> End With
>
> End Sub
>
> Public Function LastCell(Optional ByVal wks As Worksheet) As Range
> Dim lngLastRow As Long
> Dim lngLastColumn As Long
>
> If wks Is Nothing Then Set wks = ActiveSheet
> On Error Resume Next
> lngLastRow = wks.Cells.Find(What:="*", _
> After:=wks.Range("A1"), _
> Lookat:=xlPart, _
> LookIn:=xlFormulas, _
> SearchOrder:=xlByRows, _
> SearchDirection:=xlPrevious, _
> MatchCase:=False).Row
> lngLastColumn = wks.Cells.Find(What:="*", _
> After:=wks.Range("A1"), _
> Lookat:=xlPart, _
> LookIn:=xlFormulas, _
> SearchOrder:=xlByColumns, _
> SearchDirection:=xlPrevious, _
> MatchCase:=False).Column
> On Error GoTo 0
> If lngLastRow = 0 Then
> lngLastRow = 1
> lngLastColumn = 1
> End If
> Set LastCell = wks.Cells(lngLastRow, lngLastColumn)
> End Function
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Jeff Gross" wrote:
>
>> I need to copy from one worksheet to the next blank row on another
>> worksheet
>> but I keep getting an error on the below code at the line "Lr =
>> lastrow(DestSheet)". The error is a compile error (sub or function not
>> defined). Can anyone help?
>>
>> Thanks ahead.
>>
>> Sub copy_1()
>> Dim SourceRange As Range
>> Dim DestRange As Range
>> Dim DestSheet As Worksheet
>> Dim Lr As Long
>>
>> With Application
>> .ScreenUpdating = False
>> .EnableEvents = False
>> End With
>>
>> Set SourceRange = Sheets("Data Sort 1b").Range("A2300")
>> Set DestSheet = Sheets("Data Sort 1c")
>> Lr = lastrow(DestSheet)
>>
>> Set DestRange = DestSheet.Range("A" & Lr + 1)
>> SourceRange.Copy DestRange
>>
>> With Application
>> .ScreenUpdating = True
>> .EnableEvents = True
>> End With
>>
>> End Sub


 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      8th Jun 2009
You can read this on top of the page
http://www.rondebruin.nl/copy1.htm

Important: The macro examples use one function or two functions that
you can find in the last section of this page.


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm




"Jeff Gross" <(E-Mail Removed)> wrote in message news:068F86AA-EF05-4E21-8E8D-(E-Mail Removed)...
>I need to copy from one worksheet to the next blank row on another worksheet
> but I keep getting an error on the below code at the line "Lr =
> lastrow(DestSheet)". The error is a compile error (sub or function not
> defined). Can anyone help?
>
> Thanks ahead.
>
> Sub copy_1()
> Dim SourceRange As Range
> Dim DestRange As Range
> Dim DestSheet As Worksheet
> Dim Lr As Long
>
> With Application
> .ScreenUpdating = False
> .EnableEvents = False
> End With
>
> Set SourceRange = Sheets("Data Sort 1b").Range("A2300")
> Set DestSheet = Sheets("Data Sort 1c")
> Lr = lastrow(DestSheet)
>
> Set DestRange = DestSheet.Range("A" & Lr + 1)
> SourceRange.Copy DestRange
>
> With Application
> .ScreenUpdating = True
> .EnableEvents = True
> End With
>
> End Sub

 
Reply With Quote
 
Jim Thomlinson
Guest
Posts: n/a
 
      8th Jun 2009
Thanks... I have been using that code for so long without looking at it that
I never really noticed. One thing I have change din the past was xlformulas
and xlvalues so here is my new code...

Public Function LastCell(Optional ByVal wks As Worksheet, _
Optional ByVal blnConstantsOnly As Boolean) As Range
Dim lngLastRow As Long
Dim lngLastColumn As Long
Dim lngLookIn As Long

If blnConstantsOnly = True Then
lngLookIn = xlValues
Else
lngLookIn = xlFormulas
End If

If wks Is Nothing Then Set wks = ActiveSheet
On Error Resume Next
lngLastRow = wks.Cells.Find(What:="*", _
LookIn:=lngLookIn, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
lngLastColumn = wks.Cells.Find(What:="*", _
LookIn:=lngLookIn, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
On Error GoTo 0
If lngLastRow = 0 Then
lngLastRow = 1
lngLastColumn = 1
End If
Set LastCell = wks.Cells(lngLastRow, lngLastColumn)
End Function
--
HTH...

Jim Thomlinson


"Rick Rothstein" wrote:

> I have found that these simpler Find methods work just as well for finding
> the last row and column with data...
>
> LastUsedRow = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlRows, _
> SearchDirection:=xlPrevious).Row
>
> LastUsedCol = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlByColumns, _
> SearchDirection:=xlPrevious).Column
>
> You can, of course, change the ActiveSheet reference to a specific worksheet
> reference; so, in your LastCell function code, these statements would
> become...
>
> lngLastRow = wks.Cells.Find(What:="*", SearchOrder:=xlRows, _
> SearchDirection:=xlPrevious).Row
>
> lngLastColumn = wks.Cells.Find(What:="*", SearchOrder:=xlByColumns, _
> SearchDirection:=xlPrevious).Column
>
> --
> Rick (MVP - Excel)
>
>
> "Jim Thomlinson" <James_Thomlinson@owfg-Re-Move-This-.com> wrote in message
> news:25F9A623-B1DC-43C9-91FD-(E-Mail Removed)...
> > LastRow is not a defined function in VBA. You need to write your own
> > function. Here is one that I use to find the last cell in a worksheet. Try
> > this...
> >
> > Sub copy_1()
> > Dim SourceRange As Range
> > Dim DestRange As Range
> > Dim DestSheet As Worksheet
> > Dim Lr As Long
> >
> > With Application
> > .ScreenUpdating = False
> > .EnableEvents = False
> > End With
> >
> > Set SourceRange = Sheets("Data Sort 1b").Range("A2300")
> > Set DestSheet = Sheets("Data Sort 1c")
> > Lr = LastCell(DestSheet).row
> >
> > Set DestRange = DestSheet.Range("A" & Lr + 1)
> > SourceRange.Copy DestRange
> >
> > With Application
> > .ScreenUpdating = True
> > .EnableEvents = True
> > End With
> >
> > End Sub
> >
> > Public Function LastCell(Optional ByVal wks As Worksheet) As Range
> > Dim lngLastRow As Long
> > Dim lngLastColumn As Long
> >
> > If wks Is Nothing Then Set wks = ActiveSheet
> > On Error Resume Next
> > lngLastRow = wks.Cells.Find(What:="*", _
> > After:=wks.Range("A1"), _
> > Lookat:=xlPart, _
> > LookIn:=xlFormulas, _
> > SearchOrder:=xlByRows, _
> > SearchDirection:=xlPrevious, _
> > MatchCase:=False).Row
> > lngLastColumn = wks.Cells.Find(What:="*", _
> > After:=wks.Range("A1"), _
> > Lookat:=xlPart, _
> > LookIn:=xlFormulas, _
> > SearchOrder:=xlByColumns, _
> > SearchDirection:=xlPrevious, _
> > MatchCase:=False).Column
> > On Error GoTo 0
> > If lngLastRow = 0 Then
> > lngLastRow = 1
> > lngLastColumn = 1
> > End If
> > Set LastCell = wks.Cells(lngLastRow, lngLastColumn)
> > End Function
> > --
> > HTH...
> >
> > Jim Thomlinson
> >
> >
> > "Jeff Gross" wrote:
> >
> >> I need to copy from one worksheet to the next blank row on another
> >> worksheet
> >> but I keep getting an error on the below code at the line "Lr =
> >> lastrow(DestSheet)". The error is a compile error (sub or function not
> >> defined). Can anyone help?
> >>
> >> Thanks ahead.
> >>
> >> Sub copy_1()
> >> Dim SourceRange As Range
> >> Dim DestRange As Range
> >> Dim DestSheet As Worksheet
> >> Dim Lr As Long
> >>
> >> With Application
> >> .ScreenUpdating = False
> >> .EnableEvents = False
> >> End With
> >>
> >> Set SourceRange = Sheets("Data Sort 1b").Range("A2300")
> >> Set DestSheet = Sheets("Data Sort 1c")
> >> Lr = lastrow(DestSheet)
> >>
> >> Set DestRange = DestSheet.Range("A" & Lr + 1)
> >> SourceRange.Copy DestRange
> >>
> >> With Application
> >> .ScreenUpdating = True
> >> .EnableEvents = True
> >> End With
> >>
> >> End Sub

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Blank Copy =?Utf-8?B?SWRvaWE=?= Microsoft Outlook Discussion 1 5th Sep 2007 10:53 AM
Need macro to check if cell is not blank & previous cell is blank, copy information from row above & paste JenIT Microsoft Excel Programming 4 12th Apr 2007 08:56 PM
Copy to first Blank cell in Colum C Non blank cells still exist be =?Utf-8?B?VWxyaWsgbG92ZXMgaG9yc2Vz?= Microsoft Excel Programming 2 8th Oct 2006 07:35 PM
COPY A CONCATENATE CELL TO BLANK CELL PUTTING IN THE NEXT BLANK C. =?Utf-8?B?UVVFU1Q0MTA2Nw==?= Microsoft Excel Misc 1 15th Jan 2005 09:29 PM
Copy Until Blank Alec Microsoft Excel Programming 5 20th Oct 2003 03:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:27 AM.