How to ensure correct text formatting in VBA?

Johnagall

New Member
Joined
Jul 17, 2011
Messages
6
Hi There,

My first post on Mr Excel and I'm very very new to VBA so please be gentle :)

I would like to know how to use VBA to ensure that data is entered in the correct format in a user form.

For example I would like to make sure that names are entered in title case as opposed to block capitals or small text. Is there a way that I can do this?

To explain a little better I would like ensure that if a name was entered into a userform like this: "JOHN smith"; that it actually is entered into the spreadsheet like this: "John Smith".

Likewise I would like to ensure that dates are converted into my chosen format of 27th July 2011 as opposed to any other format.

Can this be done? Any advice would be greatly appreciated.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
You can tell users until you are blue in the face about how you want them to enter data and they will enter it every way imaginable other than the way you tell them.

Regarding your first question, if the name is entered into TextBox1 and you want it to be in Proper case in cell A1, then this is one way to accomplish that, presumably with the Click event of some Command Button:

Code:
Range("A1").Value = WorksheetFunction.Proper(TextBox1.Value)



As to the second question, along those lines, send the date entered from (I am assuming again) TextBox2 to cell A2, and format it as you want in the code, like this for example:

Code:
With Range("A2")
.Value = TextBox2.Value
.NumberFormat = "DD MMM YYYY"
End With
 
Upvote 0
Thanks for such a quick response.

Totally agree about trying to tell people how to enter text!

So the 'proper' command tells excel that the text should be in title case then?

If so is there another command for BLOCK Capitals?

Thanks again
 
Upvote 0
Just to follow up on Tom's posting and your additional question. Instead of calling out to the worksheet for its PROPER function, you can use VB's built-in function...

Code:
Range("A1").Value = StrConv(TextBox1.Value, vbProperCase)
If you want upper case lettering, you can use VB's UCase function...

Code:
Range("A1").Value = UCase(TextBox1.Value)
As for the number format you requested, I noticed you asked for an ordinal suffix on the day number. Try using this NumberFormat assignment instead of the one Tome suggested...

Code:
DayNumber = Day(TextBox1.Value)
.NumberFormat = "DD""" &  Mid$("thstndrdthththththth", 1 - 2 * ((DayNumber) Mod 10) * (Abs((DayNumber) Mod 100 - 12) > 1), 2) & """ MMMM YYYY"
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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