XL VBA - Paste Values and preface the pasting of Values by an equal sign

2Took

Board Regular
Joined
Jun 13, 2022
Messages
203
Office Version
  1. 365
Platform
  1. Windows
Below code pastes, but I need it to Paste Values and preface the pasting of Values by an equal sign:


VBA Code:
Sub Paste()
'Tools -> References -> Microsoft Forms 2.0 Object Library
'or you will get a "Compile error: user-defined type not defined"
  Dim DataObj As New MSForms.DataObject
  Dim S As String
  DataObj.GetFromClipboard
  S = DataObj.GetText
  'Debug.Print S 'print code in the Intermediate box in the Macro editor
  Target.Value = S
End Sub
 
You need two lines of code to copy and paste values using the code I posted:
Excel Formula:
S=cells(10,10).value   ' this line copies the values from the cell in row 10 column 10 into a variant variable called S
target.value =s      ' this line of code copies the value in variant S into the cell given by the target address.
You didn't state where you were copying from so I assumed row 10 column 10 Very unlikely to be correct) but it was just to show you.
which cells is your substitute function in?? and where do you want the results??
Hi @offthelip,

Currently I have a BeforeRightClick Worksheet event that runs Selection.Copy, so that's how I copy.
For this particular scenario, I am open to change the code that is triggered by BeforeRightClick because I know I'll be copying from A2.
For pasting I need for the target to be whatever cell I pick, can't be hardcoded.

I'm copying a formula from A2, e.g.:
=SUBSTITUTE(A4,"H",A1)
which has a value of below, when A1 contains "G", because it edits a formula in A4:
"G" & ". " & G10 & " - " & INDIRECT("G"&L1)
And I want to be able to paste the above line, but prefaced by an equal sign, so it becomes another formula, like this:
="G" & ". " & G10 & " - " & INDIRECT("G"&L1)
  • Let me know if this leaves out any of the questions you might have.
Can you give the entire code needed to go with your suggested code below?

VBA Code:
S = Cells(2,1).Value 'A2 to copy from
Target.Value = S
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
It is very easy to do this using a varaint array to hold the string between the copy and the paste.
Put this code in the worksheet module:
VBA Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Call copytovariant(Target)
End Sub

Then put this code into a standard module. Note the declaration of singlevariant must be done right at the top of the module:
VBA Code:
Public singlevariant As Variant
Sub copytovariant(trange As Range)
singlevariant = trange.Value
End Sub

Sub Pastevariant()
ActiveCell = singlevariant
End Sub
To do the paste you need to run the pastevariant code, I am not sure how you are triggering that.
 
Upvote 0
Solution
It is very easy to do this using a varaint array to hold the string between the copy and the paste.
Put this code in the worksheet module:
VBA Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Call copytovariant(Target)
End Sub

Then put this code into a standard module. Note the declaration of singlevariant must be done right at the top of the module:
VBA Code:
Public singlevariant As Variant
Sub copytovariant(trange As Range)
singlevariant = trange.Value
End Sub

Sub Pastevariant()
ActiveCell = singlevariant
End Sub
To do the paste you need to run the pastevariant code, I am not sure how you are triggering that.
Awesome solution!

I just added the "=" & into below part, and now I have what I was looking for, Thank You!

VBA Code:
Sub Pastevariant()
ActiveCell = "=" & singlevariant
End Sub

... and I'm triggering that by Worksheet_BeforeDoubleClick
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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