OnKey "Delete" clear combobox - excel

I have a userform with multiple comboboxes. User can type in a new item or pick one from the list.
He can start typing first letters of wanted item but when he makes a mistake and starts with e.g. "b" instead of "n" he has to clear the combobox manually or find the item on the list using mouse or arrows.
I would like to quickly clear the box using the delete key so the user can start typing again. But I don't know where to put this line of code if correct (enter event, change event, some other maybe?).
Application.OnKey "{Delete}", "WyczyscPole"
First Excel needs to know which box the user is in, and then clear it.
I tried to create separate module with a variable that finds out the current combobox name and clear it in next step. But I don't know whether called sub below is even correct.
Sub WyczyscPole()
Dim NazwaPola As ComboBox
NazwaPola = frmZakupy.ActiveControl.Name
NazwaPola.Clear
End Sub

You can use the KeyDown event to capture the Delete key (KeyCode = 46) when the user is in the combobox.
Private Sub NazwaPola_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 46 Then
Me.NazwaPola.Value = vbNullString
End If
End Sub

Please try: frmZakupy.value=''
It's a good practice to don't use special characters like 'ś','ć' in any names.

Related

How to prevent Userform Combobox show the drop down list if the item is only one?

I have an array which contains customer's names as arrNama variable
I use a code to filter as I type which I've found in the internet (I am sorry I don't remember the link), something like this :
Sub cbNama_Populate(Optional fltr As String)
cbNama.List = Filter(arrNama, fltr, , vbTextCompare)
End Sub
Private Sub cbNama_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Call cbNama_Populate(cbNama.Text)
If cbNama.ListCount = 1 Then cbNama.ListIndex = 0 Else cbNama.DropDown
End Sub
The If cbNama.ListCount = 1 Then cbNama.ListIndex = 0 Else cbNama.DropDown line is my modification, the original I found in the internet is just cbNama.DropDown.
I modified that line, because I thought with that IF conditon, it won't show the drop-down list if the list count of the combo box is only one. Yet it still show the drop-down list with that one item only, just like as if in the original code which has no IF condition.
Below is the example of what I mean :
At the time the combobox has only one item, the cbNama.ListIndex = 0 trigger the cbNama_Click sub. But the combobox still show the drop-down list, highlighted with blue on that one item.
My question:
How to prevent it showing the drop-down list when there is only one item?
Any kind of response would be greatly appreciated.
Thank you in advanced.
For the time being, I use cbNama.Enabled = False, this prevent the dropdown shows if there is one item only.

How do you make the value in a control box update live in VBA?

I have a bare minimum of VBA experience, and I have to edit a tool someone else made a while ago. The values in the textbox for the form control will not update in real time. There are multiple textboxes (I think form control, not sure how to check.) where the user inputs values. However, the box does not update the value inside until a cell is selected.
I don't see anything in the code for this that would affect this. Here's an example from one of the Textboxes.
Private Sub SysA_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
With Sheets("SysA")
If KeyCode = 13 Then
If Len(.SysA.Value) = 5 Then
.SysANum.Activate
Else
MsgBox "Enter in a valid number!"
.SysANum.Value = ""
.SysANum.Activate
End If
End If
End With
End Sub
I've also looked in the properties for the textbox and don't see anything that might affect it. (AutoLoad is set to False and Enabled is set to True)

Editing percentage value in Userform TextBox

I am attempting to create a textbox which allows users to input a percentage between 1-5% in my Excel-VBA Userform. I have created the textbox and added code to restrict the formatting to always show as a percentage. My problem is that when I attempt to edit the default percentage in the textbox by pressing backspace the decimal moves two spaces to the right instead of deleting any present values. Below is a copy of my code. Please note that the Textbox is not locked.
Private Sub PV_change()
PV.Text = Format(PV.Value, "Percent")
End Sub
You could do your code in the PV_Exit procedure rather than the _Change procedure, but I still think you're asking for trouble this way :)
Private Sub PV_Exit(ByVal Cancel As MSForms.ReturnBoolean)
PV.Value = Format(PV.Value, "Percent")
End Sub
I agree with the objections made previously, but to show a simple solution without error trapping try this:
Private Sub tbPV_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim dblPV As Double
' don't allow backspace
If KeyCode = Asc(vbBack) Then Exit Sub
' Change string to Double value (without percent sign)
dblPV = CDbl(Replace(Me.tbPV.Text, "%", ""))
' Show the formatted result
Me.tbPV.Text = Format(dblPV, "0.00") & "%"
End Sub
Further Remark Naming a text box 'PV' only doesn' t seem to be state of the art. Adding a prefix - e.g. 'tb' or 'txt'- to all your text boxes gives a hint to to control's original type and could give your code a comprehensible structure.
There are number of naming conventions and divergent opinions, but just to give you an impression have a look at
Hungarian in VBA okay? or search within the Stack Overflow's "naming-conventions" tag.
Best Regards

