Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Copy selection based on relative reference

I've recorded a macro, but am having trouble on one part. I know very little
about VBA, but am sure this is a simple fix. Each week my data set grows by
one row. This week the data set consists of 33 rows, however, I only want to
copy 32 rows. To say it another way, I always want to copy all rows EXCEPT
the last one. When recording the macro, I turned relative reference on. I
then used CTRL+SHIFT+DOWN, then SHIFT+RIGHT (because I want 2 columns), then
SHIFT+UP (to un-select the last row). This highlights my range, and then I
copy. When running my macro this week, I noticed it only grabbed the same 32
rows from last week. Sure enough, the VBA code specified A1:B32. How can I
alter the code to grow with the data? Below is the recorded VBA for this
part.

Windows("Order Tracking.xls").Activate
Selection.End(xlDown).Select
ActiveCell.Offset(1, 1).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveCell.Range("A1:B32").Select
Selection.Copy

Any help would be appreciated.

Jeff




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Copy selection based on relative reference

Try this:
Replace
ActiveCell.Range("A1:B32").Select
Selection.Copy

with
Selection.Resize(Selection.Rows.Count - 1, Selection.Columns.Count).Copy

-Reuel


"goofy11" wrote:

I've recorded a macro, but am having trouble on one part. I know very little
about VBA, but am sure this is a simple fix. Each week my data set grows by
one row. This week the data set consists of 33 rows, however, I only want to
copy 32 rows. To say it another way, I always want to copy all rows EXCEPT
the last one. When recording the macro, I turned relative reference on. I
then used CTRL+SHIFT+DOWN, then SHIFT+RIGHT (because I want 2 columns), then
SHIFT+UP (to un-select the last row). This highlights my range, and then I
copy. When running my macro this week, I noticed it only grabbed the same 32
rows from last week. Sure enough, the VBA code specified A1:B32. How can I
alter the code to grow with the data? Below is the recorded VBA for this
part.

Windows("Order Tracking.xls").Activate
Selection.End(xlDown).Select
ActiveCell.Offset(1, 1).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveCell.Range("A1:B32").Select
Selection.Copy

Any help would be appreciated.

Jeff




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Copy selection based on relative reference

With Workbooks("Order Tracking.xls").ActiveSheet _
.Range("A1").CurrentRegion
.Resize(.Rows.count-1,2).Copy
End With

--
Regards,
Tom Ogilvy


"goofy11" wrote in message
...
I've recorded a macro, but am having trouble on one part. I know very

little
about VBA, but am sure this is a simple fix. Each week my data set grows

by
one row. This week the data set consists of 33 rows, however, I only want

to
copy 32 rows. To say it another way, I always want to copy all rows

EXCEPT
the last one. When recording the macro, I turned relative reference on.

I
then used CTRL+SHIFT+DOWN, then SHIFT+RIGHT (because I want 2 columns),

then
SHIFT+UP (to un-select the last row). This highlights my range, and then

I
copy. When running my macro this week, I noticed it only grabbed the same

32
rows from last week. Sure enough, the VBA code specified A1:B32. How can

I
alter the code to grow with the data? Below is the recorded VBA for this
part.

Windows("Order Tracking.xls").Activate
Selection.End(xlDown).Select
ActiveCell.Offset(1, 1).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveCell.Range("A1:B32").Select
Selection.Copy

Any help would be appreciated.

Jeff






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Copy selection based on relative reference

Thanks, that did the trick!

"Reuel" wrote:

Try this:
Replace
ActiveCell.Range("A1:B32").Select
Selection.Copy

with
Selection.Resize(Selection.Rows.Count - 1, Selection.Columns.Count).Copy

-Reuel


"goofy11" wrote:

I've recorded a macro, but am having trouble on one part. I know very little
about VBA, but am sure this is a simple fix. Each week my data set grows by
one row. This week the data set consists of 33 rows, however, I only want to
copy 32 rows. To say it another way, I always want to copy all rows EXCEPT
the last one. When recording the macro, I turned relative reference on. I
then used CTRL+SHIFT+DOWN, then SHIFT+RIGHT (because I want 2 columns), then
SHIFT+UP (to un-select the last row). This highlights my range, and then I
copy. When running my macro this week, I noticed it only grabbed the same 32
rows from last week. Sure enough, the VBA code specified A1:B32. How can I
alter the code to grow with the data? Below is the recorded VBA for this
part.

Windows("Order Tracking.xls").Activate
Selection.End(xlDown).Select
ActiveCell.Offset(1, 1).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveCell.Range("A1:B32").Select
Selection.Copy

Any help would be appreciated.

Jeff




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
How do I use a relative cell reference that will copy the format? Brian Excel Discussion (Misc queries) 4 January 13th 09 10:49 PM
How do I copy a combobox to many cells with relative reference? Levc Excel Discussion (Misc queries) 1 November 21st 07 06:58 PM
Copy data from pivot table using relative reference hello Excel Discussion (Misc queries) 5 April 9th 07 04:13 PM
Copy a relative reference formula from one sheet to another. jannkatt Excel Discussion (Misc queries) 3 May 17th 06 07:13 PM
Excel - Copy range of cells based on Combobox Selection Excel-erate2004 Excel Programming 2 April 3rd 04 05:35 PM


All times are GMT +1. The time now is 12:59 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"