Link to home
Start Free TrialLog in
Avatar of Megin
Megin

asked on

Entering a string into a cell without deleting what is already there

I need a piece of code that will go into the cell I have highlighted, enter a string of text, insert a carriage return, and then leave the cell.

Every time I try to do this, it deletes the text that is already in the cell. What I need for the code to do is just insert the string above the text that is already there.

Is this possible?
Avatar of tel2
tel2
Flag of New Zealand image

Hi Megin,

> What I need for the code to do is just insert the string above the text that is already there.

Q1. What do you mean by "above" in this context?
Q2. Is the "string of text" to be inserted always the same text, or what?
Q3. And what version of Excel are you using?
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is the code for the selected range will do the requested:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A2:A25")) Is Nothing Then
  Dim copy As String
  
  copy = Target.value
  Target.value = "SAMPLE" & Chr(10) & copy
   
End If
End Sub

Open in new window


Code will run on the double click in the defined range
Avatar of Megin
Megin

ASKER

That worked perfectly!

THANK YOU!!!!

(And thank you to the rest of you who answered!)
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014