PC Review


Reply
Thread Tools Rate Thread

Copying a Range When Rows Are Long Variables

 
 
=?Utf-8?B?QUxBVEw=?=
Guest
Posts: n/a
 
      23rd Aug 2007
I have the following code that does the following:

*Sorts data according to one column B
*While the SOB values are all equal in Column B, it goes through data and
identifies the bottom and top position of each row and stores it in a Long
TopRowPos and Long BottomRowPos variable.

I would like to copy that range of data in TopRowPos and Long BottomRowPos
but can't because it is not a range value. There is a type mismatch between
the long & range variables and I can't assign them.

Does anyone have any suggestions?


Dim BottomRowPos As Long
Dim TopRowPos As Long
Dim i As Long
Dim SOB As String
Dim Same As Boolean
Dim WS As Worksheet
Dim TopRange As Range
Dim BottomRange As Range


'Sort data by Set of Books
Set WS = Worksheets("Sheet1")

Cells.Select
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

'Identify Range for Each Set of Books
BottomRowPos = WS.Range("B65536").End(xlUp).Row

While BottomRowPos > 1
TopRowPos = BottomRowPos

'While the SOB values are equal
While WS.Cells(TopRowPos - 1, 2) = WS.Cells(BottomRowPos, 2)
TopRowPos = TopRowPos - 1
Wend

Same = True

'Set SOB to BottomRowPos
SOB = WS.Cells(BottomRowPos, 2)

For i = TopRowPos To BottomRowPos
If WS.Cells(i, 2) <> SOB Then Same = False
If Not Same Then i = BottomRowPos
Next i

'Executes if Same is True. Copy range of SOB to new worksheet
**This is where I need help**
**I have a top & bottom row position but i don't know how to tell it to copy
& paste to a new worksheet**

Thx!
 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      23rd Aug 2007
set rng = ws.Range(ws.cells(TopRowPos,2),ws.Cells(BottmRowPos,2))


rng.copy

or

rng.Resize(,10).copy to copy 10 columns wide as an example - adjust to
fit your needs.

--
Regards,
Tom Ogilvy





"ALATL" wrote:

> I have the following code that does the following:
>
> *Sorts data according to one column B
> *While the SOB values are all equal in Column B, it goes through data and
> identifies the bottom and top position of each row and stores it in a Long
> TopRowPos and Long BottomRowPos variable.
>
> I would like to copy that range of data in TopRowPos and Long BottomRowPos
> but can't because it is not a range value. There is a type mismatch between
> the long & range variables and I can't assign them.
>
> Does anyone have any suggestions?
>
>
> Dim BottomRowPos As Long
> Dim TopRowPos As Long
> Dim i As Long
> Dim SOB As String
> Dim Same As Boolean
> Dim WS As Worksheet
> Dim TopRange As Range
> Dim BottomRange As Range
>
>
> 'Sort data by Set of Books
> Set WS = Worksheets("Sheet1")
>
> Cells.Select
> Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlYes, _
> OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
> DataOption1:=xlSortNormal
>
> 'Identify Range for Each Set of Books
> BottomRowPos = WS.Range("B65536").End(xlUp).Row
>
> While BottomRowPos > 1
> TopRowPos = BottomRowPos
>
> 'While the SOB values are equal
> While WS.Cells(TopRowPos - 1, 2) = WS.Cells(BottomRowPos, 2)
> TopRowPos = TopRowPos - 1
> Wend
>
> Same = True
>
> 'Set SOB to BottomRowPos
> SOB = WS.Cells(BottomRowPos, 2)
>
> For i = TopRowPos To BottomRowPos
> If WS.Cells(i, 2) <> SOB Then Same = False
> If Not Same Then i = BottomRowPos
> Next i
>
> 'Executes if Same is True. Copy range of SOB to new worksheet
> **This is where I need help**
> **I have a top & bottom row position but i don't know how to tell it to copy
> & paste to a new worksheet**
>
> Thx!

 
Reply With Quote
 
=?Utf-8?B?QUxBVEw=?=
Guest
Posts: n/a
 
      23rd Aug 2007
hmm..... this isn't quite giving me the result i am looking for.

The copy seems to work as I see all of the cells selected but it never
pastes anything. I will need it to paste to the next empty cell in the
destination worksheet.

I appreciate your time!

"Tom Ogilvy" wrote:

> set rng = ws.Range(ws.cells(TopRowPos,2),ws.Cells(BottmRowPos,2))
>
>
> rng.copy
>
> or
>
> rng.Resize(,10).copy to copy 10 columns wide as an example - adjust to
> fit your needs.
>
> --
> Regards,
> Tom Ogilvy
>
>
>
>
>
> "ALATL" wrote:
>
> > I have the following code that does the following:
> >
> > *Sorts data according to one column B
> > *While the SOB values are all equal in Column B, it goes through data and
> > identifies the bottom and top position of each row and stores it in a Long
> > TopRowPos and Long BottomRowPos variable.
> >
> > I would like to copy that range of data in TopRowPos and Long BottomRowPos
> > but can't because it is not a range value. There is a type mismatch between
> > the long & range variables and I can't assign them.
> >
> > Does anyone have any suggestions?
> >
> >
> > Dim BottomRowPos As Long
> > Dim TopRowPos As Long
> > Dim i As Long
> > Dim SOB As String
> > Dim Same As Boolean
> > Dim WS As Worksheet
> > Dim TopRange As Range
> > Dim BottomRange As Range
> >
> >
> > 'Sort data by Set of Books
> > Set WS = Worksheets("Sheet1")
> >
> > Cells.Select
> > Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlYes, _
> > OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
> > DataOption1:=xlSortNormal
> >
> > 'Identify Range for Each Set of Books
> > BottomRowPos = WS.Range("B65536").End(xlUp).Row
> >
> > While BottomRowPos > 1
> > TopRowPos = BottomRowPos
> >
> > 'While the SOB values are equal
> > While WS.Cells(TopRowPos - 1, 2) = WS.Cells(BottomRowPos, 2)
> > TopRowPos = TopRowPos - 1
> > Wend
> >
> > Same = True
> >
> > 'Set SOB to BottomRowPos
> > SOB = WS.Cells(BottomRowPos, 2)
> >
> > For i = TopRowPos To BottomRowPos
> > If WS.Cells(i, 2) <> SOB Then Same = False
> > If Not Same Then i = BottomRowPos
> > Next i
> >
> > 'Executes if Same is True. Copy range of SOB to new worksheet
> > **This is where I need help**
> > **I have a top & bottom row position but i don't know how to tell it to copy
> > & paste to a new worksheet**
> >
> > Thx!

 
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
Question about copying long rows of data Jim Radtke Microsoft Excel Misc 2 14th May 2009 05:15 PM
Question about copying long rows of data Jim Radtke Microsoft Excel Misc 0 14th May 2009 05:02 PM
Error Copying Range/Cell with long Text String MikeZz Microsoft Excel Programming 1 31st Jul 2008 03:20 AM
Selecting Rows using Long Variables pallaver Microsoft Excel Misc 2 14th Jul 2008 07:13 AM
Grouping Rows with Range Variables Dan G. Microsoft Excel Programming 1 11th Mar 2006 07:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:30 AM.