Range target different each day

g48dd

Board Regular
Joined
Jan 12, 2009
Messages
101
Excel 2003: I have a range H17:BC170 (on sheet 1), that everyday I have to copy and paste special values only to Sheet 2. It is a daily record so when I paste it to sheet 2 I have to paste it in a new spot. Like this Copy sheet 1 H17:BC170, sheet 2 paste special A10, next day the range copied will be the same but the target will be different, it will be yesterdays range plus 4 rows, so I will paste to A167 and the next day A323. Until I reach the end of the month, at which point I will change columns and start pasting at AX10. I don't need something that knows when the end of the month is, I can go in and tweak it so that it starts pasting in AX instead of A, I do this manually everyday and I can write a macro that copies and paste special to another sheet using the recorder, I don't know how to tell it skip where I pasted before, give me 3 blank rows and paste special again;

Code:
Sub Move Records()

    Application.Goto Reference:="R17C8:R170C55"
    Selection.Copy
    Sheets("Sheet2").Select
    Range("A10").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A1").Select
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Maybe something like
Code:
Sub MoveRecords()
 Dim lr As Long
 lr = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
    Application.Goto Reference:="R17C8:R170C55"
    Selection.Copy
    Sheets("Sheet2").Range("A" & lr + 4).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A1").Select
End Sub
 
Upvote 0
I am getting this when I run it:

Excel is still on sheet 1, the correct range is highlighted, it should be getting ready to copy and move to Sheet 2 and I get a run time error 1004, selected method of range class failed and this line is highlighted

Sheets("Sheet2").Range("A" & lr + 4).Select

Ken
 
Upvote 0
Sorry Ken, had a shocker
try this
Code:
Sub MoveRecords()
 Dim lr As Long
 lr = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
    Application.Goto Reference:="R17C8:R170C55"
    Selection.Copy
    Sheets("Sheet2").Range("A" & lr + 4).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
    Range("A1").Select
End Sub
 
Upvote 0
Thanks, it does exactly what I need it to do, I am just beginning to teach myself VBA, I have learned that there is a VBA library by using F2, and that helps a bit in figuring out how it all works. I also have two books, I have to learn how to think like someone who writes VBA.

Ken
 
Upvote 0
A couple of tips then....
1. Hang around here and look at other posts. Copy the code, see how it works and then try changing stuff to see what happens.
2. Ask questions here and of other users
3. Use the macro recorder to do stuff, and then try and clean up the macro code...mainly 'cause recorded code has heaps of useless code in it.

4. And finally, here is a list of VBA sites and stuff kindly provided by Hiker95

Code:
How to Learn to Write Macros 
http://articles.excelyogi.com/playin...ba/2008/10/27/

How to use the macro recorder 
http://articles.excelyogi.com/

Click here and scroll down to Getting Started with VBA. 
http://www.datapigtechnologies.com/ExcelMain.htm

If you are serious about learning VBA try 
http://www.add-ins.com/vbhelp.htm

Excel Tutorials and Tips - VBA - macros - training 
http://www.mrexcel.com/articles.shtml

See David McRitchie's site if you just started with VBA
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Debra Dalgleish has some notes how to implement macros here:
http://www.contextures.com/xlvba01.html

David McRitchie has an intro to macros:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Ron de Bruin's intro to macros:
http://www.rondebruin.nl/code.htm

Creating custom functions
http://office.microsoft.com/en-us/ex...117011033.aspx

Writing Your First VBA Function in Excel
http://www.exceltip.com/st/Writing_Y...Excel/631.html

http://www.excel-vba.com/
http://www.mvps.org/dmcritchie/excel/getstarted.htm
http://www.exceltip.com/excel_links.html

(livelessons video)
Excel VBA and Macros with MrExcel
ISBN: 0-7897-3938-0
http://www.amazon.com/Excel-Macros-M...7936479&sr=1-1

http://www.xl-central.com/index.html

http://www.datapigtechnologies.com/ExcelMain.htm

Dependent validation lists. Debra has a neat little tutorial here.
http://www.contextures.com/xlDataVal02.html 

Cascading queries
http://www.tushar-mehta.com/excel/ne...ing_dropdowns/

http://www.contextures.com/xlDataVal05.html 


Excel Data Validation - Add New Items
http://www.contextures.com/excel-dat...ation-add.html

