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 and paste problem

I have created a work sheet which using the workbook change subroutine
changes the background colour of cells and edits the content if certain
letters or number are added.

This works ok until I copy and paste into this range and then the background
changes from the selected colour to pink for all the pasted cells

I need to replicate in the range certain blocks of data and dont want to
have to type it in each time is there any way I can keep the copied cells
format as I paste them into the automated range.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
On Error Resume Next
'Sets the range of cells for the code to work if a cell other than the range
is altered
'the sub is exited.

Set Target = Intersect(Target, Range("B21:h21", "b700:h700"))
If Target Is Nothing Then
Exit Sub

'If the cells in the range equal this criteria then they are changed
accordingly
'Weekly Rest Day
ElseIf Target = "RD" Then

With Target
.Interior.ColorIndex = 17
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Annual Leave
ElseIf Target = "AL" Then

With Target
.Interior.ColorIndex = 4
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Bank Holiday Leave
ElseIf Target = "BH" Then

With Target
.Interior.ColorIndex = 24
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Duty Elsewhere
ElseIf Target = "DE" Then

With Target
.Interior.ColorIndex = 15
.Font.ColorIndex = 11
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Football
ElseIf Target = "FB" Then

With Target
.Interior.ColorIndex = 27
.Font.ColorIndex = 25
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Lieu Leave
ElseIf Target = "LL" Then

With Target
.Interior.ColorIndex = 33
.Font.ColorIndex = 1
.Font.Bold = True

End With


'If the cells in the range equal this criteria then they are changed
accordingly
'CADRE cover
ElseIf Target = "n" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 22
.Font.ColorIndex = 6
.Font.Bold = True

End With


'If the cells in the range equal this criteria then they are changed
accordingly
'CADRE cover
ElseIf Target = "c" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 22
.Font.ColorIndex = 2
.Font.Bold = True

End With


'If the cells in the range equal this criteria then they are changed
accordingly
'CADRE cover
ElseIf Target = "e" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 22
.Font.ColorIndex = 1
.Font.Bold = True

End With


'If the cells in the range equal this criteria then they are changed
accordingly
'PACE cover
ElseIf Target = "x" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 3
.Font.ColorIndex = 2
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Weekly Rest Day
ElseIf Target = "rd" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 17
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Annual Leave
ElseIf Target = "al" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 4
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Bank Holiday Leave
ElseIf Target = "bh" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 24
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Paternity Leave
ElseIf Target = "pl" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 7
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Duty Elsewhere
ElseIf Target = "de" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 15
.Font.ColorIndex = 11
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Football
ElseIf Target = "fb" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 27
.Font.ColorIndex = 25
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Lieu Leave
ElseIf Target = "ll" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 33
.Font.ColorIndex = 1
.Font.Bold = True

End With
ElseIf Target = "8" Then

Target = "8x5"
With Target
.Interior.ColorIndex = 2
.Font.ColorIndex = 1
.Font.Bold = False
End With
ElseIf Target = "10" Then

Target = "10x7"
With Target
.Interior.ColorIndex = 2
.Font.ColorIndex = 1
.Font.Bold = False
End With
ElseIf Target = "12" Then

Target = "12x9"
With Target
.Interior.ColorIndex = 2
.Font.ColorIndex = 1
.Font.Bold = False
End With
ElseIf Target = "1" Then

Target = "1x9"
With Target
.Interior.ColorIndex = 2
.Font.ColorIndex = 1
.Font.Bold = False
End With
'If the cells in the range equal this criteria then they are changed
accordingly
'Empty Cells
ElseIf Target = "" Then

With Target
.Interior.ColorIndex = 2
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If anything other than this criteria is entered then the cells are left
unformatted.

End If
End Sub

Here is the code I am currently using


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default copy and paste problem

Here is a better way of writting your code. I converted the first few ifelse
sections to demonstrate how to use the Case Select

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
On Error Resume Next
'Sets the range of cells for the code to work if a cell other than the range
'is altered
'the sub is exited.

Set Target = Intersect(Target, Range("B21:h21", "b700:h700"))

If Target Is Nothing Then
Exit Sub

'If the cells in the range equal this criteria
'then they are changed accordingly
'Weekly Rest Day
With Target

Select Case Target

Case Target = "RD"

.Interior.ColorIndex = 17
.Font.ColorIndex = 1
.Font.Bold = True

Case Target = "AL"

'If the cells in the range equal this
'criteria then they are changed accordingly
'Annual Leave

.Interior.ColorIndex = 4
.Font.ColorIndex = 1
.Font.Bold = True


'If the cells in the range equal this criteria
' then they are changed accordingly
'Bank Holiday Leave
Case Target = "BL"

.Interior.ColorIndex = 24
.Font.ColorIndex = 1
.Font.Bold = True

