Excel - Copy cell to another cell using Macro

Asked By Robert on 20-Oct-07 08:00 PM
I am trying to create a macro (attached to a transparent rectangle in cell A)
that will copy contents of cell B to Cell C and paste as a value.  Problem I
have now is the transparent rectangle is duplicated each time it is activated
in cell A  (macro does copy correctly to cell C) and I have to click on the
left side of cell A and slide over to the right side to activate transparent
rectangle (attached to macro) - any way to activate macro without clicking on
left side and sliding over the right.

See Example of Macro I have so far.  Any help will be greatly appreciated.

Sub Macro4()
'
' Macro4 Macro
' Macro recorded 10/15/2007 by Robert
'

'
Application.ScreenUpdating = False
ActiveCell.FormulaR1C1 = "R"
ActiveCell.Offset(0, 1).Select
Selection.Copy
ActiveCell.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.OnKey "{ENTER}"
Range("o7").Select
ActiveCell.Select

End Sub


Robert B.




HelpFrom replied on 20-Oct-07 08:55 PM
How about getting rid of the rectangle(s) completely and using the
worksheet's _BeforeDoubleClick event to do the work when you double-click a
cell in column A?  Would that work for  you?  Here's code to do that.  To put
the code into the proper place, right-click on the worksheet's name tab and
choose [View Code] from the popup list.  copy and paste the code below into
the code module area that appears.  From then on, when you double-click in
column A, whatever is in column be on the same row will be echoed into column
C on that row.

I'll 'warn' about this: sometimes when you click in the cell and try to
double click, the system may miss the double click, interpreting it as 2
single clicks.  That happens to me sometimes.  If the operation doesn't work
first try, try again.  When I set something up like this, I try to remember
to first click in the cell and THEN, after a very short pause, do the actual
double-click.

Private Sub Worksheet_BeforeDoubleClick(ByVal _
Target As Range, Cancel As Boolean)
If Target.Column = 1 Then
Target.Offset(0, 2) = Target.Offset(0, 1)
End If
End Sub
HelpFrom replied on 20-Oct-07 08:58 PM
Oops, left out a line of code that you'll probably want - the Cancel = True
statement I've added here will keep you from ending up in the A cell in
editing mode when you double-click it:

Private Sub Worksheet_BeforeDoubleClick(ByVal _
Target As Range, Cancel As Boolean)
If Target.Column = 1 Then
Target.Offset(0, 2) = Target.Offset(0, 1)
Cancel = True
End If
End Sub
Gord Dibben replied on 20-Oct-07 09:41 PM
Instead of dinking about with transparent triangles on cells, I would just use
doubleclick event code to run the macro.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
Cancel As Boolean)
If Target.Address = "$A$1" Then
'For a range use
'If Not Application.Intersect(Range("A1:A20"), Target) Is Nothing Then

On Error GoTo endit
Application.EnableEvents = False
Application.ScreenUpdating = False

With Target
.Value = "R"
.Offset(0, 1).Copy
.Offset(0, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks _
:=False, Transpose:=False
End With
Application.CutCopyMode = False
End If
Cancel = True
Range("O7").Select

endit:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

This is sheet event code.  Right-click on the sheet tab and "View Code"

Copy/paste into that sheet module.

When you doubleclick on A1 the events take place.


Gord Dibben  MS Excel MVP

On Sat, 20 Oct 2007 17:00:00 -0700, Robert B.
Gord Dibben replied on 20-Oct-07 09:57 PM
I like that bit Jerry.

Much shorter than the version I came up with.

Gord
HelpFrom replied on 20-Oct-07 10:57 PM
Finally!!  It happened to someone else instead of me <g>.  I usually end up
writing 14 pages of code and someone comes along with a single, under 32
character worksheet function, to do the same darned thing.
Robert replied on 21-Oct-07 04:46 AM
To: All

Thanks for the suggestions in code.  I will try them today.  I am just
learning how to use VBA.  What I am trying to create is a entry / exit log.
Will let you know if it works.

Thanks again

Robert B.
Gord Dibben replied on 21-Oct-07 11:08 AM
See also Jerry's post with the shorter code although it does not enter an "R" in
column A

You can fix that by combining the two.


Gord Dibben  MS Excel MVP
Robert replied on 21-Oct-07 10:33 PM
Mr. Dibben,

The code below worked perfectly for 1 column.  I have four columns in my
worksheet that need same.  Is there a way to add more ranges to the code or
can I copy code and change ranges?  I tried to copy and change ranges but
computer gave me an error about a name.  I am not sure what that meant.

Any help Greatly appreciated

Robert B.
Robert replied on 21-Oct-07 10:44 PM
Mr. Dibben,

I figured it out - how to make it work in multiple columns.

Thanks again for your help.

Robert B.
Gord Dibben replied on 22-Oct-07 11:58 AM
Thanks for the update.

Gord
Robert replied on 27-Oct-07 10:32 AM
Mr. Dibben,

Everything so far is working out great on the in/out log I am creating with
your help.  I have one more question.  I have made four worksheets for entry
/ exit.  Is there are way to so out times from all four worksheets between
the hours of 18:00 and 06:00?  Doing a data sort puts the hours from 00:00 to
24:00.  I need it to sort from 18-0600

Robert
Robert replied on 07-Nov-07 01:11 PM
Mr. Dibben,

One more quick question - How can what is being copied (t - column) be
pasted as a value in the U column?

Thanks for any help you can provide

Robert B.
Robert replied on 07-Nov-07 01:43 PM
Mr. Dibben,

Forget the last question - Sorry.  I wrote it the wrong way.  It should be:

Question:  What I am trying to do is I think very similar to the code you
originally wrote.  I want to after making a selection from the drop down menu
in Column T copy Column U to Column V and Paste as a Value.

Thanks again for any help you can provide.  This should be my last question.

Robert B.