VBA worksheet change when copy paste multiple range

priisha

New Member
Joined
Apr 4, 2022
Messages
22
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,

I'm back here for help again so please bear with me.

I have a vba code that is triggered when there is a change in a range of cells. It works well if the changes are done cells by cells but i get an error in the case if i were to copy and paste for multiple cells in one go.

I hope you can help me adjust it so the code can still work if i were to copy and paste
here is the current VBA code

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B20:C35")) Is Nothing Then
    If Target.Value <> OldValue Then
        Target.Interior.Color = vbWhite
        Target.Borders.LineStyle = xlNone
    End If
End If
End Sub

thank you !
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You haven't given us all your code: where does Oldvalue get set?? Also what do you want to happen when you paste a range of cells??
 
Upvote 0
As OldValue has not been defined i have assumed that you just want to reset the formatting in the pasted range, give the below a try:
Note: If your paste range goes outside of the target area the code will still amend those cells

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rCell As Range
  
    If Not Intersect(Target, Range("B20:C35")) Is Nothing Then
        For Each rCell In Target.Cells
            With rCell
                .Interior.Color = xlNone
                .Borders.LineStyle = xlNone
            End With
        Next rCell
    End If
End Sub
 
Upvote 1
Solution
As OldValue has not been defined i have assumed that you just want to reset the formatting in the pasted range, give the below a try:
Note: If your paste range goes outside of the target area the code will still amend those cells

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rCell As Range
 
    If Not Intersect(Target, Range("B20:C35")) Is Nothing Then
        For Each rCell In Target.Cells
            With rCell
                .Interior.Color = xlNone
                .Borders.LineStyle = xlNone
            End With
        Next rCell
    End If
End Sub
This code worked wonder !!!
In the previous vba code where I had Oldvalue it was not defined in my vba and it just referred to the old value that was in the cell previous to the change. Another question if I had multiple range can I enter it as

Target, range("B20:C35", "C8:C13")?
 
Upvote 0
You're welcome, thanks for the feedback.

For your additional question, the syntax would be as below:
Excel Formula:
Target, Range("B20:C35,C8:C13")
 
Upvote 0
You're welcome, thanks for the feedback.

For your additional question, the syntax would be as below:
Excel Formula:
Target, Range("B20:C35,C8:C13")
ok, i thought the intersect function cant be used for multiple non continugious ranges and that the code should be
VBA Code:
Set uRanges = Union(Range("B20:C35"), Range("C8:C13"))       
  
If Not Intersect(Target, uRanges) Is Nothing Then
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,983
Members
449,092
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top