How to require values in cell if one cell has value - excel

Appreciate if anyone can assist me to derive a code for this situation.
I am creating a request form with column A to U and rows 11 to 60.
What I want is "when there's a value provided in 1 cell in column "A", when I click my submit button and I missed to populate other or some cells in the same row it should trigger a message to require a value from those cells.
For example, in column A cell 11 (A11) I entered "New Request" as the value, but I missed to populate E11 or G11 when I click submit I should have a message about it.
What makes it harder is that I need to do it per row. Request details are being populated per row.

In simple try following. Then you can modify codes to work dynamically for you.
Sub btnSubmit()
If Range("A11") = "New Request" Then
If Range("E11") = "" Or Range("G11") = "" Then
MsgBox "Value Required"
Exit Sub
End If
End If
End Sub

Related

Enter specific text into textbox and add +1 to another cell

I'm new to excel but now i'm stuck at something.
The one thing i try to achieve is that if i add a specific word into a textbox, another cell gets +1 (so if 0 and text has been entered in the textbox, it changes to 1 etc.)
so for example:
Cell B2 = Apple
Cell H2 : value of B2
I'd like to get, if possible, one or two textboxes where i could put the type of product and another box for the amount.
Thanks in advance.
OK. Here's a solution.
Set up a named range. I entered 5 different fruit in one column and 5 different quantities in the adjacent column (doesn't matter where but must be adjacent). I named the range "Products" but any other name will do just as well.
I set up a data validation list. I used cell G3 but any other will be equally suitable. I pointed the data validation list to =INDEX(Products,0,1), meaning the first column of the Products range.
Now I added code to the worksheet. This code must be in the code sheet of the worksheet on which G3 is located. That code sheet will have a name like Sheet1 (Sheet1). Don't use a standard code module with a name like Module1. Here is the code.
Private Sub Worksheet_Change(ByVal Target As Range)
' 018
Const Trigger = "G3" ' change to suit
Dim Qty As Long
With Target
If .Address = Range(Trigger).Address Then
On Error Resume Next ' in case not found
Qty = Application.VLookup(.Value, Range("Products"), 2, False)
.Offset(0, 1).Value = Qty + 1
End If
End With
End Sub
Note that the Trigger cell is named as "G3". This must be the cell where you have the data validation drop-down.
This code will run whenever Trigger is changed. You make a selection there and the VLOOKUP function will find the quantity in column 2 of the Products range. If the item isn't found in the list it will return 0 but you can set the cell validation to prevent the entry of an item that isn't in the list. The code will add one to the quantity found and issue the result in .Offset(0, 1), meaning one column to the right of the Trigger cell.
You might want to do other things with your idea. I think the system I suggest can be adapted to whatever you have in mind, including changing the quantity in the Products list.

Dynamic/Customize message with the entered value in a cell

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)

Warn if blank/empty cells

I'm trying to pop up a warning dialog if one of the cells in a range is blank.
Excel is popping up the warning when a cell is populated by a data validation drop down menu.
The code works on a range that doesn't contain data validation drop downs.
All data are strings and the cells are formatted as "General".
ActiveSheet.Range("I3:I10").Select
For Each cell In Selection
If cell.Value = "" Then
If MsgBox("Customer information missing. Okay to proceed; Cancel to stop and fill in missing information.", vbOKCancel) = vbCancel Then
Exit Sub
Else
Exit For
End If
End If
Next
The issue seems to stem from cells being merged across multiple columns, so Excel is checking each cell I3:K10 and finding J3:K10 blank. Unmerging the cells isn't an option.
If a cell is set up with data validation using a list and one of the cells in the list range is blank, the cell will be considered blank even if the user has selected the blank cell from the drop-down. However, if you also want to check if the cell is empty and does not have data validation, you can use the following (thanks to Determine if cell contains data validation)
Dim blnValidation As Boolean
Dim cell As Range
ActiveSheet.Range("I3:I10").Select
For Each cell In Selection
If cell.Value = "" Then
blnValidation = False
On Error Resume Next
blnValidation = Not IsNull(cell.Validation.Type)
On Error GoTo 0
If Not blnValidation Then
If MsgBox("Customer information missing. Okay to proceed; Cancel to stop and fill in missing information.", vbOKCancel) = vbCancel Then
Exit Sub
Else
Exit For
End If
End If
End If
Next
So, I was wrong with my initial guess, but the cure still would've worked.
How to avoid using Select in Excel VBA
The selection is actually Range("I3:K10"). So after it checks column I it moves onto Column J and then K
There a COUNTBLANK function but in this case you're probably better off with the COUNT Application.WorksheetFunction.
You could replace your loop with:
If Application.WorksheetFunction.Count(Range("I3:I10")) < 8 Then
'missing data - notify user here
End If

Excel, Add auto Sequence number

I need to ad the next sequence number in Column A automatically when I fill enter the next value in Column B. This sounds confused. Just see the snap so you will get the clear picture.
This should be done without usual dragging option. Is there any way
Make the Value of A1 equal to 1 and then from the A2 use the formula:
=IF(B3<>"",A2+1," ")
Drag this formula for the whole column.
In my solution I have a drag, but it is to define the formula for each of the fields. (I'm not sure if is this you are trying to avoid when you say
without the usual dragging option
)
You may achieve this using macros. Formula will increase the file size and processing time.
Right click on the sheet name on sheet tab-->select view code-->paste below code--> save file as macro enabled workbook.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo step
If Target.Column = 2 And Target.Value <> "" Then
Target.Offset(0, -1) = Target.Row - 1
End If
Exit Sub
step:
Exit Sub
End Sub
You can also do this without using macro and formula:
Type first 2 value to the cell to establish the pattern
highlight the cell range
Under Home tag -> select Fill -> choose Series
Follow the image and Select OK
If you convert your table to a List ("Table") and use a formula for the first column, that formula will "auto-extend" as new values are typed in under "Item"

Excel Data Validation Drop Down Box

I have a question that does not require any code. Let's say in Excel you set a data validation on a cell, and insert a drop down of specific values for the user to select from that cell. Let's say also that in VBA you are designating that cell a value from a DB. If the value from the DB does not match any of the values you have designated in the drop-down, will it populate the value in the cell? Or will it just leave it blank? Does anyone have experience with this?
Code will ignore the DV settings and simply populate it anyway. If you need to test afterwards whether it's valid data, check the Validation.Value and see if it's True:
With Range("T1")
.Value = "maybe"
If .Validation.Value Then
MsgBox "Valid entry"
Else
MsgBox "Invalid entry"
.ClearContents
End If
End With
for example.

Resources