live execution of formula - excel

can we execute a formula without moving cursor from the cell? After giving data in to Input cell either we press enter or we move from that cell and then any formula related to that gets executed. Is there any way that while entering input itself we can see the result in output in excel?Please help me in finding this option.

I don't believe it's possible for code to execute while the "editor" of a cell has focus and control. For example, try doing something else in Excel while you are editing a formula. I believe that the cell has to finish saving its value or formula before code can execute.
You can have a formula "listen" for when a cell is updated, but this is a little bit complicated. It could then force you back into the original cell once it is updated. Here's how you can write some code to "listen" for when a value of a cell changes. In the following example:
I record the value of Cell I1 to strValue.
Then I run a loop that stays running as long as the value of cell I1 has not changed.
The DoEvents command releases control back to the processor, which is what allows you to work in Excel while this is running.
Once the value of cell I1 changes, the While statement will be false and the loop will exit.
After the loop exits, a message box pops up that tells you the value changed and then the original cell is selected again.
'
Sub testChange()
Dim strValue As String
strValue = Range("I1").Value
Do While strValue = Range("I1").Value
DoEvents
Loop
MsgBox "The value changed!"
Range("I1").Activate
End Sub
Perhaps if you simply wanted to return control back to the original cell every time the value changed, you could use the line above without the message box and return to the loop once your method executes. Keep in mind that you might have to figure out another way to exit your code if you do that.
Why do you want to have it execute from within the cell? What is the end purpose of your task?

Press F9 to do a re-calc without moving from the input cell. This works in Excel 2007. I'm not sure about earlier versions.

Related

Insert values in cell in VBA if previous is empty upon each click of a button

I have this simple vba,
Private Sub CommandButton7_Click()
Range ("F1:F100").Value=Time
End Sub
As you already know, this will insert the current time in cells from F1 to F100(all at once) whenever I click the button once.
However, I want to insert time in each cells for each click. If I click the button first time, only f1 should be filled in. The second click should fill in f2 only because the previous cell is empty and so on and so forth.
I am not sure if it should be done in loop but I was unable to find references. I am trying to find a simpler code to achieve this.
Thanks commentators for your assistance.I just put the answer below in case any one needs it. The code below served my objective.
Once a cell is filled in upon clicking, you can directly go to the next cell and fill in the cell value upon clicking the button again,
Dim timeCell as Range
Set timeCell = Range("F" & rows.count).End(xlUp).Offset(1)
timeCell.value = time

I need a VBA code that after Userform enters date into a database, refreshes the cell the same way as going into it and pressing enter does

I have a long chunk of code that handles date data from a hidden datasheet, however anytime I enter a new date trough a userform, it writes it into the cell in the database in a format that for some reason can't be handled error free by my code chunk.
The problem is I need a sub to change the format (which is easy) but it does not actually apply the formatting unless I manually go into the datasheet, enter the cell and press enter.
I have searched and could not find a solution on how to do this in VBA, obviously this code is supposed to run automatic flawlessly plenty of times and having to manually go in an enter the cell defeats the entire purpose of the programming.
Sub Testing123()
MsgBox "run"
Workbooks("Excel Stock System.xlsm").Worksheets("DataNews_Events").Columns(8).NumberFormat = "dd.mm.yyyy hh:mm"
'something magic that actually automatically applies the format
End Sub

Most efficient way to remove all linebreaks in worksheet

I have a macro which iterates over column C, anytime the cell is not empty it opens a file referenced in Column B, searches this newly opened file for a string that is written in column A and then replaces the content of that cell with what is written in column C. My problem though is that in the newly opened file, there are line breaks in some columns (sometimes trailing, sometimes where there should only be a space), so the macro doesn't find the right cell.
I was thinking to solve this I write another macro which goes through those files and replaces every line break with a space. It's basically just
For Each Cell In Worksheet.Range(Cells.Address)
Cell.Value = Replace(Cell.Value, Chr(10), " ")
Next
This seems to work, but I can't run it without excel crashing. Are there more efficient ways to do this? Or maybe all together a better approach to my problem? Thanks.
The VBA DoEvents function temporarily pauses a running macro, giving Excel a chance to process key presses, mouse clicks, and other operating system messages.
In long-running macros, Excel can appear to hang and become unresponsive, and the macro may be impossible to interrupt. If DoEvents is included in your code, users can be assured that the macro is still running, and can still interrupt execution if necessary.
For Each Cell In Worksheet.Range(Cells.Address)
Cell.Value = Replace(Cell.Value, Chr(10), " ")
DoEvents
Next
Source: automateexcel
This (I hope) is an implementation of BigBen's suggestion for the active sheet:
Sub test()
Dim r As Range
Set r = ActiveSheet.Cells.SpecialCells(xlCellTypeConstants)
r.Replace Chr(10), " "
End Sub
EDIT#1:
The code above relies on the proper worksheet being Active. A safer alternative would be to use something more explicit like:
Set r = Sheets("my data").Cells.SpecialCells(xlCellTypeConstants)
NOTE:
We are using the .Replace() Method rather than the Replace() function.

