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

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
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,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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