Hello,
I'm trying to write a macro to copy a row of cells to where the cursor is. The info is constant and the pasteing changes. I've used the "record" button with both relative on and off and the pasted info always goes back to where I ran the "record" button. Example: I put the cursor in cell A15 and want to copy cells C13 to C15 to cells A15 to A18. After I record this, I put the cursor in cell A17 and run the macro. The info gets pasted back to cells A15 to A18. I tried using range names for the info location and the result location (after deleting the range name that I used for the first pasting and creating a new range name for the new spot as part of the macro) but the pasting always goes back to where I first started when I used the "Record" button. I'm using excel 2008
Configuration: Windows XP..Microsoft Office 2007
I am confused.
Sub test() Range(Selection, Selection.Offset(3, 0)).Copy Cells(Selection.Row, "A").PasteSpecial Application.CutCopyMode = False End Sub don't forget to keep the cursor at the proper place. |
Hi, Venkat 1926.
|
I am still confused.
|
Hi, Venkat1926..
|
Suppose your data is like this
Private Sub Worksheet_Change(ByVal Target As Range) Dim r As Range, cfind As Range Application.EnableEvents = False If Target.Column <> 2 Then Application.EnableEvents = True Exit Sub End If Set r = Range(Target.Offset(-1, 0), Target.End(xlUp)) Set cfind = r.Cells.Find(what:=Target.Value, lookat:=xlWhole) If Not cfind Is Nothing Then Range(cfind, cfind.End(xlToRight)).Copy Target Else MsgBox "name not found fll up data" End If Application.EnableEvents = True End Sub NOTE: 1. try the code in the experimental data given by you first 2. in A5 type some date and inB5 type the one of the names already available from the previous data . see what happens When you type the same name as in the above rows<souligne> be careful of spelling 3.in A6 type some date and in B6 type some name which is NOT available in the rows abvoe and see what happens. 4. There should not be any gap in any of the cells or rows. </souligne> |
Hey, Venkat 1926,
|
I did not catch the point
|
Hello Venkat1926,
|
I have modified the EVENT CODE.
Private Sub Worksheet_Change(ByVal Target As Range) Dim r As Range, cfind As Range Dim msg, style, response Application.EnableEvents = False If Target.Column <> 2 Then Application.EnableEvents = True Exit Sub End If Set r = Range(Target.Offset(-1, 0), Target.End(xlUp)) Set cfind = r.Cells.Find(what:=Target.Value, lookat:=xlWhole) If Not cfind Is Nothing Then msg = "do you want to continue the macro" style = vbYesNo response = MsgBox(msg, style) If response = vbNo Then Application.EnableEvents = True Exit Sub End If Set cfind = Cells.FindPrevious(after:=Target) Range(cfind, cfind.End(xlToRight)).Copy Target Else MsgBox "name not found fll up data" End If Application.EnableEvents = True End Sub then you can use the event code in your data. BUT BE CAREFUL TO SAVE THE ORIGINAL DATA BASE SOMEWHERE SO THAT IF IT IS MESSED UP YOU CAN RETRIEVE THE DATA. CONFIRM IF THIS IS OK |