I'm running into an issue where I've set up Data Validation for characters that = 10 in a specific column in Excel. My issue is, if a user pastes over the cell, the data validation gets overwritten. Is there a way to accomplish a text limit by using VBA, that'll alert the user to only have 10 characters in a cell within a defined range? One that won't get overwritten if a user pastes over a cell?
Paste this into sheet's module:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
For Each cell In [E14:E1000]
If Len(cell) <> 10 Then
MsgBox "Text length in cell """ & cell.Address(0, 0) & """ does not equal to 10.", vbExclamation
Application.Undo
Exit For
End If
Next
End Sub
Explanation
When you paste text onto sheet, the procedure scans each cell in a range. As soon as it finds text with length more than 10, it notifies user which cell breaks the limit and undoes all the changes with Application.Undo. Then it immediately exits.
Related
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.
Is there any way by which i can display the data validation message with the entered value in a cell.
Example : A cell which has been restricted to enter the value between 5 to 10, and If i am entering the value as 11, it should display the message saying
"11 does not come under cell restriction value".
I know we can enter the customize message(cell> Data > Data Validation > Error Alert) but i want the message to show the current entered value along with my customized message not only generic message.
and also if possible i want to concatenate any other existing column value of that particular row in future with the error message.
Therefore, can anyone please advice me on the below queries :
How can I display the cell entered value with the message
And also another cell value of different column of that specific row.
Thanks !
Without using Data Validation, this code will check for changes in a range you define. Then compares the value of the changed cell inside that range against the conditions you set (between 5 and 10 in this case).
Let me know if this works as you need:
(Copy and paste onto the Sheet where this validation is needed)
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("B2:B10")) Is Nothing Then 'Change the range as you need
If Target = "" Then Exit Sub 'If the value is deleted, do nothing
If Target < 5 Or Target > 10 Then 'Validation condition(s)
MsgBox Target & " does not come under cell restriction value" 'The message will show the value in the changed cell (Target)
Target = "" 'Delete the not valid value
End If
End If
End Sub
You can also use the following code in the same code sheet to get the changed cell's row, and find another value in the same row and different column:
Me.Range("A" & Target.Row)
I need some help with an excel problem.
It is a combination of the two problems below:
1) Excel - Popup message with sound alert when cell value meets certain criteria
2) VBA code to show Message Box popup if the formula in the target cell exceeds a certain value
In Sheet1, I have a range of products and sales figure.
Example: Sheet1
In Sheet2, I have multiple columns of sumif() functions. Example: Sheet2.
It contains a column of names in (A:A) and data in (B:B) & (C:C) which are linked to cells in other sheets. I would like a pop up notification saying ("Text in column A" sold is > 20) when the value of any cell in column B exceeds 20 or column C exceeds 40.
For example: If one of the cell value in column "B" gets updated to 33 (which is >20) and the corresponding cell value in column "A" contains text as "Charlie", excel sheet should popup message saying "Charlie sold is > 20".
The below VBA code accomplishes this IF it was raw data. However, it does not work when the cells are linked to data from other sheets, as in the case of this workbook.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.column = 2 and target.value > 1 Then
MsgBox target.offset(0,-1).text & " sold is > 20"
End If
End Sub
This alternative code works with data linked from other sheets, however it only applies to a specific cell, not an entire column.
Private Sub Worksheet_Calculate()
If Sheets("Sheet2").Range("B2").Value > 20 Then
MsgBox "B2 is >20", vbOKOnly
End If
End Sub
What I'm trying to achieve is this: As I input my raw data in Sheet1, the figures in Sheet2 column(B:B) and column(C:C) get updated. Once any of the cells in Sheet2 column(B:B) exceed 20 or column(C:C) exceed 40, there will be a popup notification that links back to column A text such as MsgBox target.offset(0,-1).text & " sold is > 20". Is there a way to combine the above two codes to achieve this?
Any alternative solutions are welcome too, thank you!
Compare all the Sums in Summary table
Private Sub Worksheet_Calculate()
Dim RangeToCheck As Range
Dim Cell As Range
Set RangeToCheck = Sheets("Sheet2").Range("B2:B5") 'See comment #1
For Each Cell In RangeToCheck.Cells
With Cell
If .Value2 > 20 Then
MsgBox "Product: " & .Offset(columnoffset:=-1).Value2 & _
" in cell: " & .Address & " is " & .Value2 & ">20", vbOKOnly
End If
End With
Next
End Sub
Comments
I recommend turning the range on Sheet2 to Excel table and using ListObject and Listcolumns instead.
Off topic hint: You can also use some counter, put the range into an VBA array and loop through array, it will be faster, than refering to sheet cell one after another. See Writing efficient VBA UDFs (Part 1).
I have 5,000 part numbers contained on one sheet of an Excel workbook (Part Numbers), ranging from cell A1:A5000. I want to find a way to click on any one of those part numbers and have it automatically populate into cell D7 on another sheet of the same workbook (Price Sheet). What is the easiest way to accomplish this task?
To do it that way, you will have to write VBA code to catch every SheetSelectionChange event to see if the new selection range is in your cells A1:A5000. And then if it is, execute the VBA code to fill OtherSheet!D7.
If I recall correctly, the VBA code to do this would look something like this:
Private Sub WorkSheet_SelectionChange(ByVal Target As Range)
'Check if it is in the range A1:A5000
If Target.Column = 1 And Target.Row <= 5000 Then
'get the target worksheet
Dim TargetWS As Worksheet
Set TargetWS = Sheets("OtherSheetsName")
'copy the value of the just selected cell to D7 on the target WS
TargetWS.Cells(7, 4).Value = Target.Value
End If
End Sub
(Oops, forgot about the need for "SET" in VBA.)
You can do this without VBA:
Select the partnumbers A1:A5000 and type PartNumbers in the Name Box (to the left of the formula bar) and press carriage return (OartNumbers should now be visible in the Name Box whenever you select a1:a5000
Now go to cell D7 on Price Sheet, and use Data Validation-->List and enter =PartNumbers in the Source box
Now you can select any of the 5000 part numbers from the dropdown in cell D7
Hello: I have a set of cells on a worksheet called "Docs". The cell range is (B13:C23).
When users get taken to this page, they are meant to fill out each of these cells with a value from 0 through 6. My Question: Is there some code that I can attach to this sheet where, if a user does not fill in a cell with anything (ie. leaves it blank) and tries to leave the sheet or close the workbook, they are somehow reminded to fill it in? Or is there a way to not let them leave the sheet until it's completed? Thanks.. Allan
You can attach a macro to the change event of the form. Excel comes with built in validation but it does not work that well. For instance if someone pastes a value into the cell it does not validate what is pasted.
Start by creating a range by selecting the range of cells to be validated, right click and select "Name a Range". Note that I am testing this with Excel 2007. Say you call your range "InputRange".
Then open the VBA editor and create a procedure for the change event.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim vrange As Range, cell As Range
Set vrange = Range("InputRange")
If Intersect(vrange, Target) Is Nothing Then Exit Sub
For Each cell In Intersect(vrange, Target)
If cell.Value < 1 Or cell.Value > 6 Then
MsgBox "Invalid Entry", vbCritical
Application.EnableEvents = False
cell.ClearContents
cell.Activate
Application.EnableEvents = True
End If
Next cell
End Sub
Note you can attach to any event that suits you.
You could give these cells conditional formatting, making them red if empty.
Try writing a vba macro. Alt + F11 opens the VB Editor. Check out this SO post for VBA tutorials.
There are worksheet and workbook events that you can use. For example, Workbook_BeforeClose or Workbook_SheetChange. If you create methods for those events you can put code inside that checks that the required cells are filled.