Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copying a variable nuimber of rows

I want to copy the most recent 10 rows (excluding the header row) from
worksheet A to worksheet B. My problem is how do I define a variable number
of rows if there are fewer than 10 lines completed? I do not want to copy
any blank lines. I am fairly new to VBA and am struggling to work out the
code for this so any help will be gratefully appreciated. John
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 834
Default Copying a variable nuimber of rows

Try

With Activesheet

LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
if LastRow 10 Then

FirstRow = LastRow - 9
Else

FirstRow = 1
EndIf

.Rows(FirstRow).Resize(LastRow - FirstRow).Copy
'then paste it wherever
End With

--

HTH

Bob

"FinMan@Sussex" wrote in message
...
I want to copy the most recent 10 rows (excluding the header row) from
worksheet A to worksheet B. My problem is how do I define a variable
number
of rows if there are fewer than 10 lines completed? I do not want to copy
any blank lines. I am fairly new to VBA and am struggling to work out the
code for this so any help will be gratefully appreciated. John



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Copying a variable nuimber of rows

Hi,

Try this, you need to change srcsht and dstsht to the correct sheet names

Sub copyrows()
Dim LastRow As Long
Set SrcSht = Sheets("Sheet1")
Set DstSht = Sheets("Sheet2")
LastRow = SrcSht.UsedRange.SpecialCells(xlCellTypeLastCell). Row
SrcSht.Rows(WorksheetFunction.Max(2, LastRow - 9) & ":" & LastRow).Copy _
Destination:=DstSht.Range("A1")
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"FinMan@Sussex" wrote:

I want to copy the most recent 10 rows (excluding the header row) from
worksheet A to worksheet B. My problem is how do I define a variable number
of rows if there are fewer than 10 lines completed? I do not want to copy
any blank lines. I am fairly new to VBA and am struggling to work out the
code for this so any help will be gratefully appreciated. John

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Copying a variable nuimber of rows

Just out of curiosity, what would be the problem in copying blank lines
along with your data lines (in the case when there were less than 10
completed lines)?

--
Rick (MVP - Excel)



"FinMan@Sussex" wrote in message
...
I want to copy the most recent 10 rows (excluding the header row) from
worksheet A to worksheet B. My problem is how do I define a variable
number
of rows if there are fewer than 10 lines completed? I do not want to copy
any blank lines. I am fairly new to VBA and am struggling to work out the
code for this so any help will be gratefully appreciated. John


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
Autofil on variable rows, delete extract and show remaining rows 1plane Excel Programming 2 November 16th 09 09:46 PM
Inserting and copying Rows based on a variable Darwin[_2_] Excel Programming 2 September 25th 09 10:33 AM
Copying (variable) rows from another spreadsheet Tommy Excel Programming 1 January 6th 09 02:02 PM
Macro for copying a value to a variable no. of rows Ian Grega Excel Discussion (Misc queries) 6 April 15th 08 02:48 PM
Copying range to variable number of rows nospaminlich Excel Programming 2 December 1st 05 11:34 PM


All times are GMT +1. The time now is 07:20 AM.

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"