Solved

z = x + y – 1

Posted on 2016-08-06
6
54 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
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:Roy_Cox
Comment Utility
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 17

Accepted Solution

by:
Roy_Cox earned 500 total points
Comment Utility
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
Comment Utility
Your minus sign is not really a minus sign. Overkey and the error goes away
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 5

Expert Comment

by:rberke
Comment Utility
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
Comment Utility
Thanks for the help
0
 
LVL 17

Expert Comment

by:Roy_Cox
Comment Utility
Pleased to help
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now