Method to tab between Text Boxes in excel? - vba

Does anyone know of a way to press the 'Tab' key and move between text boxes in Excel (2010)? The VBA solution I've seen is way too manual, as you need to create code for the 'Tab' event on each text box.
For example, the following would need to be replicated for every TextBox in the workbook:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyTab Then TextBox2.Activate
End Sub
(The text boxes are NOT part of a VBA form. They are part of a regular worksheet.)
Thank you,
Don

First Go To Special, select Objects and OK.

Related

How to delay Userform TextBox1_Change() triggering until entering multiple characters singly is complete?

I have a macro which uses entered value from TextBox1. If a value is given to TextBox1 as a whole, the code runs fine. If a value is entered character by character (scanned with a handheld barcode scanner) the code: TextBox1_Change() triggers on every character.
How do I make the macro start only when the whole value is entered (the length of the value may differ)? Maybe there is a way to catch the barcode scanners ENTER?
Extra buttons to manually trigger the code are not an option.
Also asked here
There are a few different event triggers for a text box. TextBox1_Exit would be the one you're looking for. It triggers when the user clicks away from the text box or closes the userform.
Change the name of the Sub to TextBox1_Exit() and see if the behavior is what you're looking for.
If that isn't it then try Application.OnTime. You can delay the macro execution by a second giving the barcode scanner plenty of time to finish inputting the text.
Application.OnTime Now() + TimeValue("00:00:01"), "MacroName"
Solved with this:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
End If
End Sub

Excel Userform Textbox Constant Set Focus

First of all I would like to thank all of you guys. Maybe you did not notice but you help me to grasp VBA to some level from scratch. I am still in learning process so I may be missing something really simple, please be gentle :)
First of all I would like to give a small backgroud update about my issue. I have been writing a small program to scan incoming parts to my work to be able to keep inventory status. Latest look of the program is like below:
And numbers on the picture are my nightmares lately:
1. Scanned Part Number: This is the textbox where scanner inputs the value. After I receive the input I immidietly convert that data to a variable and clear the textbox value as below:
Private Sub PN_CurrentScan_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then
EnteredPN = Replace(PN_CurrentScan.Value, Chr(32), "", 1) '<---PN_CurrentScan is the name of text box
EnteredPN = Left(EnteredPN, 12)
PN_CurrentScan.Value = ""
After making some corrections on the scanned data I basically write it to a sheet in the workbook. Then I also have a pivot table in the same workbook which uses this scanned data as source and counts how many parts scanned from each part number.
2. Current Status: This ListBox contains all the part numbers scanned (Coming from the pivot table mentioned above) and waiting to be scanned (Coming from another worksheet). Then it refreshes it self every time a new part is scanned.
3. ListBox Scroll Bar: Since I have very long part number list it is not possible for me to fit everything on the screen that is why listbox creates this scroll bar.
Enough with the background I think :)
So if we come to my concern. Since my collages using cordless scanner to do this operation sometimes they don't have the chance to see the screen so they can not understand if the cursor is on the "Scanned Part Number Text Box" or not. That is why I need focus to be on that box no matter what happens (Of course we can not do anything if warehouse burns down, earth quake or tsunami hits the place but let do not think about those).
WHAT I HAVE TRIED:
First of all I disabled all the remaining objects from properties window
Then I diabled tab stops of all controls:
Dim contr As Control
For Each contr In ScannerInterface.Controls
On Error Resume Next
contr.TabStop = False
Next
ScannerInterface.PN_CurrentScan.TabStop = True
Added setfocus property to all button clicks:
Me.PN_CurrentScan.SetFocus
Added setfocus property to listbox click:
Private Sub CurrentStatus_List_Click()
Me.PN_CurrentScan.SetFocus
End Sub
Added set focus to enter and exit events of listbox however this did not work:
Private Sub CurrentStatus_List_Enter()
Me.PN_CurrentScan.SetFocus
End Sub
Private Sub CurrentStatus_List_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Me.PN_CurrentScan.SetFocus
End Sub
So, with all these counter measures I have managed to improve up to somepoint, only concern remaining is when I click on the scroll bar next to the listbox, text box lose focus and without clicking in the textbox I could not manage to set the focus again. I tried all events with listbox non of them worked. Is there any way to solve this problem or do I need to deal with this? Thanks in advance for your supports.
SOLUTION:
Thanks to #Rory we have managed to solve my problem. As he noticed and explained in the answer below, both my textbox and listbox were in frames. I have tried several setfocus options but I always gave the focus to the textbox. However, solution was to give the focus to the frame which was containing the target textbox:
Private Sub CurrentStatus_Frame_Enter() '<-- Enter event of the frame which contains listbox
Me.PN_CurrentScan.SetFocus '<-- Setfocus to target textbox
Me.Scanned_Frame.SetFocus '<-- Setfocus to frame which contains target textbox
End Sub
Having (eventually) noticed that both of your controls are inside container Frame controls, you can actually use the Enter event of the Frame that contains the listbox to set focus to the Frame that contains the textbox, rather than to the textbox itself.

