Excel VBA: Deselect a cell - vba

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

Related

excel checkboxes to hide and unhide rows disappear when i save and reopen file

I have 2 sheets in excel where if you tick the checkboxes on the first sheet it hides or unhides the rows on sheet2. I used active x controls and set checkbox properties to 'move and size with cells' but when i unhide the rows, the checkboxes disappear but are still in the document as their height changes and remains at 0.(the rows appear just fine). Please help!!
ActiveX checkboxes are reknown for problems like these. FOrms checkboxes are a bit better, but I would advise to just use cells with data validation set to list and have it accept 0/1 or True/False. It is relatively easy to set up an event macro that toggles the value of those cells after a double-click for instance.

excel: use keyboard to move column in data validation mode

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.

Programmatically add a button with a macro to an Excel file

I'm creating formated worksheets with an Excel VBA application and I would like to add programmatically add a button to each worksheet when created. This button must have a macro attached to it, which display the average of the values in a certain range in a special cell, and change the background color of this cell depending on the value of the average. Is it possible to this, and if yes, how can I do it ?
You can add the Button like this or example:
ActiveSheet.Buttons.Add(52.5, 7.5, 173.25, 41.25).Select 'Arguments are coordinates
Selection.OnAction = "Button_Click"
Now make sure the Button_Click sub has the logic you want.

Make Excel select cell when clicked if previously selected cell had validation dropdown list displayed

If the user is on a cell in Excel, and that cell has a data validation dropdown list, and the user has the list displayed:
If the user then clicks on another cell, that other cell is not selected. Instead, all that happens is that the first cell's data validation dropdown list is dismissed. The first cell is still selected, until the user clicks on the second cell again.
Is there any way in VBA to detect when that happens, and select the second cell so the user doesn't have to click a second time?
I'm on Excel 2007.
You could get the behavior you want by using a ComboBox fit into the cell and use that instead of the built-in data validation. You just lock the cells and have a ComboBox appear in the cells in that column that they select. If they choose another cell the combobox simply goes invisible. This also solves the problem of using a custom font (if you have a font with special symbols, for example.)

set cell focus in data grid view .NET

In my datagridview (Mydgv1),I want to set fourth cell into focus and edit it, after i leave the first cell.
On first cell's leave event , i have written the code to focus the 4th cell, it comes into focus, but there is no cursor in it, and then the focus shifts to second cell and second cell becomes blue(by default highlighted cell in datagridviews). Please post some code for it. So far I have tried this.
Mydgv1.ClearSelection()
Mydgv1.CurrentRow.Cells(3).Selected = True
Mydgv1.BeginEdit(False)
BeginEdit will only have effect on the current cell (marked by the CurrentCell property of the DataGridView object). Selecting it won't help (especially since you can select multiple cells in some DataGridView configurations). Instead try this:
Dim ColumnIndex As Integer = 3
Mydgv1.CurrentCell = Mydgv1.CurrentRow.Cells(ColumnIndex)
Mydgv1.BeginEdit(False)