Programming The VBA Editor - Created by Chip Pearson at Pearson Software Consulting LLC
This page describes how to write code that modifies or reads other VBA code.
http://www.cpearson.com/Excel/vbe.aspx

Locating files containing VBA
Searching Files in Subfolders for VBA code string:
http://www.dailydoseofexcel.com/arch...a-code-string/

http://www.pcreview.co.uk/forums/thread-978054.php

Excel 2003 Power Programming with VBA, by John Walkenbach

VBA and Macros for Microsoft Excel, by Bill Jelen "Mr.Excel" and Tracy Syrstad

Excel Hacks 100 Industrial-Strength Tips & Tools, by David & Traina Hawley

VBA and Macros for Microsoft Excel 2007, by Bill Jelen "Mr.Excel" and Tracy Syrstad

Excel 2007 Book: you can try this...there is a try before you buy ebook available at this link…
http://www.mrexcel.com/learnexcel2.shtml 

DonkeyOte: My Recommended Reading:

Volatility
http://www.decisionmodels.com/calcsecretsi.htm

Sumproduct
http://www.xldynamic.com/source/xld.SUMPRODUCT.html

Arrays
http://www.xtremevbtalk.com/showthread.php?t=296012

Pivot Intro
http://peltiertech.com/Excel/Pivots/pivotstart.htm

Email from XL - VBA
http://www.rondebruin.nl/sendmail.htm

Outlook VBA
http://www.outlookcode.com/article.aspx?ID=40

Function Dictionary
http://www.xlfdic.com/

Function Translations
http://www.piuha.fi/excel-function-name-translation/

Dynamic Named Ranges
http://www.contextures.com/xlNames01.html

And, as your skills increase, try answering posts on sites like:
http://www.mrexcel.com/
http://www.excelforum.com/
http://www.ozgrid.com/
http://www.vbaexpress.com/portal.php
http://p2p.wrox.com/excel-vba-79/


Where to paste code in VBE VBA
Introducing the Excel VBA Editor
http://www.ask.com/web?qsrc=2417&o=1...cel+VBA+Editor

VBA for Excel (Macros)
http://www.excel-vba.com/excel-vba-contents.htm

VBA Lesson 11: VBA Code General Tips and General Vocabulary
http://www.excel-vba.com/vba-code-2-1-tips.htm

Excel VBA -- Adding Code to a Workbook
http://www.contextures.com/xlvba01.html 

Explanation and Examples of VBA
http://www.beyondtechnology.com/vba.shtml

Training for Excel, Access, VBA, with free downloads and course notes
http://www.fontstuff.com/excel/index.htm

VBA samples for Excel and outlook, couple of video tutorials

http://www.codeforexcelandoutlook.com

More VBA samples
http://msmvps.com/blogs/nateoliver

Simple short VBA for beginners with explanations
http://www.angelfire.com/biz7/julian_s/julian/julians_macros.htm#J
 
Upvote 0
Michael I am having a problem with it that I can not figure out. When I get a new script I always run it in a new workbook first. First I run it just like it is written in the forum, then I name the tabs, and put fake data in, and tweak the script as if it is in the SS that I am going to use it with. It ran fine. So I went to insert new module and paste the script. I copy it from the workbook it is working in. This script does everything except it does not move to new range + 3 rows, it just paste over what was there before. I can't figure it out because it runs just fine in the test work book. Here is the tweaked script, I copied this from the workbook that it does not run correctly in:

Code:
Sub MoveRecords()
 Dim lr As Long
 lr = Sheets("Records").Cells(Rows.Count, "A").End(xlUp).Row
    Application.Goto Reference:="R15C8:R170C55"
    Selection.Copy
    Sheets("Records").Range("A" & lr + 4).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
    Range("A1").Select
End Sub
The only thing I adjusted was I changed the name of the tab from sheet2 to Records and I changed the GoTo Reference from R17C8 to R15C8.

Ken:(
 
Upvote 0
Hi Ken
That's because your running the code while {"Records") sheet is active !!
Sheet1 needs to be the active sheet
I've adjusted the code to suit
Code:
Sub MoveRecords()
 Dim lr As Long
 lr = Sheets("Records").Cells(Rows.Count, "A").End(xlUp).Row
    Sheets("Sheet1").Activate 'added this line to activate Sheet1
    Application.Goto Reference:="R15C8:R170C55"
    Selection.Copy
    Sheets("Records").Range("A" & lr + 4).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
    Range("A1").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,242
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top