excel: use keyboard to move column in data validation mode - vba

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.

Related

Change the color of a selected record in an Access REPORT

My Access REPORT has a text box with the Record ID that looks like a button with an on click event to go to a form for that specific record. This works great, but when I return to the report I cannot see which record was clicked. I want to temporarily change ONLY the record that was clicked until another record is selected.
The reason I want this on a report and not a form is because I want the user to have a quick way to proof read in the format needed to print, and make a change or check a detail if necessary, then update the report AFTER all proof reading and updates are completed and before final print. But with many records on the screen it is easy to lose track of which record you were checking when returning from the form.
I tried:
Private Sub btn_txt_GoToTransaction_Click()
Dim vColor
vColor = RGB(51, 204, 51) 'green
Me.btn_txt_GoToTransaction.BackColor = vColor
DoCmd.OpenForm "Account_frm", acNormal, , "[TransactionID]=" & Me.TransactionID
End Sub
But this does not work because every button turns color not just the selected record.
Any suggestions? Thanks.
This is a great question because there are many benefits to highlighting a row or item in an Access Report. You are not able to just change the button color in one row only, but you can highlight the whole row so the user knows where they were.
Here are two methods to accomplish this:
Method 1 - Click on a Label
This works great in newer versions of MS Access when using Report View. Use a Label Control instead of a Button. You could make the label look like a button if you format it that way. I prefer to stretch an invisible Label across the whole row on top of all the other controls in that row. Then if you click anywhere in the row, it automatically selects that row and then runs whatever code you have in the OnClick Event. This works best if the Label is not linked to a Text Box.
This picture shows an example of how this method looks. You can click anywhere in the row and it highlights that row with the red outline and grey background.
This is very simple and works well but there are a couple disadvantages:
1- You can not change the color of the highlight.
2- If any of the text boxes CanGrow, the row height may be higher then the Label and create areas where the invisible label doesn't capture your click.
3- Clicking on a Text box does not work for this method.
Method 2 - Change Color of a Text Box
In order to just highlight one row or one piece of data in a report, we can use the "FormatConditions" property. This is the same as Conditional Formating from the MS Access design interface but we are going to change it programmatically on the fly. You can't do this with a button or label - it needs to be a Text Box with unique data, such as your TransactionID.
This picture shows an example of how this method looks. You can set the color of the highlight if you follow the steps below.
STEP 1) I recommend that you add a text box to your report that stretches from the left to the right, set the Back Color and Fore Color to White, set the Control Source to TransactionID, and set the Name to TransactionID. Then right click on this text box and select Position > Send To Back. This works best if the other text boxes and labels on the report have a transparent background.
STEP 2) Add this code:
Private Sub HightlightRow(intRowID As Integer)
With Me.TransactionID.FormatConditions
.Delete
With .Add(acFieldValue, acEqual, intRowID)
.BackColor = vbGreen
.ForeColor = vbGreen
End With
End With
End Sub
STEP 3) Also change your button code to call this subroutine like this:
Private Sub btn_txt_GoToTransaction_Click()
HightlightRow Me.TransactionID.Value
DoCmd.OpenForm "Account_frm", acNormal, , "[TransactionID]=" & Me.TransactionID
End Sub
STEP 4) I like to set it up so if the user clicks anywhere in the row, it will pop up with a modal with more detail regarding that row. Also, the user can't make any changes to the data in the Report View, so I use the pop up modal to allow changes. To accomplish this, I do a couple more things:
First, we need to add the code to the OnClick event for every control in that row. Ofcourse, each OnClick event will simply can that subroutine HightlightRow Me.TransactionID.Value
Second, if the user clicks on a Text Box, the Text Box gets the focus and hides the highlight. Therefore, I like to set the focus to something else. In your case, you could set the focus to the button by adding this line to the end of the HighlightRow subroutine: btn_txt_GoToTransaction.SetFocus
In my case, I am not using a button, so I set up a tiny Text Box with = " " (just an equal sign a space in quotation marks) as the Control Source. Then I position this tiny Text Box to the far right. And in the HighlightRow subroutine, I set the focus to this textbox.
STEP 5) You may also want a button or method of removing the highlight. To do that simply have the code run this line:
Me.TransactionID.FormatConditions.Delete

ComboBox inputbox causes reselection of range

