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.
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.