How to Restrict Ctrl + End in an Excel Sheet - excel

I have some data inserted at Rows 500 - 503. I placed the data intentionally at those rows because these rows will be used in Macro's and not for User's view. The data concerned to the user ends at Row 100.
I have hidden the Rows 500 - 503, yet after hitting Ctrl + End, the Cursor moves to Row 499.
My requirement is after hitting Ctrl + End, the Cursor should be at the last cell of Row 100.
So, how do i do this ?
EDIT : Thanks for all the answers. Many advised me to have another sheet for non user related data, but i can't have it. Also i am not supposed to have the vba scripts.(Should have given you these conditions before... my bad!)

This problem will be removed with better isolation.
That is, worksheets which are designed for use by a user should be restricted to containing only the elements necessary for the user interface. In this case, only the rows which the user is supposed to see.
Any data which needs to be persisted in worksheets which is not designed for direct user consumption should be on their own worksheets, which can even be hidden from the user completely.

The specific answer to your question of course is: trap the usage of the Ctrl-END key.
You can do this easily by specifying the following event triggers to your sheet:
Private Sub Worksheet_Activate()
Application.OnKey "^{END}", "SuppressMe"
End Sub
Private Sub Worksheet_Deactivate()
Application.OnKey "^{END}", ""
End Sub
and another module containing
Sub SuppressMe()
' you can be very creative here about line 100 or not 100
MsgBox "Co-Cooooo :-P"
End Sub
Another cute way meeting the requirements you described and eliminating the need of watching and suppressing keys would be to limit navigation to row 100. Now a Ctrl-END would place the user into the rightmost column of row 100, and Ctrl-DOWN would be covered as well. In fact the user just can't go past row 100
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Row > 100 Then
Me.Cells(100, Target.Column).Select
End If
End Sub
but .... wait !!! is there really no way? ... of course the user still may SCROLL past row 100 and detect your little row-500 easter egg. So I concur to 100% with earlier suggestions about a different strategy, because
a user finding out that exactly line 500 - 503 is hidden can become tremendeously curious about the why's
Ctrl-END is not the only way the user can navigate past row 100 - you would need to suppress END-DOWN (a 2 - key combination ... you need to buffer the END and ask the next key: are you a DOWN ... have fun!)
and you are wasting disk space to store a sheet with so many blank rows just for that line 500
You may have reasons to implement a key supressor, but always remember there are many different ways for the user to bypass it and this needs to be considered very carefully.

That's Excel's built-in behaviour, and I don't think you can change it. Suggestions:
Put your hidden data on another sheet of the same workbook. This is really the cleanest way to work.
Alternatively, navigate using CtrlDown arrow or EndDown arrow instead of CtrlEnd.

As others said it's certainly better to separate your non-user data on a different sheet.
That said, one thing you could do is protect your sheet and disallow access to locked cells, leaving only the cells in which the user has to input something unlocked.
Doing that prevents CtrlEnd from doing anything at all: it can't jump to the last cell as it is locked, so it doesn't do anything. (Tested on Excel 2007)
But again, you'll probably be better off by separating user and non-user data in different sheets.

Related

Is there a way using sheet protection to allow a user to enter values but protect formats?

I have a worksheet containing a column of numbers. The column is formatted with a background color, number formats, etc. The column is unlocked. I protect the sheet manually by right-clicking on the tab and selecting Protect. In the Protect Sheet dialog, 'format cells' is unchecked. I interpret to mean that the user should not be able to format cells. Yet when the user pastes into the column, formats are pasted along with values.
If I protect the sheet in VBA using
sh.Protect UserInterfaceOnly:=True
I get the same result: formats are pasted. I do not specify AllowFormattingCells:=False because the default is False.
I have seen posts suggesting that formats can be restored by copying and pasting them from a shadow area. I have used this solution before I started protecting worksheets and found it overly complex. I had hoped this was something protection would handle. If there is a way to handle it, I'd like to do it in VBA.
This question is a little old, but I had the same one, and so have many people in the past. With a bit of browsing I came up with a solution which seems quite clean and appears to work. Am I missing something?
Private Sub Worksheet_Change(ByVal rngTarget As Range)
Dim vPaste As Variant
With Application.CommandBars("Standard").Controls("&Undo")
If Not .Enabled Then Exit Sub
If .ListCount < 1 Then Exit Sub
If .List(1) <> "Paste" Then Exit Sub
End With
vPaste = rngTarget.Value2
On Error Resume Next
Application.EnableEvents = False
Application.Undo
rngTarget.Value2 = vPaste
Application.EnableEvents = True
On Error GoTo 0
End Sub
This could go in Workbook_SheetChange, but not if you already have code in Worksheet_Change, because the worksheet event handler gets called before the workbook event handler. But this code can go in a module to keep things tidy.
There is no built-in protection option you can use to achieve your desired result.
The only thing that works in this case is the clunky workaround that you mention, i.e. use a Worksheet_Change event that ensures the correct format after a cell has been modified.
Since there are many different ways to paste content, i.e. via various menus, ribbon commands, keyboard shortcuts, etc., any VBA solution that tries to intercept pasting will become very complex, much more complex than the change event that restores the format to its original state.
Another option might be user education and training (so they know to paste values only), although user behaviour may be the toughest element to change in the whole scenario.

Excel: How can I hide the Selection Box flying around?

