Solved

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

Posted on 2013-06-17
11
260 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 45

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 45

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
 
LVL 45

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

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 45

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 45

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 45

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will show you how to use shortcut menus in the Access run-time environment.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now