Avatar of AaronBanker
AaronBanker
 asked on

z = x + y – 1

Why do I have a Syntax Error for "z = x + y – 1"


Private Sub cmdAddSheets_Click()
'dim variables
Dim x As Integer
Dim y As Integer
Dim z As Integer
'set error handler
On Error GoTo Copier_Error
'turn off screen updating and calculation
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'check if the workbook isshared and exit if it is
If ThisWorkbook.MultiUserEditing Then
MsgBox "Please Unshare the workbook before you proceed"
Exit Sub
End If
'check that all values are in the userform
If Me.txtName = "" Or Me.cboNum = "" Or Me.cboNumSt = "" Then
MsgBox "You forgot to add the parematers for the sheet name."
Exit Sub
End If
'protect_all
Protect_All
'set variables for number of sheet
x = Me.cboNum.Value
'set variable for sheet name prefix
y = Me.cboNumSt.Value
z = x + y – 1
'check the first sheet name
NewName = Sheet1.Range("A4")
'loop through to check
For Each sh In ThisWorkbook.Worksheets
If sh.Name = NewName Then
MsgBox "This information already exists.You are not permitted to copy over it"
'if sheet name exists then got to end of sub
GoTo Copier_Error:
End If
Next
'run loop to add sheets
For numtimes = y To z
Sheet3.Copy after:=Worksheets(Worksheets.Count)
'create the new sheet name
ActiveSheet.Name = Me.txtName & numtimes
Next
'return to Interface sheet
Sheet1.Select
'list workshhet names on the interface
ListWorkSheetNames
'turn of screen updating and calculation
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
'error handler
On Error GoTo 0
Exit Sub
Copier_Error:
'message if sheet already exists
MsgBox "I think " & NewName & " already exists.  Check the starting number"
End Sub

Open in new window

Microsoft ExcelVisual Basic Classic

Avatar of undefined
Last Comment
Roy Cox

8/22/2022 - Mon
Roy Cox

Use Val to convert to numeric, by default your code in a userform is using Strings because TextBox value, ComboBoxe values etc default to Strings

'set variables for number of sheet
x = Val(Me.cboNum.Value)
'set variable for sheet name prefix
y = Val(Me.cboNum

Open in new window

ASKER CERTIFIED SOLUTION
Roy Cox

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Robert Berke

Your minus sign is not really a minus sign. Overkey and the error goes away
Robert Berke

cut and paste it into word, highlight it then use alt x to see its unicode value.
a normal minus is  \u002d but your minus is \u2013.

Instead of pasting t word,, you can paste the character into the top box at is https://www.branah.com/unicode-converter.  That is a nice website for converting unicode text to unicode \u notation and the decimal equivalent.

By the way \u2013 is an En dash which is used to signify a range. for instance januayr endash december.  Or atlantic endash pacific
Your help has saved me hundreds of hours of internet surfing.
fblack61
AaronBanker

ASKER
Thanks for the help
Roy Cox

Pleased to help