'If the cells in the range equal this _
' criteria then they are changed accordingly
'Duty Elsewhere
Case Target = "DE"
.Interior.ColorIndex = 15
.Font.ColorIndex = 11
.Font.Bold = True
End Select
End With
End If

End Sub


"Fred Kruger" wrote:

I have created a work sheet which using the workbook change subroutine
changes the background colour of cells and edits the content if certain
letters or number are added.

This works ok until I copy and paste into this range and then the background
changes from the selected colour to pink for all the pasted cells

I need to replicate in the range certain blocks of data and dont want to
have to type it in each time is there any way I can keep the copied cells
format as I paste them into the automated range.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
On Error Resume Next
'Sets the range of cells for the code to work if a cell other than the range
is altered
'the sub is exited.

Set Target = Intersect(Target, Range("B21:h21", "b700:h700"))
If Target Is Nothing Then
Exit Sub

'If the cells in the range equal this criteria then they are changed
accordingly
'Weekly Rest Day
ElseIf Target = "RD" Then

With Target
.Interior.ColorIndex = 17
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Annual Leave
ElseIf Target = "AL" Then

With Target
.Interior.ColorIndex = 4
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Bank Holiday Leave
ElseIf Target = "BH" Then

With Target
.Interior.ColorIndex = 24
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Duty Elsewhere
ElseIf Target = "DE" Then

With Target
.Interior.ColorIndex = 15
.Font.ColorIndex = 11
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Football
ElseIf Target = "FB" Then

With Target
.Interior.ColorIndex = 27
.Font.ColorIndex = 25
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Lieu Leave
ElseIf Target = "LL" Then

With Target
.Interior.ColorIndex = 33
.Font.ColorIndex = 1
.Font.Bold = True

End With


'If the cells in the range equal this criteria then they are changed
accordingly
'CADRE cover
ElseIf Target = "n" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 22
.Font.ColorIndex = 6
.Font.Bold = True

End With


'If the cells in the range equal this criteria then they are changed
accordingly
'CADRE cover
ElseIf Target = "c" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 22
.Font.ColorIndex = 2
.Font.Bold = True

End With


'If the cells in the range equal this criteria then they are changed
accordingly
'CADRE cover
ElseIf Target = "e" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 22
.Font.ColorIndex = 1
.Font.Bold = True

End With


'If the cells in the range equal this criteria then they are changed
accordingly
'PACE cover
ElseIf Target = "x" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 3
.Font.ColorIndex = 2
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Weekly Rest Day
ElseIf Target = "rd" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 17
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Annual Leave
ElseIf Target = "al" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 4
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Bank Holiday Leave
ElseIf Target = "bh" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 24
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Paternity Leave
ElseIf Target = "pl" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 7
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Duty Elsewhere
ElseIf Target = "de" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 15
.Font.ColorIndex = 11
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Football
ElseIf Target = "fb" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 27
.Font.ColorIndex = 25
.Font.Bold = True

End With

'If the cells in the range equal this criteria then they are changed
accordingly
'Lieu Leave
ElseIf Target = "ll" Then

Target = UCase(Target)
With Target
.Interior.ColorIndex = 33
.Font.ColorIndex = 1
.Font.Bold = True

End With
ElseIf Target = "8" Then

Target = "8x5"
With Target
.Interior.ColorIndex = 2
.Font.ColorIndex = 1
.Font.Bold = False
End With
ElseIf Target = "10" Then

Target = "10x7"
With Target
.Interior.ColorIndex = 2
.Font.ColorIndex = 1
.Font.Bold = False
End With
ElseIf Target = "12" Then

Target = "12x9"
With Target
.Interior.ColorIndex = 2
.Font.ColorIndex = 1
.Font.Bold = False
End With
ElseIf Target = "1" Then

Target = "1x9"
With Target
.Interior.ColorIndex = 2
.Font.ColorIndex = 1
.Font.Bold = False
End With
'If the cells in the range equal this criteria then they are changed
accordingly
'Empty Cells
ElseIf Target = "" Then

With Target
.Interior.ColorIndex = 2
.Font.ColorIndex = 1
.Font.Bold = True

End With

'If anything other than this criteria is entered then the cells are left
unformatted.

End If
End Sub

Here is the code I am currently using


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 and paste problem Germano Excel Discussion (Misc queries) 1 August 24th 07 12:45 PM
Copy/Paste problem Dan R. Excel Programming 3 March 8th 07 06:19 PM
Excel cut/Paste Problem: Year changes after data is copy and paste Asif Excel Discussion (Misc queries) 2 December 9th 05 05:16 PM
Copy/Paste Problem -JB- Excel Programming 1 October 1st 03 06:31 PM
Copy/Paste Problem Pete McCosh[_5_] Excel Programming 0 October 1st 03 05:14 PM


All times are GMT +1. The time now is 02:40 AM.

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"