Lock Select Box Size In Excel

This seems like a simple thing, but I cannot figure it out, or find it online.
If I select 5 cells in a column(say A1:A5), and I would like to move this selection shape(column 1:5) over (to B1:B5); Is there shortcut to do this? Currently I hit the left arrow, and the select box changes size to just B1, and I have to hit shift and select B2:B5. Ideally I would like to discover a hot key that "locks" the shape of the select box.
It has been suggested by colleagues to write a macro, but this is ineffective in many cases. For example what if instead of a column I wanted to do the same thing with a row, or with a different sized shape. It seems likely that excel has this feature built in.
I'm not sure how a macro would be ineffective. I would write procedures similar to what's below, then assign them to hotkeys. Let me know if it works for you.
Option Explicit
Public rowDim As Long
Public colDim As Long
Public putSel as Boolean
Sub togglePutSel()
putSel = Not putSel
End Sub
Sub GetSelShape()
rowDim = Selection.Rows.Count
colDim = Selection.Columns.Count
putSel = True
End Sub
Sub PutSelShape()
Selection.Resize(rowDim, colDim).Select
End Sub
If you want to make it work for whenever you hit the arrow keys, then in your Sheet code, you can use this. You may want to do a quick check that rowDim and colDim aren't 0. The only issue with this is that you'd be stuck with that behavior unless you create a trigger to stop calling PutSelShape. So, I'd suggest one macro (hotkeyed to GetSelShape) to toggle it, and another hotkey for togglePutSel.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If putSel Then
Call PutSelShape
End If
End Sub

Disable button on a userForm

I'm trying to figure out how to disable a button within my userForm if a certain cell within my spreadsheet equals a certain number. I tried the code stated below, but it isn't working.
Private Sub UserForm_Initialize()
Label2 = Sheets("DATA").Range("AM2").Value
Label4 = Sheets("DATA").Range("AO2").Value
Label7 = Format(Sheets("DATA").Range("R8").Value, "Currency")
If Sheets("DATA").Range("AL10").Value = 10 Then
ActiveSheet.Shapes("CommandButton1").Select
UserFormact_Upgrade.CommandButton1.Enabled = False
Else
End If
End Sub
Your code should be working, as you're on the right path.
To test it, simply create a new form and add this code, you'll see it should work. Maybe you're having problems within the IF clause?
Besides, you don't need to select the shape prior to disabling it; just disable it right away.
Private Sub UserForm_Initialize()
CommandButton1.Enabled = False
End Sub
I know this is old, but got to this thread trying to solve my problem, and found a solution that wasn't mentioned here. So in case someone gets here like I did, and this didn't quite get them where they needed to go, I thought this might help.
I had a userform with a drop down box called cmdADAMFields, and I didn't want my submit button called FieldsSubmitButton to be enabled until I selected something from the dropdown box.
I had to break up my argument into two different private subs vs one larger If-Then-Else statement.
First, I put:
Private Sub UserForm_Activate()
If cmbADAMFields.ListIndex = -1 Then FieldsSubmitButton.Enabled = False
End Sub
Then when for my pulldown's private sub when it's value changed I wrote:
Private Sub cmbADAMFields_Change()
FieldsSubmitButton.Enabled = True
End Sub
The proper place for setting Enabled property is in Activate event (associated with Show method) and not Initialize event (associated with Load instruction).
The below code disable the button CommandButton1 when AL10 cell >= 10.
Private Sub UserForm_Activate()
CommandButton1.Enabled = ( Sheets("DATA").Range("AL10") < 10 )
End Sub
For buttons you can choose between normal buttons (property Enabled=False and property Visible=true), disabled buttons (property Enabled=False and property Visible=true) and invisible buttons (property Enabled=False and property Visible=False), that it is a cleaner interface, in most cases.
Concerning text boxes, besides normal, disabled and invisible status, there is a locked status, that is enabled and visible, but cannot be user edited. (property Locked = True)
A locked control only can be changed by VBA code. For instance, someone can includes date text boxes, that it's filled using a secondary popup date form with Calendar control.

Resources