Can a textbox be added to the Excel Menu area?

Hatcheda

Active Member
Joined
Dec 8, 2005
Messages
354
The following code is a brief code that I just wrote to help with selecting a large area quickly.

Is there a way to add a text box or input box to the excel tool bar similar to the formula bar that can be referred to in a way similar to the code below with the input box?

Ex.

Suppose I have a new input box on my toolbar (Mentioned above, if possible to create) and I type A1, I then press a command button that looks to that input box and runs a specific change to the location.
(Such as select) Another may color, another may cut, and so on . . .

Possible?
Code:
Sub MacroSelectionTest()
Dim MyRange As String
MyRange = InputBox("Type the Range you Wish to Select:", "Range Selection")
Range(MyRange).Select
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Yes it is possible, but why don't you keep it simple and either

Use the name box just to the left of the formula bar where you can enter a cell address...

or...

As long as you are planning to employ a command button, why not just have an input box appear first when that command button is clicked, asking the user to specify the range to be dealt with.
 
Upvote 0
Tom: Thanks for the reply! :-)
I can't seem to get the box to the left to work with a full range (A1:B30)

I am looking for something like "F5" that is always open . . .

So you said that this can be done?
 
Upvote 0
It's a lot easier for people to select a range using the mouse and/or keyboard than typing the address as text.

What you should do is let people select the range on their own and then have your code work on the 'Selection' as in the untested code below that enters the numbers 1, 2, ... into the selected cells
Code:
dim i as integer, aCell as range
i=1
for each acell in selection
    acell.value=i
    i=i+1
    next acell
The following code is a brief code that I just wrote to help with selecting a large area quickly.

Is there a way to add a text box or input box to the excel tool bar similar to the formula bar that can be referred to in a way similar to the code below with the input box?

Ex.

Suppose I have a new input box on my toolbar (Mentioned above, if possible to create) and I type A1, I then press a command button that looks to that input box and runs a specific change to the location.
(Such as select) Another may color, another may cut, and so on . . .

Possible?
Code:
Sub MacroSelectionTest()
Dim MyRange As String
MyRange = InputBox("Type the Range you Wish to Select:", "Range Selection")
Range(MyRange).Select
End Sub
 
Upvote 0
Tusharm - Thank you for your reply and I do agree with you.

But this is the problem I keep bumping my head with.
On a new unpopulated workbook, selecting a high range takes a lot of time waiting on it to scroll. I could more easly type A1:C40000
 
Upvote 0
You don't have to scroll. Have the user select the first cell (the one that represents the upper left corner of the range), then enter the last cell in the name box (right bottom corner), press the Shift key, and press the Enter key. Then, you can have the caption on your button:
"Select range and click here" and assign a string variable to Selection.Address.


If you really have your heart set on using an textbox on the menu bar, then in a test workbook, follow these steps:

Step 1
Close all workbooks in Excel except the test workbook you are working with.

Step 2
Find the little Excel workbook icon near the upper left corner of your workbook window, usually just to the left of the File menu option. Right click on that icon, left click on View Code, and paste the following procedure into the large white area that is the workbook module.

Code:
Private Sub Workbook_Open()
Run "ResetMenu"
Run "MakeTextBox"
Dim TymeOfDay$
If Time < 0.5 Then
TymeOfDay = "Good Morning !!" & vbCrLf & vbCrLf
ElseIf Time >= 0.5 And Time < 0.75 Then
TymeOfDay = "Good Afternoon !!" & vbCrLf & vbCrLf
Else
TymeOfDay = "Good Evening !!" & vbCrLf & vbCrLf
End If
MsgBox _
TymeOfDay & _
"A text box has been placed on the menu bar." & vbCrLf & vbCrLf & _
"Use that text box to enter a cell address or range," & vbCrLf & _
"and confirm the entry by pressing the Enter key.", 64, "Tip:"
End Sub

Private Sub Workbook_Activate()
Run "ResetMenu"
Run "MakeTextBox"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Run "ResetMenu"
ThisWorkbook.Save
End Sub

Private Sub Workbook_Deactivate()
Run "ResetMenu"
End Sub


Step 3
While you are in the Visual Basic Editor, click Insert > Menu and paste this in:

Code:
Private Sub ResetMenu()
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("Custom Text Box").Delete
Err.Clear
End Sub

Private Sub MakeTextBox()
With Application
.ScreenUpdating = False
Run "ResetMenu"

Dim objTextBox As Object
With .CommandBars("Worksheet Menu Bar")
Set objTextBox = .Controls.Add(Type:=msoControlEdit, before:=.Controls.Count)
End With

With objTextBox
.Caption = "Custom Text Box"
.Width = 100
End With

.ScreenUpdating = True
End With
End Sub

Sub CustomtextBoxtext()
Dim tbVal$
tbVal = Application.CommandBars("Worksheet Menu Bar").Controls("Custom Text Box").Text

If Len(tbVal) = 0 Then
MsgBox "You did not enter anything in the text box.", 48, "Nothing to read !!"
Exit Sub
End If

On Error Resume Next


'Do whatever you want to that range
Range(tbVal).Select


If Err.Number <> 0 Then
MsgBox "You entered an invalid range reference in the menu bar text box." & vbCrLf & _
"''" & tbVal & "'' is not a valid range." & vbCrLf & vbCrLf & _
"Please enter a bona fide cell address or range.", 16, "No such worksheet range !!"
Exit Sub
Else

End If
End Sub


Step 4
Save the workbook.

Step 5
Close the workbook.

Step 6
Re-open the test workbook with the code you just installed, and notice:
- A message box greets the user with awareness of the textbox and how to enter a range in it (must hit the Enter key to confirm entry).
- Validation controls are in place to alert the user of they entered an invalid range or no text at all.
- You need to assign the CustomtextBoxtext macro to the Forms button on your sheet.
- Replace this code line
Range(tbVal).Select
with whatever action you want to take on that range, I only used Select for demo purposes.



Again, in my opinion, this is not an intuitive approach, which is why I added instructions in the message box, and validation alerts when the text is not entered with a recognizable range, but there it is 'cause you asked.

All the above steps worked for me, but it seems a lot easier to just enter with the keyboard, or select with the mouse (with or without the name box's help) the range you want to work with in an input box, than all this code for a textbox that will be visible all the time while the workbook is active, whose use is limited to this one task.
 
Upvote 0
Tom - Your post will be very useful for many many other projects to come! Thank you! Honestly, you cought me with the first part of you answer.

I didn't realize
You don't have to scroll. Have the user select the first cell (the one that represents the upper left corner of the range), then enter the last cell in the name box (right bottom corner), press the Shift key, and press the Enter key. Then, you can have the caption on your button:
"Select range and click here" and assign a string variable to Selection.Address.
Exsisted! Thanks alot! I will be using the text box for other things, like quick VBA procedures for myself! :-)
 
Upvote 0
Thanks for the follow-up message. Now at least you have several options to choose from, and it sounds like you learned a couple tricks in the process, so it's a good outcome all the way around.
 
Upvote 0

Forum statistics

Threads
1,225,117
Messages
6,182,933
Members
453,140
Latest member
SAbboushi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top