Issues with "Private Sub Worksheet_SelectionChange(ByVal Target As Range)" - Code restarting from pasting

Stildawn

Board Regular
Joined
Aug 26, 2012
Messages
200
Hi All

Have a complicated sheet that does things on the fly as the user changes fields and enters data etc etc.

To do this I use: Private Sub Worksheet_SelectionChange(ByVal Target As Range) on Sheets(1)

Which as you know runs the code whenever the selection changes on the sheet, which is great.

My issue is that I have this code within the SelectionChange sub:

Code:
With Thisworkbook.Sheets(1)
Lastrow = ThisWorkbook.Sheets(2).Range("A" & ThisWorkbook.Sheets(2).Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets(2).Range("C" & Lastrow & ":G" & Lastrow).Copy
.Range("D" & i).PasteSpecial xlPasteValues
End With

There is massively more code within the With block but that is the relevant part.

What happens is when the ".Range("D" & i).PasteSpecial xlPasteValues" line is run, the code takes that as a selectionchange, and then the whole selectionchange code runs again from the start.

Which is screwing up the rest of the code below ".Range("D" & i).PasteSpecial xlPasteValues"

Any ideas on how to get around this?

Regards
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Last edited:
Upvote 0
ok try this

Code:
With Thisworkbook.Sheets(1)
Lastrow = ThisWorkbook.Sheets(2).Range("A" & ThisWorkbook.Sheets(2).Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets(2).Range("C" & Lastrow & ":G" & Lastrow).Copy Destination:=.Range("D" & i)
.Range("D" & i, .Range("D" & i).Offset(ThisWorkbook.Sheets(2).Range("C" & Lastrow & ":G" & Lastrow).Rows.Count - 1, ThisWorkbook.Sheets(2).Range("C" & Lastrow & ":G" & Lastrow).Columns.Count - 1)).Clearformats
End With

i was trying to use destination to get your values into the cells you want and then clear the formatting from those cells by getting the shape of your copy area and offsetting it to the destination. i hope that works with the With .Range code
 
Upvote 0
wait sorry i was assuming you were pasting values to avoid formats like highlights or font. i didn't consider it was to get values from formulas. destination will paste the formulas
sorry i couldnt help
 
Upvote 0
bump for help
i hope someone can solve your problem since i wasnt any use
 
Upvote 0
You can disable events while pasting like this:

Code:
With Thisworkbook.Sheets(1)
Lastrow = ThisWorkbook.Sheets(2).Range("A" & ThisWorkbook.Sheets(2).Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets(2).Range("C" & Lastrow & ":G" & Lastrow).Copy
Application.enableevents = false
.Range("D" & i).PasteSpecial xlPasteValues
Application.enableevents = true
End With
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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