Input Box - don't allow spaces

Shiremaid

Board Regular
Joined
Jun 18, 2013
Messages
73
I have a macro to allow users to add a country to a project. But it causes errors if they include spaces in the country name. I have instructed them not to include spaces, but of course someone always ignores that. Is there a way I can make the input box not accept spaces in the entry?

Code:
Sub AddCountry

Sheets("SiteTemplate").Visible = True
Sheets("SiteTemplate").Copy Before:=Sheets("SiteTemplate")
Range("A1") = InputBox("Country Name (no spaces)")
ActiveSheet.Name = Range("A1").Value

End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
12,670
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
Try:
Code:
Sub AddCountry()
    Dim response As String
    Sheets("SiteTemplate").Visible = True
    response = InputBox("Country Name (no spaces)")
    If InStr(1, response, " ") > 0 Then
        MsgBox ("Do not include spaces in the country name.  Please re-enter the country name without spaces.")
        Exit Sub
    Else
        Sheets("SiteTemplate").Copy Before:=Sheets("SiteTemplate")
        ActiveSheet.Range("A1") = response
    End If
    ActiveSheet.Name = Range("A1").Value
End Sub
 
Upvote 0

Forum statistics

Threads
1,190,916
Messages
5,983,569
Members
439,850
Latest member
suhailrocks786

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
Top