Excel IF statement setting a fix value - excel

I just want to know the right syntax for this IF statement.
I just want to set a specific value to a cell.
My syntax is:
=IF(HOUR(F2)>6,F2=6)
Correct me if I miss something.

You could perhaps use the Data Validation feature of Excel. For example, in the below scenario, I'm putting this data validation on cell B2. So I select it first and go to Data > Data Validation:
Then I pick some options:
A user will be able to enter only a value between 0 and 6 inclusive.
A macro that could do that would be:
To use this macro, right click on the sheet tab and click on 'View Code'. Copy and paste the below, run (play button) and save.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$2" Then
If Target.Value > TimeValue("06:00") Then
Target.Value = TimeValue("06:00")
End If
End If
End Sub
Whenever a time above 6:00 am will be entered, the macro will automatically change it to 6:00 am. Formatting of the cell may be required.

Related

excel VBA Sendkey F2 for range of cells

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

Automatically add to user input

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.

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: Hyperlink to a cell specified by another cell's value

I have a document that needs to generate a hyperlink to a cell in the same workbook by getting the address from another cell. Here's what I have right now:
=hyperlink(CELL("address",INDEX('Budget Record'!C3:C105,MATCH(Y73,'Budget Record'!C3:C105,0),1)))
This displays the appropriate location:
'[Calendar Budget.xlsx]Budget Record'!$C$3
However, when clicked, it says that Excel cannot open the specified file.
I have tried manually creating a hyperlink to that value, and it still doesn't appear to work:
=hyperlink('[Calendar Budget.xlsx]Budget Record'!$C$3)
However, if I plug that into the goto dialogue box, it has no problems with it.
Am I missing an extra step?
After looking into all of the built-in hyperlink options that Excel offers, I could not find a way to have Excel link you to a dynamic/ changing cell reference. Naturally, I turned to VBA :)... this is a very simple macro with only a few lines of code. Give it try:
Press Alt + 11 to open the Visual Basic Editor (VBE)
If your project explorer is not already open, click this icon:
Double click the worksheet tab that you want the hyperlink to work on:
Paste the following code into the white space:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'check if user is selecting the 'hyperlink' cell
If Target.Address = "$A$1" Then
'run the 'selectCell()' subroutine
Call selectCell
End If
End Sub
Sub selectCell()
Dim goToAddress As String
'get cell address
goToAddress = Range("A2").Value
'send user to this cell
Range(goToAddress).Select
End Sub
Go to your worksheet and test the script by typing G3 in cell B1.
Select cell A1. The macro should have automatically selected cell G3.
Hope this helps!
(Posted on behalf of the OP).
I have identified a solution. Basically it looks like the problem was in the formatting.
Basically I needed three steps. The first one is getting the location of the cell returned as a full address (including the file, sheet, and Cell). I do that with this:
=CELL("address",INDEX('Budget Record'!C3:C105,MATCH(Y73,'Budget Record'!C3:C105,0),1))
Here's a sample of what that results in:
'[Calendar Budget.xlsx]Budget Record'!$C$3
The problem with this is that the apostrophe at the beginning is in the wrong spot. To work as a hyperlink, the string should be this:
[Calendar Budget.xlsx]'Budget Record'!$C$3
So, I have a step where I remove the first 22 characters of the starting string:
=RIGHT(Z73,LEN(Z73)-23)
This results in the following:
Budget Record'!$C$3
Next, I need to add on this to the start of the string:
[Calendar Budget.xlsx]'
I do that with the following:
=HYPERLINK("[Calendar Budget.xlsx]'"&AA73)
The resulting output looks like this:
[Calendar Budget.xlsx]'Budget Record'!$C$3
So, we have a hyperlink to a cell in another sheet that dynamically changes depending on the initial referenced cell.

how to make an excel cell to show today's date

I was trying to make an excel cell print today's date like this
=DATE(2013,3,23)
But it only prints that date until some user changes the numbers.
So please help me!
=Today()
This formula is dymanic and will always show the current date.
You could do this, or use the Date() formula, and then copy/paste values.
But even if you do this, the user can still "change the numbers" after the fact, so you can't avoid that unless you protect the sheet, or implement some sort of event-based macro to maintain the desired date.
Here is a simple macro that will always put today's date in Cell A1 no matter what the user does to it. Place this in the Worksheet's code module.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub 'This will only trigger the event if the cell A1 changes. You can modify this as needed.
Target.Value = DateSerial(Year(Now()), Month(Now()), Day(Now()))
End Sub

Resources