Paste from One Worksheet to Another *Sometimes* Activates Target Worksheet

Domer82

New Member
Joined
Sep 24, 2018
Messages
1
I have a very strange issue that I'm hoping someone can help me with. I've written a macro to automate a weekly task my wife has to perform at work. It involves sifting through a long list of raw data records and copying them to other worksheets in the same workbook according to specified filter criteria. I wrote and tested the macro on my laptop (running Office 365 Pro Plus). I also tested it on a PC running Office 2016 as well as another PC running Excel 2010 and it worked fine on those platforms, too. But it crashes on my wife's PC at work.

Here is the relevant section of code. The range to be copied is already defined by the StartRow and StopRow variables. There are some hidden columns that I do not want copied (hence the "xlCelltypeVisible" flag).

For i = 0 To NumFilters - 1
If CarrierName = Filter(i, 0) And (AcctNumber = Filter(i, 1) Or Filter(i, 1) = "") Then
'================================================================
' We have a filter match so copy the entire chunk of data to the
' corresponding worksheet and remove the yellow highlighting.
'================================================================

RangeString = "A" & StartRow & ":" & LastSrcColRef & StopRow
Range(RangeString).SpecialCells(xlCellTypeVisible).Copy
RangeString = "A" & TrialBalanceWB.Worksheets(Filter(i, 2)).Cells(Rows.Count, 1).End(xlUp).Row + 1
TrialBalanceWB.Worksheets(Filter(i, 2)).Range(RangeString).PasteSpecial Paste:=xlValues
RcdsCopied = RcdsCopied + (StopRow - StartRow + 1)
RangeString = CarrierColRef & StartRow & ":" & CarrierColRef & StopRow
Range(RangeString).Interior.ColorIndex = xlNone
Exit For
End If
Next i

On all the platforms where the macro completes normally, the source worksheet (the one with the raw data) remains as the active worksheet throughout the above process. But on my wife's PC at work, the "PasteSpecial" line of code changes the active worksheet to the target worksheet, thereby causing subsequent code to get lost, starting with the line that removes the background color (the line just before the "Exit For").

After looking at the bread crumbs left behind when it crashed on her machine, I was able to ascertain what was probably happening, so I adding this line immediately after the "PasteSpecial" statement and that fixed the problem:

TrialBalanceSheet.Activate

But I am clueless as to why this works on 3 different platforms but fails on her office PC.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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