Limiting number of character in InputBox to 31 - VBA Help

DJMXM

New Member
Joined
Jun 19, 2013
Messages
45
I need to limit the number of Characters that can be inputted to the popup InputBox to just 31. How do I set MaxLength? Code I am using is listed below.

Code:
Sub NewRecipeSheet()
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Worksheets("1. Recipe Master Sheet")
ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
Application.Goto Reference:=Sheets("1. Recipe Master Sheet").Range("A1")
Range("A1").Value = InputBox("Menu Item Name?")
    
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
There is no way to force the InputBox to limit the typing to a maximum amount of characters (31 in your case). The best you can do is create a loop and don't exit it until the user enters less than that maximum. Something like this...

Code:
Dim Answer As String
....
....
Do
  Answer = InputBox("Menu Item Name?")
  If Len(Answer) > 31 Then MsgBox "You typed in too many characters... 31 maximum!"
Loop While Len(Answer) > 31
Range("A1").Value = Answer
....
....
 
Upvote 0
It works Ok... It seems I have to answer the question twice before it excepts even if it's less than 31 characters

Mike
 
Upvote 0
Here is the Code after I put it into my Macro

Code:
Sub NewRecipeSheet()
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Worksheets("1. Recipe Master Sheet")
ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
Range("A1").Value = InputBox("Menu Item Name?")

Dim Answer As String
    Do
        Answer = InputBox("Menu Item Name?")
    If Len(Answer) > 31 Then MsgBox "You typed in too many characters... 31 maximum!"
    Loop While Len(Answer) > 31
Range("A1").Value = Answer

Application.Goto Reference:=Sheets("1. Recipe Master Sheet (2)").Range("A4")
End Sub
 
Upvote 0
Here is the Code after I put it into my Macro

Code:
Sub NewRecipeSheet()
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Worksheets("1. Recipe Master Sheet")
ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
[COLOR=#FF0000][B]Range("A1").Value = InputBox("Menu Item Name?")[/B][/COLOR]

Dim Answer As String
    Do
        Answer = InputBox("Menu Item Name?")
    If Len(Answer) > 31 Then MsgBox "You typed in too many characters... 31 maximum!"
    Loop While Len(Answer) > 31
Range("A1").Value = Answer

Application.Goto Reference:=Sheets("1. Recipe Master Sheet (2)").Range("A4")
End Sub

Remove the line of code I highlighted in red... it is the source of your needing to answer the question twice (if you look at the code I posted, you will see it was not included there).
 
Upvote 0
Much Better - Thank You VERY MUCH.... I am teaching myself VBA as fast as I can but still have a VERY LONG WAY to go!!
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,244
Members
448,879
Latest member
VanGirl

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