Solved

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

Posted on 2013-06-17
11
264 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

809 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