Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-05-2013, 05:05 AM
SaneMan SaneMan is offline Automatically add spaces to userform field entry Windows 7 64bit Automatically add spaces to userform field entry Office 2007
Novice
Automatically add spaces to userform field entry
 
Join Date: Jan 2011
Posts: 20
SaneMan is on a distinguished road
Default Automatically add spaces to userform field entry

Hi all

I wondered if anyone could help with a query I have? I have a userform which users will fill in with generic information. One of these fields is to enter a national grid reference (i.e. 'SE 12345 12345').

This NGR should always be entered as two letters (SE) then a space and then either 4 or 5 numbers, then a space and then again 4 or 5 more numbers. The amount of numbers depends on how accurate we need the NGR to be and varies case by case.



I'm trying to add a code in which will automatically convert 'SE1234512345' or 'SE12341234' so that they are correctly spaced. However I have no clue how to do this, or if it's even possible.

Anyone have any ideas?

Thanks in advance!

Antony
Reply With Quote
  #2  
Old 12-05-2013, 08:14 AM
macropod's Avatar
macropod macropod is offline Automatically add spaces to userform field entry Windows 7 32bit Automatically add spaces to userform field entry Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Antony,

It's easy enough to automatically space when there's two letters followed by 8 or 10 digits, but impossible to do reliably when there's 9 digits. Assuming you can't actually have those, you could use code like:
Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim StrIn As String, StrOut As String
  StrIn = Trim(Me.TextBox1.Text)
  If (Len(StrIn) < 10) Or (Len(StrIn) > 12) Or (Len(StrIn) Mod 2 = 1) Then
    MsgBox "Incomplete Data", vbExclamation
    Me.TextBox1.SetFocus
    Exit Sub
  End If
  If Mid(StrIn, 3, 1) = " " And Mid(StrIn, (Len(StrIn) - 2) / 2 + 1, 1) = " " Then Exit Sub
  StrIn = Replace(Me.TextBox1.Text, " ", vbNullString)
  StrOut = Left(StrIn, 2) & " " & Mid(StrIn, 3, (Len(StrIn) - 2) / 2) & _
    " " & Mid(StrIn, (Len(StrIn) - 2) / 2 + 3, (Len(StrIn) - 2) / 2)
  Me.TextBox1.Text = StrOut
End Sub
where TextBox1 is the input TextBox name. Note that, with the above code, there's no need for the user to input the spaces.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-06-2013, 01:08 AM
SaneMan SaneMan is offline Automatically add spaces to userform field entry Windows 7 64bit Automatically add spaces to userform field entry Office 2007
Novice
Automatically add spaces to userform field entry
 
Join Date: Jan 2011
Posts: 20
SaneMan is on a distinguished road
Default

That works perfectly! Thanks very much for your time and the help.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Userform Code not quite right - help please vbanovice Word VBA 1 09-29-2013 09:20 PM
Automatically add spaces to userform field entry Forms - Repeating or Duplicate Field of First Entry? Constitution Word 5 09-04-2013 08:26 PM
Automatically add spaces to userform field entry Form field selection allows entry into form text box David C Word 1 10-24-2012 04:53 AM
Automatically add spaces to userform field entry double spaces in field ref (bookmarks) GLENCOE Word 3 10-25-2011 07:35 AM
Form field to automatically be added to header? razberri Word VBA 3 02-22-2010 03:48 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:39 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft