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
Related
I have a memo field in an MS Access database where I create notes. I often use bulleted lists in this note field that is displayed on a form with a large text box.
The MS Access default when tab key is pressed is to cycle to the next control, however when I'm editing a "note" (memo field type) text box making a bulleted or numbered list I want to indent that line.
I have been able to "catch" the tabkey and execute "IndentIncrease" from the commandbar object. ("Catching" the tab key required using the KeyPress event of the text box, and turning off tabstop on all objects in the form).
However, this indents the bottom line of the field, even if the cursor is in the middle somewhere. Using the mouse to click the "increase indent" button in the text formatting section of the Ribbon works fine and indents the intended line of the ribbon.
Is there any way to get my code to catch the tab key and indent the correct line of my memo field?
Private Sub tbNoteEditor_KeyPress(KeyAscii As Integer)
If KeyAscii = 9 Then
Application.CommandBars.ExecuteMso ("IndentIncrease")
End If
End Sub
In this case, you want the KeyDown Event, as opposed to KeyPress. KeyPress represents a character being typed, which is not always the case with Tab. KeyDown will grab any key. Also, if you are catching Tab to increase the indent level, you probably also want to catch Shift-Tab to decrease the indent level. And, you will not need to turn off TabStop all over your form. :)
Private Sub tbNoteEditor_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 9 Then
If (Shift And acShiftMask) > 0 Then
Application.CommandBars.ExecuteMso ("IndentDecrease")
Else
Application.CommandBars.ExecuteMso ("IndentIncrease")
End If
KeyCode = 0
End If
End Sub
I am trying to help another user in this forum (reference). The goal is to use the Enter key on the numeric keypad to trigger a macro rather than use the Worksheet_Change event macro for the same purpose.Before anything is run, both Enter keys work identically. You type stuff in, touch either key, the material gets placed in the cell and Selection moves downward. If you just pump either key, Selection just moves downward.
In a standard module I put:
Sub NumericEnter()
Application.OnKey "{ENTER}", "InsertIntoTables"
End Sub
and
Sub InsertIntoTables()
MsgBox "Inserting"
End Sub
After I run NumericEnter(), the Enter key on the numeric keypad calls the proper macro each time it is touched. The normal Enter key is not affected.
I then tried to restore the numeric Enter key by running:
Sub ClearEnter()
Application.OnKey "{ENTER}", ""
End Sub
The numeric Enter key partially reverts to its original behavior. It no longer calls the macro; it allows data entry into cell, but it does not move Selection at all! (the regular enter key still behaves normally)
I have no idea of what I have missed.
What I have looked at:
The normal enter key, the ARROW keys, and the TAB key work just fine at changing Selection
Mouse clicking works fine at changing Selection
the values of Application.MoveAfterReturn and Application.MoveAfterReturnDirection look fine as well.
Using just
Application.OnKey "{ENTER}"
with no second argument seems to clear the macro mapping and return the default key action (Excel 2013)
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-onkey-method-excel
expression.OnKey( Key , Procedure )
The description for the Procedure parameter has this:
A string indicating the name of the procedure to be run. If Procedure
is "" (empty text), nothing happens when Key is pressed. This form of
OnKey changes the normal result of keystrokes in Microsoft Excel. If
Procedure is omitted, Key reverts to its normal result in Microsoft
Excel, and any special key assignments made with previous OnKey
methods are cleared.
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!
Usually in excel, when we press the "->" in keyboard, the cell will move from "A1" to "B1". However, it does not work on a drop-down list.
I create this drop-down list by data validation. When I press "->", nothing happen. I have to use mouse to click it every times, it is trouble. Is there any way, or any key, to let me move to new cell?
Assuming you have your drop-down box on a worksheet, you are right that it doesn't work.
Although you may have lined the drop-down box with the cells around it, it is an object sitting over the cell, when you push -> (the right arrow?) or the tab button the selection will go to the next cell, which is under the drop-down box.
The only way to get focus onto the drop down box is to click on it, from there you can use up and down keys to change selection and enter to select an item. After selection focus is given back to the selected cell in the worksheet.
You have tagged this question as 'vba' so to that end what you could do (although it may be a little excessive) is
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Replace(Target.Address, "$", "") = "C2" Then
UserForm1.Show 1
End If
End Sub
The above code is placed with the required sheet, if the selection is equal to C2 then a UserForm opens, within that you could build the selection that are keyboard compatible so you don't need to reach for the mouse.
I am trying to create a path selection user interface for an extensive VBA program I've been working on, but I can't seem to get the ellipsis textbox that I'd like. This is a very common feature, especially in option tables. This is an example of what I'd like to get, straight from the VBA Options panel:
I would LOVE to find a way to get the same functionality in a Userform. The only solution that I've found thus far is to use a combo box with the ellipsis arrow option enabled. However, there doesn't seem to be an apparent way to use the activation of the combo box arrow to run a dialog box, nor does there seem to be a way to make it look UNLIKE a combo box. Last resort I use a button below the text box, but I'd really prefer a less-bulky way of doing this.
Any solution would be greatly appreciated.
The only solution that I've found thus far is to use a combo box with
the ellipsis arrow option enabled. However, there doesn't seem to be
an apparent way to use the activation of the combo box arrow to run a
dialog box, nor does there seem to be a way to make it look UNLIKE a
combo box
Your suggestion does work, and it is surely less complex and more elegant than having two controls work together, Button + Textbox.
A Combo can achieve perfectly the desired feature, in the following way.
1) In design mode, set the button style to Ellipsis
DropButtonStyle = fmDropButtonStyleEllipsis
And eventually, make the ellipsis show up only when the combo is entered, by setting the design-time property:
ShowDropButtonWhen = ShowDropButtonWhenFocus
2) If needed, you can set other design-time properties to have some look and feel. The defaults look pretty good however.
3) Add the following handler to the parent userform. The snippet simulates the launching of a dialog and getting a new value or cancelling. It does not show any dropdown window. (but you still have control over that: if you want to show it according to some condition, you still can call the method ComboBox1.DropDown)
Private Sub ComboBox1_DropButtonClick()
' The following two lines avoid to call the routine twice, at entry and at exit
Static i As Integer
i = (i + 1) Mod 2: If i = 0 Then Exit Sub
With ComboBox1
s = InputBox("enter some text", , .Value) '<~~ simulates any dialog
If s <> "" Then .Value = s
SendKeys ("{Enter}") '<~~ to close immediately the dropdown window
End With
End Sub
Try it ;)
Not only do ComboBoxes have Drop Buttons, so do TextBoxes (as do Excel's RefEdit controls). Even though you can't access the Textbox's Drop Button at design time, you can do so at runtime. Using a textbox avoids having to deal with the dropped down list of a combobox.
Given a textbox named TextBox1, the following code will provide the desired ellipsis drop button:
Private Sub UserForm_Initialize()
With Me.TextBox1
.DropButtonStyle = fmDropButtonStyleEllipsis
.ShowDropButtonWhen = fmShowDropButtonWhenAlways
End With
End Sub
Then use the DropButtonClick event of the textbox to perform whatever action you want:
Private Sub TextBox1_DropButtonClick()
'' Code here to do what you need
End Sub
I have an extensive example at Alternative to Excel’s Flaky RefEdit Control that uses a textbox with a "Reduce" drop button to replicate the functionality of Excel's unreliable RefEdit controls.