Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copy cells to the left of an event in a cell

I need some help writing code. When I type "d" in any cell on the
spreadsheet, I want excel to initiate a macro that will automatically copy
the values of the two cells to the left of it.

For example: if I type "d" and then enter in cell C3, I want it to
automatically copy the range A1:A2. OR if I type "d" and then enter in cell
F7, I want it to automaticall copy the range G7:E7. Any combo like that
should work.

The last step I can do, which is pasting special values in the next
available cell of a different sheet.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copy cells to the left of an event in a cell


Jacob M;465624 Wrote:
I need some help writing code. When I type "d" in any cell on the
spreadsheet, I want excel to initiate a macro that will automatically
copy
the values of the two cells to the left of it.

For example: if I type "d" and then enter in cell C3, I want it to
automatically copy the range A1:A2. OR if I type "d" and then enter in
cell
F7, I want it to automaticall copy the range G7:E7. Any combo like
that
should work.

The last step I can do, which is pasting special values in the next
available cell of a different sheet.


Paste this into the relevant sheet's code
module:Private Sub Worksheet_Change(ByVal Target As
Range)
If Target.Cells.Count = 1 And Target.Value = "d" And Target.Column 2
Then
Target.Offset(, -2).Resize(, 2).Copy
End If
End Sub
but check that If I type "d" and then enter in cell C3, I want it to automatically copy
the range A1:A2 should read:

If I type "d" and then enter in cell C3, I want it to automatically
copy the range A1:*B1*


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=128785

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Copy cells to the left of an event in a cell

So I typed:

Sub Draft()
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count=1 And Target.Value="d" And Target.Column2
Then
Target.Offset(, -2).Resize(, 2).Copy
End If
End Sub

But the compiler doesn't like: "If Target.Cells.Count=1 And Target.Value="d"
And Target.Column2". Any ideas?

Jacob M

"p45cal" wrote:


Jacob M;465624 Wrote:
I need some help writing code. When I type "d" in any cell on the
spreadsheet, I want excel to initiate a macro that will automatically
copy
the values of the two cells to the left of it.

For example: if I type "d" and then enter in cell C3, I want it to
automatically copy the range A1:A2. OR if I type "d" and then enter in
cell
F7, I want it to automaticall copy the range G7:E7. Any combo like
that
should work.

The last step I can do, which is pasting special values in the next
available cell of a different sheet.


Paste this into the relevant sheet's code
module:Private Sub Worksheet_Change(ByVal Target As
Range)
If Target.Cells.Count = 1 And Target.Value = "d" And Target.Column 2
Then
Target.Offset(, -2).Resize(, 2).Copy
End If
End Sub
but check that If I type "d" and then enter in cell C3, I want it to automatically copy
the range A1:A2 should read:

If I type "d" and then enter in cell C3, I want it to automatically
copy the range A1:*B1*


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=128785


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copy cells to the left of an event in a cell


You're probably pasting the code into the wrong module and you don't
need to start it with *Sub Draft()*
Select the sheet you want it to work in, right-click its tab and choose
*View code..*, the vbe will open with the flashing cursor where you need
to paste the code.
Go back to the sheet and test.


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=128785

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Copy cells to the left of an event in a cell

That worked. I thought I could combine the two steps, but I'm having trouble.
So now when I type "d" and enter, it copies the cells to the left. I then hit
ctrl+d and it pastes the values in the next available cell in a different
sheet. Is there anyway to combine the two?

Sheet1 Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count = 1 And Target.Value = "d" And Target.Column 2 Then
Target.Offset(, -2).Resize(, 2).Copy
End If
End Sub

Module1 Code:
Sub DraftPlayer()
'
' Keyboard Shortcut: Ctrl+d
'
Sheets("Team Roster").Select
Range("C2").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.PasteSpecial (xlPasteValues)
ActiveWorkbook.Sheets("Team Roster").Activate
Application.CutCopyMode = False
End Sub

Thanks,
Jacob M

"p45cal" wrote:


You're probably pasting the code into the wrong module and you don't
need to start it with *Sub Draft()*
Select the sheet you want it to work in, right-click its tab and choose
*View code..*, the vbe will open with the flashing cursor where you need
to paste the code.
Go back to the sheet and test.


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=128785




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copy cells to the left of an event in a cell


Jacob M;466008 Wrote:
That worked. I thought I could combine the two steps, but I'm having
trouble.
So now when I type "d" and enter, it copies the cells to the left. I
then hit
ctrl+d and it pastes the values in the next available cell in a
different
sheet. Is there anyway to combine the two?Well it worked here fine. Are these sheets all in the same workbook?


By the way, a little improvement:Private Sub
Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count < 1 Then Exit Sub
If Target.Value = "d" And Target.Column 2 Then
Target.Offset(, -2).Resize(, 2).Copy
End If
End Sub
eliminates a bug when you changed more than one cell at
once on Sheet1.

If you want you could avoid having to do ctrl+d by putting everything
in the event code:Private Sub Worksheet_Change(ByVal
Target As Range)
If Target.Cells.Count < 1 Then Exit Sub
If Target.Value = "d" And Target.Column 2 Then
Set Source = Target.Offset(, -2).Resize(, 2)
Set Dest = Sheets("Team Roster").Range("C2")
Do
If Not IsEmpty(Dest) Then
Set Dest = Dest.Offset(1)
End If
Loop Until IsEmpty(Dest)
Set Dest = Dest.Resize(, 2)
Dest.Value = Source.Value
End If
End Subwhich also avoids lots of sheet swapping and
selecting.


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=128785

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy cell to the left mike_vr Excel Discussion (Misc queries) 1 March 11th 09 05:48 PM
macro to look down cells and if negative in the right of the cell change to left of the cell [email protected][_2_] Excel Programming 9 August 7th 07 12:06 PM
Excel VB-Copy formula down until adjacent cell (left) is blank? Tony P.[_2_] Excel Programming 3 May 18th 05 09:08 PM
Excel VB-Copy formula down until adjacent cell (left) is blank? Tony P. Excel Discussion (Misc queries) 1 May 18th 05 06:11 PM
Copy formula relative to left cell? JayL Excel Programming 3 September 20th 04 02:12 AM


All times are GMT +1. The time now is 04:01 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"