I have the following code to simulate some cells behave like buttons
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Application.ScreenUpdating = False
If Target.Cells.Count = 1 Then
'~~~~~~ pseudocode ~~~~~~
If {select cell is one of the chose ones}
{do some stuff}
End If
'~~~~~~ pseudocode ~~~~~~
End If
Range("A1").Select
'Application.ScreenUpdating = True
End Sub
It doesn't matter whereas I use the ScreenUpdating code, the "Selection box" is flying around from A1 to the selected cell and back and it make the sheet much slower.
Can this flying (animation) selection box be stoped?
So far I have found this, not possible to hide, but noting about the flying efect:
Hide the cell selection box in Excel
Edit:
I need (I think so) edit capabilities on the sheet, therefore the option of not changing selection cell is not applicable. Due to:
most of the sheet is informative, and should be available for copy (not edited)
some cells are input forms (free text thing), selection as usual
some cells should behave like buttons (plus/minus for a numeric value, sclaes, simple stuff, but thousand of then, so much easier do/maintain by code), and user must not edit them
grouping should be available, (so that's complicate protecting the sheet)
I am not closed to the option : Range("A1").Select after each (most) of user interaction, but no other method comes into mind to me now.
An example:
I know some would say: "you should make this out from excel", and I agree with you, but this is a mandatory thing, I do not have the power to raise this question
As you can see, I got the "flying selection" that I try to get rid off
cell A1 is already hodden, that will do most of the trick
final version sure will go with hidden gridlines and headlines
rows groups exist, and are important, so no protection possible
all the functionality, I can do easy with vba, just problem with the animation
Maybe this is not the answer that you have been waiting for, but as Mathieu mentioned in his comment, please try to avoid using Selection.
It does make things slower and often causes errors (in example try selecting cell from hidden sheet). Instead just do something with the range that you define with your if statements directly. Every property of Cell or Range can be accessed directly.
Hope it helps.
Not sure how you can achieve you "flying select box" problem, but at least you could add this code, so opening/closing groups are available on protected sheets:
'Password Protect Current Sheet
ActiveSheet.Protect Password:="Add_here_your_password", UserInterfaceOnly:=True
'Enable Group Collapse/Expand Capabilities
ActiveSheet.EnableOutlining = True
How about trying to remove the "back to A1" as much as possible?
Maybe do it only on absolutely necessary, or move back to the changed value (the 33 in your example), or to the question title (in your multi-option example)

How to delete a row instead of hiding it?

Trying to delete, rather than hide from one worksheet (where the filter button is to another where the catalogue list is. i.e. trying to create something like (best way to describe this) a shopping basket based upon the users selection and reducing the (long) list on the other worksheet after selection.
I am beginning to pull my hair out on this one and after having read and watched many, many articles on deleting rows after filtering on empty cells and today nothing has really helped as there are more issues than solutions when following codes from other, thus far.
Basically, I have a Hugh catalogue on a separate worksheet and if the user says yes, this should be shown and if not if blank (but with a value) it should be deleted. See so very basic script I have that works perfectly, but I have to delete and not hide. It appears EntireRow.delete is something beyond me, as it introduces many many issues, where hide simply worked so smoothly.
Trying many other scripts, they all really fail in simplifying the answer and 99% are actually for a single worksheet and range rather than a specific worksheet and specific columns i.e. E:E (script script below shows more) I am using a table too, so this is a little different too.
For a = 2 To 150
If Worksheets("Requirements").Cells(a, 5).Value = "High" Then
Worksheets("Requirements").Rows(a).Hidden = True
End If
Next
Anyone with a brilliant one or two liner to delete rather than hide, or delete all hidden if necessary
Many thanks in advance
Consider:
Sub sjdhfs()
For a = 150 To 2 Step -1
If Worksheets("Requirements").Cells(a, 5).Value = "High" Then
Worksheets("Requirements").Cells(a, 5).EntireRow.Delete
End If
Next a
End Sub
Note we run the loop from the bottom to the top.

Change to next available row when there is a conflict in shared workbook

I have a workbook which is shared among multiple users.
There is a Userform which asks for data and stores it in the workbook. It generates a row number where the user will enter data.
When two users open the Userform at the same time and one submits the data before other, Excel shows the conflict to other user.
Instead it should go to the next row and save the data. How can I do this?
You don't. Excel has not been designed for simultaneous editing by multiple users. Although shared workbooks are possible, they have many limitations, the most annoying of which is that they will become corrupt eventually, show erratic behaviour and are impossible to troubleshoot.
If you need simultaneous edit access, consider a database like Access or SQL. You can still use Excel as a front end.
Normally by doing a check if your row is different than blank like this:
indx = 1 'this is the generated index of your row
If Cells(1, indx) <> "" Then '1 is your column, in this case A
indx = indx + 1
End If
But if you have multiple users accessing it the cell will not be updated in the second user worksheet, they are not synchronised and data entered by the 1st user remains invisible for the second up to the point when he reopens it.

How to erase one region in Excel when closing the spreadsheet

I have a spreadsheet, with whatever in it. Now I want use one specific region (let's say C1:D10) to hold temporary data (in case I cannot remember clearly or mess them up).
However, I don't want keep the data for ever, and it is totally useless when this spreadsheet is used next time. Is there any way to erase this part when the spreadsheet is closed, no matter whether the user choose "save the change" or not, so that (C1:D10) is blank when I open the spreadsheet next time.
Instead of clearing the cells upon exit, use the Workbook_Open to clear the cells. This way you don't need to bother whether the user save the workbook on exit or not. This code will clear the cell the moment the workbook is opened.
Change Sheet1 below to the relevant sheet.
Private Sub Workbook_Open()
Sheets("Sheet1").Range("C1:D10").ClearContents
End Sub

Resources