Solved

z = x + y – 1

Posted on 2016-08-06
6
97 Views
Last Modified: 2016-08-06
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

0
Comment
Question by:AaronBanker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 20

Expert Comment

by:Roy Cox
ID: 41745494
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

0
 
LVL 20

Accepted Solution

by:
Roy Cox earned 500 total points
ID: 41745502
Here's a tip, don't use error handlers until your code is fully tested because you are switching off helpful messages
0
 
LVL 5

Expert Comment

by:rberke
ID: 41745527
Your minus sign is not really a minus sign. Overkey and the error goes away
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 5

Expert Comment

by:rberke
ID: 41745546
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
0
 

Author Closing Comment

by:AaronBanker
ID: 41745583
Thanks for the help
0
 
LVL 20

Expert Comment

by:Roy Cox
ID: 41745602
Pleased to help
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question