Forum Discussion
perkin_warbeck
May 17, 2021Brass Contributor
In VBA how can I prevent change of focus when a cell is selected?
In my VBA application, the user may click on certain chart elements (e.g., a series or a point in a series). This triggers an EventChart_Select event. In the event handler, I select a cell on the sa...
- May 17, 2021If i understand what you are doing, the reason Application.EnableEvents = False doesn't work is because that macro ends and hence EnableEvents gets turned back on and Excel will scroll. That said you can try adding ActiveWindow.ScrollRow = Range("cell where you want the window to be") before you turn EnableEvents back to True
mtarler
May 17, 2021Silver Contributor
Let me ask why you select that cell in the first place? Without the code i don't know what you are doing but in nearly all cases it is poor form, poor performance, and generally a bad idea to actually "select" cells in your macro. Just assign the .value of that cell to a variable as needed.
- perkin_warbeckMay 17, 2021Brass ContributorWhen the cell is selected, its contents do not change. However what the user does next on the chart WILL change the contents. You could argue that I should be using a global variable for this, but the cell is being watched for changes by Worksheet_Change. That's why I'm using a cell and not a variable. I don't think there's a way in VBA to watch for changes to a variable, but I'm new to VBA so maybe there is.