Exact Copy VBA

irie267

New Member
Joined
Jul 1, 2012
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
Hello All--

I am trying to create a macro that will copy a cell (formula range or one cell) exactly how it is currently written and then be able to paste it elsewhere in the workbook, so that when it's pasted it will result in the same result as the original copied formula. Essentially, I want whatever happens when I press "CTRL C" using the native excel shortcut minus the changing of references (I am aware of locking the reference before copying, but I dont want to have to do that every time). Is this possible in VBA?
 
Put this in the sheet module :
VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Range)
Static lastRng As Range
On Error Resume Next
Target.Interior.ColorIndex = 8
lastRng.Interior.ColorIndex = xlColorIndexNone
Set lastRng = Target
End Sub
Then run this:
VBA Code:
Sub InputBoxExample()
Dim rngS As Range, rngD As Range, cel As Range
Set rngS = Selection
On Error Resume Next
Set rngD = Application.InputBox(Prompt:="enter a range", Type:=8)
On Error GoTo 0
If rngD Is Nothing Then
    MsgBox "A range was not selected"
    End
End If
With rngD.Resize(rngS.Rows.Count, rngS.Columns.Count)
    .Select
    .Formula = rngS.Formula
End With
End Sub
 
Upvote 0
Solution

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Put this in the sheet module :
VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Range)
Static lastRng As Range
On Error Resume Next
Target.Interior.ColorIndex = 8
lastRng.Interior.ColorIndex = xlColorIndexNone
Set lastRng = Target
End Sub
Then run this:
VBA Code:
Sub InputBoxExample()
Dim rngS As Range, rngD As Range, cel As Range
Set rngS = Selection
On Error Resume Next
Set rngD = Application.InputBox(Prompt:="enter a range", Type:=8)
On Error GoTo 0
If rngD Is Nothing Then
    MsgBox "A range was not selected"
    End
End If
With rngD.Resize(rngS.Rows.Count, rngS.Columns.Count)
    .Select
    .Formula = rngS.Formula
End With
End Sub
This is great, thank you for all the work.
 
Upvote 0
I have another issue now. I recently upgraded to Excel 365, and this macro does not work with dynamic ranges; is there a way to tweak the code to not allow dynamic ranges to interfere with the macro? When the macro is used on a dynamic range, it seems to be adding the "@" in the front of the newly pasted formula for some reason.
 
Upvote 0

Forum statistics

Threads
1,215,836
Messages
6,127,183
Members
449,368
Latest member
JayHo

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