Currently I'm working on a userform prompting an inputbox for a range whenever the combobox dropdown button is clicked.
The problem is that whenever the range is selected (selecting a cell and then clicking ok), the userform is unselected (greys out), shows an empty dropdown list and forces me to reselect a range after I click anywhere on the workbook.
Is there any way to prevent a re selection of a range when clicking the dropdown button?
Code Below:
Private Sub ComboBox1_DropButtonClick()
Dim InputCell As Range
Set InputCell = Application.InputBox("Select Lookup Cell", "Obtain Object Range", Type:=8)
ComboBox1.Text = InputCell.Address(0, 0, external:=True)
End Sub
When I've used the RefEdit object in the past, I used the real thing (which I know has it's issues) and just used the Change event.
Perhaps you could just use the Enter event (you just have to ignore it the first time, if's it the first object that gets focus when initialized)
Resources:
Using RefEdit Controls in Excel
Dialogs - Jon Peltier
RefEdits must be placed directly on the UserForm itself. If you put a
RefEdit in a frame or on a multipage, strange things will happen,
including bizarre Excel crashes.
RefEdits must not be used on modeless forms. RefEdits on modeless
forms will result in bizarre Excel crashes.
RefEdit event procedures should be avoided. RefEdit events do not
behave reliably, and they may result in VBA errors which are difficult
to debug.
References to RefEdits must be removed. When a UserForm is added to a
VB project, Excel adds a reference to the Microsoft Forms 2.0 Object
Library. This reference is required for proper operation of the
UserForms in your project. To see what references your project has,
select the project in the Project Explorer, then select References
from the Tools menu.
Alternative to Excel’s Flaky RefEdit Control - Jon Peltier
The new approach uses a TextBox in the dialog instead of the RefEdit.
The TextBox does not interact directly with a range as does the RefEdit. Instead, when this drop button is clicked, the dialog is temporarily hidden, and an InputBox appears to solicit the user’s input.
"It may be tempting to use type 8 to indicate a range object, but there
is an obscure glitch that causes it to fail when the worksheet
contains conditional formatting conditions that use formulas in their
definitions. Fortunately using Type 0 to specify a formula works just
fine."
Cannot use keyboard shortcuts to select ranges in RefEdit control in Excel
From this source: Ozgrid
Though I'll be using custom text boxes, this could also work if anyone wants to use the combo boxes.
Private Sub ComboBox1_DropButtonClick()
Static Abort As Boolean
If Abort Then Exit Sub
Abort = False
Dim InputCell As Range
Set InputCell = Application.InputBox("Select Lookup Cell", "Obtain Object Range", Type:=8)
ComboBox1.Text = InputCell.Address(0, 0, external:=True)
Abort = True
End Sub

Excel VBA: Deselect a cell

Is there a way to deselect a cell with Excel VBA? My situation is I have a worksheet double-click event, so the cell must be selected for the macro to run. But that leaves a flashing cursor in the cell that's selected.
How do I deselect the cell once the macro is done? I've searched Google for a few methods and tried some, but none quite work properly. I've tried moving the selection left one cell and then right, back to the original cell. But for some reason, the cursor is still flashing in the selected cell. Any help?
The Worksheet_BeforeDoubleClick event has an argument called Cancel. Have a line in your double click event code that sets Cancel = True and that will cancel the cell edit mode and just have the cell selected instead of the blinking cursor within the cell

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!

How to get selected text in VBA

I have a macro that changes the selected text, and I have it assigned to a button.
It works perfectly when i run it directly from visual basic, but when I click the button, the button gets the focus and my text is no longer selected so the macro change the selected element to (button).
How can I select the text and run the macro by clicking on the button and still have the text selected?
The way to do this is to set the set the TakeFocusOnClick property of the CommandButton to False. Here are is the code I use.
Private Sub CommandButton1_Click()
Dim Sel As Selection
Set Sel = Application.Selection
If Sel.Type <> wdSelectionIP Then
MsgBox Sel.Text
End If
End Sub
Is the button embedded in the document? You may need to put it on a form that loads on top of the Word window or in a menu/toolbar, so that clicking it does not affect the selection in the document itself.
Edit:
I think you can use Application.Selection.Previous to get at what you need. You could use this to restore the selection after the click event, or to act upon that section of the document, or both.
I assume that this is available in previous versions of Word, but have only confirmed its presence in 2007.
You need to change TakeFocusOnClick to "False" in the Button Preferences.