Solved

Simple Macro - I want to repeat screen moves, not select "ranges"

Posted on 2013-06-17
11
263 Views
Last Modified: 2013-06-18
I'm simply trying to replicate identical screen moves of a cursor.

I have 2 columns of data.
The last row of this data has formulas in it.
When I run the macro, I simply want to copy that last row of formulas, to the row
directly beneath it.
Then, turn the original last row (now the next to the last row) into values, by doing a
"Paste value".
When recording the macro, this simply does the same actions to the same cells, (even though I recorded it with "END, DOWN ARROW", etc., etc.

I thought that macros were recorded, "literally" with each move of the cursor.
If resolving, please make the answer specific relevant to the code below, or any specific declarations, etc. required.

Thanks.



Sub Macro2()
'
' Macro2 Macro
' part 2
'
' Keyboard Shortcut: Ctrl+i
'
    Range("C2").Select
    Selection.End(xlDown).Select
    Range("C5:D5").Select
    Selection.Copy
    Range("C6").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("C5:D5").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveSheet.Paste
    Application.CutCopyMode = False
End Sub
0
Comment
Question by:James Powell
  • 7
  • 4
11 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39253288
I don't think I understand what you want. What I did was to but the values 1, 2, 3, 4 in cells C2, C3, C4 and C5. I then put a simple formula (=C5*5) in cell D5 so D5 became 20. When I ran your macro the value in C5 was copied to C6, the formula in D5 was copied and adjusted to D6 so it became =C6*5 and the formula in C5 was removed but the value of 20 remained. What do you want that's different than that?
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39253300
I corrected a typo in my question above but I still got it wrong.

...and the formula in D5 was removed...
0
 
LVL 1

Author Comment

by:James Powell
ID: 39253517
The very last ROW,  should always have formulas.

No matter how long the list grows,  everything will be "pure values", except for that last row.

The way Excel "records" the macro,  it only acts on the same cells each time.

That's why,  the "variable",  really is the "END (keyboard button)  DOWN ARROW (keyboard button)".
But it doesn't record that way.   It records as  "Cell  B5".   I need that destination to "expand, one cell lower, each time it is run".
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 46

Accepted Solution

by:
Martin Liss earned 300 total points
ID: 39253562
Okay now I think I understand. Try this macro which adds a row without you needing to select anything.


Sub AddRow()
Dim lngLastRow As Long
Dim lngRow As Long

lngLastRow = Range("C65536").End(xlUp).Row

Cells(lngLastRow + 1, 3).Value = Cells(lngLastRow, 3).Value
Cells(lngLastRow, 4).Copy
Cells(lngLastRow + 1, 4).PasteSpecial Paste:=xlPasteFormulas
Cells(lngLastRow, 4).Select
Cells(lngLastRow, 4).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:James Powell
ID: 39253807
Almost there.

That works, but not for both columns (of just the last row).

I have 2 columns:

Date                 Amount
6/15/2013         1,000
6/16/2013         1,250
TODAY()             =OTHER $Absolute reference$ CELL TOTAL

I need to copy both of those down.
Paste value on the "original" ones.
and make sure the last row always has those formulas.

Thanks for your help.

I still don't understand why, when I do a "record macro",  it's turning my
screen and keyboard moves into VB, but more specifically, VB that is not literal to what is on the screen.  To me, that's not recording crap.

Thanks for your help.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39253823
I don't understand this

=OTHER $Absolute reference$ CELL TOTAL

In your example should it be the sum of the rows above, in other words 2,250?
0
 
LVL 1

Author Comment

by:James Powell
ID: 39253921
No.  It's just a reference to a cell (on another sheet)
that needs to stay the same reference, each time it's copied down.

i.e.  =$Q$99

The dollar signs ensure that the Q doesn't change when copied.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39254397
In my code in line 9 has Paste:=xlPasteFormulas so whatever formula is in the last row of column D will be copied to the new last row of column D. A formula like =$Q$99  that has two absolute references will be copied without any adjustment, so what is wrong? In other words exactly what to you get as the formula in the new last line and what do you want?
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39255018
BTW I forgot to mention that line 7 should be

Cells(lngLastRow + 1, 3).Value = Date

if you want the last row to show the current date.
0
 
LVL 1

Author Comment

by:James Powell
ID: 39257449
It was only doing it for one column.

Based on your instruction and the bulk of your code, this is what I came up with that works for both column.  

Only one of the columns was being selected, then copied.
So I needed to add that loop a second time....this works.

Thanks for your help!


Sub AddRow()
Dim lngLastRow As Long
Dim lngRow As Long

lngLastRow = Range("C65536").End(xlUp).Row

Cells(lngLastRow, 3).Copy
Cells(lngLastRow + 1, 3).PasteSpecial Paste:=xlPasteFormulas
Cells(lngLastRow, 4).Copy
Cells(lngLastRow + 1, 4).PasteSpecial Paste:=xlPasteFormulas
Cells(lngLastRow, 4).Select
Cells(lngLastRow, 4).PasteSpecial Paste:=xlPasteValues
Cells(lngLastRow, 3).Copy
Cells(lngLastRow, 3).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39257540
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question