I have a macro that creates an additional range from one given, depending on some criteria. This subband is then copied to some place where it forms the data read by the chart.
The macro is called every time I click on the counter, as this changes the original range, and therefore it is necessary to calculate a new subrange.
The procedure works fine, although it is probably very primitive (first coding experience with VBA-Excel). Only when I click on the counter to update the data and start the procedure, the "focus" of the sheet moves from the location of the button to the beginning of the range on which the macro works.
I overcome this using Application.Goto at the end of the procedure to return the upper left corner of the window to where the Chart and button are.
However, this means that every time I run a macro, the contents of the window are moved to a range, and then back to where I want it. This is not very elegant, besides the potential cause of motion sickness; -)
I was wondering if I can avoid this and how.
Thanks for any input, the code follows.
Sub test_copy2()
Dim InRange As Range
Dim c As Range, o As Range, t As Range
Dim r As Integer
Set InRange = Cells.Range("M434:ATF434")
Set o = Cells.Range("L434")
For Each c In InRange
If c.Offset(-219, 0).Value = 8448 Then
Set t = Application.Union(o, c)
Set o = t
End If
Next c
o.Copy
InRange(1, 1).Offset(2, -1).PasteSpecial (xlPasteValues)
Application.Goto Cells.Range("AF456"), Scroll:=True
End Sub
source
share