How to detect if ctrl + enter in an Excel spreadsheet with VBA - vba

My understanding is: Macro canNOT be activated while a cell is active for editing.
I notice by hitting ctrl + enter, the cell is de-activate for editing but remain selected. I would like then to run some macro refer to this cell.
Therefore, using VBA, how to detect if ctrl+ enter is pressed?

Easiest way should be using the Change event of the worksheet.
This would not (only) detect a ctrl + enter but any change of a cell.
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
This sub has to be added to the worksheets code (not into a module)!
Another approach would be using the OnKey Method
Application.OnKey "^{RETURN}", "InsertProc"
See here for a decent explanation: Application.OnKey Method (Excel)

Related

Excel Form pops automatically when sheet opens

I have a specific worksheet with 2 sheets(Sheet1 & Sheet2). For Sheet2 I have implemented a form for the table (Using the basic Excel Form from the top bar).
My problem is that I have to make the form appear automatically every time I open Sheet1 (even if the data from the form will be completed in Sheet2).
Is this possible? Or how can I do it? (I can also use VBA)
To show the DataForm associated with a Worksheet, you use the command Worksheet.ShowDataForm (MSDN Article)
To show the DataForm for Sheet1 whenever you go to Sheet2, you can use the Worksheet_Activate event in Sheet2, like so:
Option Explicit
Private Sub Worksheet_Activate()
Sheet1.ShowDataForm
End Sub
A quick way to figure things like this out is use the "Record Macro" button, carry out the action you want, and then hit "Stop Recording" and look at the macro

Cancel alt + down arrow: remaining effect from VBA code

I have a pivot table with page field. I wrote a few lines of code under the Pivot Table update event to do something.
When the page field filter is used via the keyboard, the filter form is accessed with the shortcut alt + down arrow typed on a page field cell.
Code works fine and at end moves focus to a specific cell.
Problem is that alt + down arrow effect remains! The cell is shown with a drop down list displayed and the operator needs to hit the escape key to continue working. I try to avoid this by adding a sendkeys {esc} line after, but the sendkeys numlock bug is more annoying than having to directly hit the escape key.
My question is:
Is there another way to close, using vba code, the drop down list displayed in final cell?
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Sheet1.ListObjects("precomanda").Resize Range(Sheet1.ListObjects("precomanda").HeaderRowRange, _
Sheet1.ListObjects("precomanda").HeaderRowRange.Offset(Sheet1.PivotTables("Istoric").PivotFields("Produs").DataRange.Count, 0))
Sheet1.Range("A5").Activate
Application.SendKeys ("{ESC}")
End Sub

Excel 2010 F2-Enter Macro Button

I have a chart with two columns that count horizontally across the grid cells with a specific color fill. (=CountCellsByColor(##:##,#) There are 24 Rows in each column.
I now want to create a macro button in a particular cell so the user can click the button and the columns will auto update. Currently this is accomplished by clicking into a cell, pressing F2 then Enter, which is fine but if I can make it simpler all the better.
This is what I have, I created a commandButton from the Developer tab (Insert, Button); Then in the VBA screen the following code;
Sub RefreshCells()
Dim r As Range, rr As Range
Set rr = Selection
For Each r In rr
r.Select
Application.SendKeys "{F2}"
Application.SendKeys "{ENTER}"
DoEvents
Next
End Sub
I get a restricted use error that pops up after I click the button, it works once but that's it then locks the rest of the cells from editing. I have a number of VBA codes running on the sheet that allow users of the sheet to simply click certain cells to turn them different colors.
I was making this far too complicated! If I may attempt to answer my own question, here is what I did to solve this,
Under Developer Tab, I hit Record Macro Then F2 Enter followed by Stop Record and saved that function. Then I created a Button under the developer tab and assigned it that macro.
Sub Macro5()
'
' Macro5 Macro
'
'
Range("D33:D34").Select
ActiveCell.FormulaR1C1 = ""
Range("D35").Select
End Sub
It's probably not right but it seems to have the same effect of clicking into a cell, hitting F2 + Enter . The columns that don't have an automatic count update perfect, no errors, no bugs!

Macro enabled button VBA

I'm working with VBA macros and I have a macro enabled button of which I want to disable from being clicked when the workbook is first opened. Is there anyway to do this. The macro behind it just leads to another worksheet. ![enter image description here][1]
Any help would be great
Thanks
If your button is an ActiveX Command Button, put the following code in ThisWorkbook:
Private Sub Workbook_Open()
Worksheets("Sheet1").OLEObjects("CommandButton1").Enabled = False
End Sub
Change "Sheet1" to reference the sheet that contains the button and change "CommandButton1" to reference the name of the button.

handle the event of finished editing of the cell value (vba)

Hi in my EXCEL workbook I am using vba. How can I handle the event of loss of focus of the cell or finished editing its value.
You can use the Worksheet_Change event. For example in the code below we are checking if any changes have been made in cell A1
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
MsgBox "Hello World"
End If
End Sub
Please note even if you press F2 and press Enter the above event will fire. If you need to run the code only if there is an actual change in the cell then you will have to take help of a Public variable which will store the previous value of the cell.
The Worksheet_Change event goes in the sheet code area. Please see screenshot.
See this Link: http://www.wiseowl.co.uk/blog/s194/event-handling-vba.htm
There are five different short, helpful articles on events, how to use them, and which events are available to you. This article was helpful to me also.