How to be able to select the text but not the textbox

This seems like it would be pretty simple but I can't figure out a way to do it and there isn't a solution anywhere on the internet that I can find leading me to believe it isn't possible using this language.
I have a simple program, you give it input, hit a button and it outputs into a textbox.
I had to make it a textbox and not a label because you can't highlight the text in a label, which is necessary for the user to be able to copy the text. However, as a textbox, the user can select the box. I've set it to ReadOnly, so they can't type in it, but the blinking text cursor is still there if the box is clicked on and it looks really bad. I've also tried setting Enabled to False, but then the text can't be highlighted.
The textbox itself shouldn't be able to be selected, just the text inside the textbox.
How do I do such a thing?
P/Invoke is the only method I know of.
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function HideCaret(hWnd As IntPtr) As Boolean
End Function
Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
HideCaret(TextBox1.Handle)
End Sub
Here is documentation on the user32.dll HideCaret function:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms648403(v=vs.85).aspx

How can I enable highlight when mouse over into VBA?

how can I enable highlight when mouse over into VBA? I write a marco into VBA, that after creating a new shape the shape should be enabled highlight when mouse over. Maybe with color?
Thank you
argonist
Your macro would need to run on a specific event. So in Powerpoint you have options such as:
MouseDown
MouseMove
MouseUp
You can hit the F1 key to tell you exactly which event does what.
In VBA there is no event called 'MouseOver' so you need to improvise such as using a 'MouseMove' event to change the colour when you hover over it, and then maybe the pages 'MouseMove' event to change it back to the default colour when you move the mouse onto something else.
You should be able to find all of these events in the drop down list for the object in the Visual Basic window
Another option rather than using VBA is to play around with powerpoints animations and effects which may get you the same result.
I found the simply way.
myshape.ActionSettings(ppMouseOver).AnimateAction = msoTrue
But I cannot change the color and the line size. That is enough for me. Thank you.
Try this one on ActiveX picture attach in slide (code run in show mode)
'zmien_w_trybie_prezentacji - VBATools.pl
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call kolor2(Image1)
End Sub
Sub kolor2(osh As Image)
If osh.BackColor = 255 Then _
osh.BackColor = 13998939 Else _
osh.BackColor = 255
End Sub

VB.NET How to insert text at cursor position in a different window?

I have a small application that displays a listbox under the cursor position when the user uses a shortcut key.
When the user double clicks a selection from the listbox I want to insert that selected text at the curser position of that opened window.
Example: user has microsoft word open. He/she uses a shortcut key that displays a listbox just under the cursor position. The listbox has a collection of text. When the user double clicks a selection that selected text is inserted at the cursor position.
I tried the following:
Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Text.Insert(Cursor.Position, ListBox1.SelectedItem)
End Sub
But that doesn't work.
Any help will be sincerely appreciated.
The best (most generic) approaches will be to trick the application into thinking you have entered some text. For example:
Send keypress windows messages for all of the characters you wish to "type" to the target window (e.g. with WM_KEYDOWN or WM_CHAR type messages. Some experimentaiton may be needed to find the approach that works best).
Copy the text onto the clipboard and send a single ctrl+V keypress message to the application. (This will overrite the clipboard and may not work in apps that don't support that key shortcut though)
If you know the specfic application (e.g. MS Word) then you may be able to use application-specific automation (OLE, etc) interfaces to insert text.