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.
Related
I have been looking for a straightforward and reliable method to do sendkey "F2" on range of cells.
I want users to be sent directly to edit mode when selecting certain cells, which will allow them to copy or enter data in a more user friendly manner.
Kind regards,
Luke
Thanks for your help everyone. Found the answer I was looking for on another forum.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge = 1 Then
If Not Excel.Application.Intersect(Target, Range("CertainCells")) Is Nothing Then
VBA.SendKeys "{F2}"
End If
End If
End Sub
I am not sure if it is a good idea to change into "edit mode" for some specific cells. The only difference is that the cursor gets visible and jumps to the end of the cell content if the cell is not empty. While it may be convenient to immediately append data, the price is that it is not possible to simply press Ctrl+C to copy the cell content. However, I will not argue with you.
As already written in the comment, use the Selection_Change-Event and check the parameter target if one of your "certain cells" was selected. If target.count is greater that 1, the user selected multiple cells - you probably don't want to enter Edit mode in that case.
The following example sends F2 if a cell in column B was selected - adapt it to your needs. You need to put the code into the sheet module of the sheet where you want the magic to happen and change the If-condition to your needs.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column = 2 Then ' Change this to check for the cells you are interested in
Application.SendKeys "{F2}"
End If
End Sub
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 have a worksheet with two tabs and wanted to have a VBA to run automatically and pop up a window message saying "NPV negative! Please enter future Savings!" anytime cell J27 (in both tabs) calculates negative value.
I have the below code in the Module and when I test and click run it does what I wanted to do but in reality when I change information and cell J27 is a negative number nothing happens. I'm very new with VBA so any help is appreciated.
Private Sub Worksheet_Calculate()
For Each cell In Range("J27")
If cell.Value < 0 Then MsgBox ("NPV negative! Please enter future Savings!"), , "Invalid Entry"
Next cell
End Sub
Thank you!
The Worksheet_Calculate subroutine is only going to be triggered when the worksheet that you created it in is recalculated. Range("J27") is only looking at the cell in that worksheet, so your loop only iterates one time.
To get the value in cell J27 from both worksheets, you need to specify the sheet and the range for each one.
Private Sub Worksheet_Calculate()
If Sheets("This Sheet").Range("J27").Value < 0 And _
Sheets("That Sheet").Range("J27").Value < 0 Then
MsgBox ("NPV negative! Please enter future Savings!"), , "Invalid Entry"
End If
End Sub
Put that in both worksheets to get it to run when either worksheet is recalculated. (Or, better yet, save it in a separate Module and call it from both Worksheet_Calculate subroutines.)
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 ;)
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