Question

My aim is to have a macro which takes a week's worth of data, pastes the summary/total into another worksheet, then hides/deletes the week I just copied across (or moves it to a different sheet, I don't care). At the end of the year, you should basically have copied each week from Sheet1 to Sheet2

At the moment I have named cells capturing Monday to Friday - I would like that to move across after the macro is run.

What I have so far...

Worksheets("Paste Stuff Here").Range("A1").Value = Worksheets("YearlyDaily").Range("oneweek incl total column").Value

Worksheets("YearlyDaily").Range("Mon-Fri").Select Selection.Delete Shift:=xlToLeft

Was it helpful?

Solution

You can change Named Ranges in the same way you define them:

Option Explicit 

Sub NamedRange() 

    Dim Rng1            As Range 

     'Change the range of cells (A1:B15) to be the range of cells you want to define
    Set Rng1 = Sheets("Sheet1").Range("A1:B15") 
    ActiveWorkbook.Names.Add Name:="MyRange", RefersTo:=Rng1 

End Sub 

[Source]

Yet, as the other answers pointed out, there is probably other (better) ways to handle this. Please tell us more or ask a new question with more information so that we could help you.

OTHER TIPS

I know it's good practice, but is there a reason why you're using a named range for Mon-Fri? If you use cell references, they will always point to the same area, even after the delete. Unless I'm missing something. Difficult to see without seeing your columns.

I'm assuming here that what you mean by "I would like that to move across after the macro is run." is that you want the named ranges to move across.

I have a spreadsheet for tracking time by the week, and the "first" sheet is the active one. When the week is over with, I copy the entire active sheet to a new sheet. There are totals I want carry over from the prior week, so I preserve that data. I do that by having 3 columns: current week total, old (prior weeks') total, new total. I paste values from new total to old total. Then I clear out the cells used for input.

My every instinct was (is) to use named cells, but in this case I concluded it was not the right thing to do. I just used the cell ranges (A1, K2:K0, etc.) The problem is that names are not local to each sheet, so you'd have to move the names from the old sheet to the new, and that is just not worth the effort. Also, in this case, the alternative of creating similar names that are unique to each sheet (e.g., Mon-FriWeek1, Mon-FriWeek2, etc.) doesn't get you anything practical over Week1!A1:A10, etc. -- except maybe self-satisfaction that you used names like you're "supposed" to. That's my opinion anyway!

And maybe this might help: in my spreadsheet rows can be added/deleted from one week to the next. In order to allow for that and still avoid named ranges, I mimic going to a column that has a formula in each row, and pressing Shift + End then the down arrow. The selection will stop on the last cell that has something entered. Here's the code for that (the first row is a header/label):

    'Copy New Total Hours as PasteValues to Old Total
    Dim lastRow As Long
    Range("K1").Select
    Range(Selection, Selection.End(xlDown)).Select
    lastRow = Selection.Rows(Selection.Rows.Count).Row + 1
    Range("K2:K" & CStr(lastRow)).Select
    Selection.Copy
    Range("J2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

You do need to keep at least one row in there, or it (and a great many other things in my case) doesn't work. But that suits my needs just fine.

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top