Managing Excel Worksheet_Change Feature

I have created a spreadsheet which uses the Worksheet_Change feature and the code associated with that works very well. I can stop it firing unnecessarily when inside the module by using Application.EnableEvents = False.
However, while I've created a form to enter data directly into the next available row (again, that works fine in terms of entry) it doesn't cause the formulae in the sheet to calculate (even though auto calculation is on and re-enabled within the module). If I manually place my cursor in the row, hit F2 and simply press enter, everything then fires up.
I have tried to enter data directly into the cells, but of course the Worksheet_Change feature then kicks in again and the cursor isn't simply moving to the next adjacent cell ....
I've tried to check firs for any direct entry with the code below and if it looks like the user isn't entering directly into the cell, the Worksheet_Change is disabled:
Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo eventhandler
Sheets(1).Range("a1").Select
LastCell2 = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
Dim intersection As Range
Set intersection = Intersect(Target, Range("A3:F" & LastCell2))
If intersection.Row = LastCell + 1 Then
Exit Sub
End If
Application.EnableEvents = False
The code above is simply checking to see if data is being entered into the next empty cell and if that's the case I want it to just exit there but it isn't working.
So I actually have 2 problems :
the first is why this formula isn't triggering after entry via a vba form - I've used INDIRECT since there are other macros that delete rows by moving the remaining cells up and that was causing the count in the $A$3:$A$500 to reduce to $A$499 and then 498 etc - the addition is done depending on the system date and the transaction date so I get a current value and a future value using a standard sum statement:
=AD1-(SUMIF(INDIRECT("$A$3:$A$500"),"<="&TODAY(),INDIRECT("$E$3:$E$500")))+(SUMIF(INDIRECT("$A$3:$A$500"),"<="&TODAY(),INDIRECT("$F$3:$F$500")))
The second is why I can't enter data directly into the spreadsheet and trap the fact that I don't want it to do anything and simply allow the user to hit enter and move to the next adjacent cell to the one they just entered data into.
Is this a lost cause and am I trying to do too much here? I'm relatively new to coding and teaching myself so apologies if the standard and style isn't to everyone's taste.
Thanks in advance for any replies.

Excel Worksheet_Change not working when macro changes cell

I'm trying to finish up a project of mine and I right now have a Form Control that when pressed adds to the value of a number and another button will subtract that value.
Another value has the two different buttons for the same thing, but the value is also dependent on the first value and other things than just the buttons modify that value. I tried implementing this code for validation
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("F19")) Is Nothing Then
If Range("E19") = 2 And Range("F19") < 12 Then
Range("E20") = 1
End If
End If
End Sub
but Excel apparently doesn't recognize that cell F19 has changed when the change is caused by the button, only when it is caused by user input. So, what this is saying is, if F19 updates and 19 is 2 and F19 is less than 12 (the prerequisite for E12 being 2 is F19 being 12 or greater) then set E20 to 1 (E20 is a modifier for E19 which also has other modifiers going into it). This method works on other values that aren't button controlled, but how can I get excel to realize when the Form Control button changes the value (or at least monitor when the form control is pressed.)
Edit: The macro actually doesn't work if the cell changes by formula either. I don't think I can use Worksheet_Calculate to monitor the change in a specific cell.
Why are you doing this with code anyway? You could have Cell E20 have a formula like: =IF(AND(E19=2,F19<12),1,"") which would make the cell blank unless the condition is met.
If you really want to do it with code, you should take this into account: The Worksheet_Change event "Occurs when cells on the worksheet are changed by the user or by an external link."
I would recommend instead of having
If Range("E19") = 2 And Range("F19") < 12 Then
Range("E20") = 1
End If
In your Worksheet_Change event that you add it as a separate sub, that you call from Worksheet_Change. You would also call the sub from the code for your button, after you've performed whatever action your button does. That way, you're guaranteed the check gets run and do not try to rely on events.
Daniel is partialy right, Worksheet_Change "Occurs when cells on the worksheet are changed by the user or by an external link." But this includes changes caused by VBA, and Excludes changes by a formula.
Your problem may be caused (or at least exacerbated) by the only partial qualification of your ranges:
Range("E19") will refer to 'E19' on the active sheet, which may or may not be be the sheets Target is on. You got it right with Target.Worksheet.Range("F19")
Try (note the .'s)
With Target.Worksheet
If Not Intersect(Target, .Range("F19")) Is Nothing Then
If .Range("E19") = 2 And .Range("F19") < 12 Then
.Range("E20") = 1
End If
End If
End With
BTW. I'm with Daniel in that your whole solution seems a little off, but we may not be getting the whole picture...

Resources