The user can enter data in that cell:
By choosing from a predefined list of options (scrolling a drop down list)
By typing in directly the alphanumeric value (all numbers, all letters, or a combination of the two)
By doing copy (from other documents) and paste the identifier in that cell
I do not want to give the user the right to perform option 3? Is there a VBA code to accomplish that?
Excel has always had a problem with validations. They work fine when you need someone to choose from a list or enter specific data, but the moment a user copies data from someone else and Pastes in a Validation Cell, it ignores validations and allows it
Here is an example:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target = Range("A1") Then
If Not (Target >= 1 And Target <= 10) Then
Application.Undo
MsgBox "Please enter value between 1 to 10", vbOKOnly + vbCritical
End If
End If
Application.EnableEvents = True
End Sub
Related
I have the following task: I want to write a macro that can be activated when you click on a specific cell and hides a certain number of rows under it.
I suppose that I need to solve the following problems:
I need to call macros when I select the cell.
We need to detect the exact coordinates of selected cell.
The final point: we need to hide N rows under the selected cell.
The first point works good because I found the code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D4")) Is Nothing Then
MsgBox 'you clicked d4!'
End If
End If
'End Sub
But when I try to replace the MsgBox 'you clicked d4!'for:
callingCellRow = Application.Caller.Row
It does not work Error:
the variable is undefined
Can you give some examples or provide the code which combines these thirds points?
I want to put up a validation that should not allow percentage sign in excel sheet. I tried the data validation option to select only decimals, but still when I put the percent sign at the end of the number, excel considers this. Please help
try this in the Data-Validation as Formula:
=LEFT(CELL("format",A1),1)<>"P"
Best regards chris
Maybe this VBA-solution can help:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
For Each cell In Target
If InStr(cell.Text, "%") > 0 Then
cell.Value = ""
MsgBox "It's not allowed to insert a %-sign", vbInformation
End If
Next
End Sub
This procedure will validate evry input and if a %-sign is inside, it will throw an message box with an error message.
If you are not familiar with VBA: You have to copy this procedute into the code-area of the sheet. Therefore open the Visual Basic Editor. On the left side you will find a list with your sheets. Double click on the sheet where you want to validate for a %-sign and copy the procedure into the code-area.
I'm trying to add a formula to a specific cell that says "Enter user data then add 20" Ex: If a user enters 10 into a cell when they press enter the cell returns 30.
I've tried a formula that says, =this cell (plus this cell plus 20) but can't have a formula and user input in the same cell.
Create a named range on the cell that you are wanting to add 10 to, I called that range rngCellToAdd10To ...
Then add the below code (using the VBA editor) to the worksheet object that the cell you want to monitor changes for ...
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Intersect(Target, Range("rngCellToAdd10To")) Then
Application.EnableEvents = False
Range("rngCellToAdd10To").Value = Range("rngCellToAdd10To").Value + 10
Application.EnableEvents = True
End If
End Sub
... then add a number to the cell and watch it go.
If you're not sure how to add code into the VBA editor, I suggest Googling it.
How can I make column 6 (F) and 13 (M) to accept inputs only when I double click, which means it should not accept any numbers, letters or symbols and even the "delete" button but double click?
The following is the code I have worked out:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Checking whether target cell is in third column
Select Case Target.Column
Case 6, 13
If Not Intersect(Target, Range("F2:F13, M2:M13")) Is Nothing Then Cancel = True
'Prevent cell going into Edit Mode
Cancel = True
'Changing font type of the cell
Target.Font.Name = "Marlett"
'Checking if target cell value is blank then inserting tick
If Target = "" Then
Target = "a"
Else
MsgBox "You cannot modify the cell."
End If
End Select
End Sub
When the user types something into the Worksheet, the events that get fired is Change and SelectionChange. BeforeDoubleClick is only triggered when they double-click, so of course it doesn't run if they only type.
The easiest way to do what you want is to go to the Review tab > Protect Sheet, then Allow Users to Edit Ranges. The VBA is only to mark the checkbox in F2:13 and H2:13.
I have a large spreadsheet and have validation on several columns that have drop down lists.
I have the below VBA code in place that restricts user from hitting the delete button and blanking out cell in column with drop down. This works nicely but it does not prevent user from copying a cell from another column and pasting over the dropdown. Below is the code for one column.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C5:C5004")) Is Nothing Then
If Len(Target.Text) = 0 Then
MsgBox "You must select an item from the list!"
Target.Select
Application.Undo
End If
End If
Please advise if there is a way to limit copy and paste to the same column.
The spreadsheet that I am working with is for users to compile a large list of data, and I want to maintain data integrity with the drop down lists and validation of lengths etc. Once they are done I will take and using SSIS load the application with data in various tables as a speed load.
This is the only missing ingredient that I am needing. I am not a master at VBA which is why I am asking you.
From the code present in Excel VBA How to detect if something was pasted in a Worksheet you can adapt it to have something similar to this:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lastAction As String
' Get the last action performed by user
lastAction = Application.CommandBars("Standard").Controls("&Undo").List(1)
If Not Intersect(Target, Range("C5:C5004")) Is Nothing Then
' Check if the cell was cleared or the last action was a paste
If Len(Target.Text) = 0 Or Left(lastAction, 5) = "Paste" Then
MsgBox "You must select an item from the list!"
Target.Select
Application.Undo
End If
End If
End Sub
Tip: You can also detect other actions performed in the sheet. To do so just print lastAction to a MsgBox or Debug.Print and catch the ones you need.
HTH ;)