Solved

variable looses all the zeros in + calc

Posted on 2004-09-01
8
203 Views
Last Modified: 2010-05-02
ok I have one problem...
when I have number 000001 stored to variable, and I add 1 to it
(000001 + 1) I end up with 2, but I wanna have 000002. I know
that I can add those missing zeros with 00000 & 2 but I would like to
know if there is any way to not to loose those zeros in the calculation process.

This would help me a lot in what I have been coding
(my own coordinate system)
0
Comment
Question by:jozne
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 8

Accepted Solution

by:
mladenovicz earned 125 total points
ID: 11950366
Format(number, "000000")
0
 
LVL 10

Expert Comment

by:anv
ID: 11950448
the only way to do so is using string

let var1 stores the value 000001

then do following
dim var1Str as string, lastDigit as integer
dim otherDigits as string

var1str=cstr(var1)
lastDigit = cint(left(var1str,1)) 'will store 1

otherDigits = right(var1str,len(var1str)-1 'will store 00000

lastDigit=lastDigit +1 'will have 2 now

var1 = otherDigits + lastdigit

'now var1 will have 000002


0
 
LVL 19

Expert Comment

by:Shauli
ID: 11951144
Private Sub Command1_Click()
Var1 = "000001"
MsgBox Format(Var1 + 1, "000000")
End Sub
0
 
LVL 4

Expert Comment

by:VIkasumit
ID: 11951153
Hi..
I certainly go with the format string however the correct syntax for me is

newvalue=format (number, "00000#")

VIkasumit
http://sumitonnet.20m.com

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 10

Expert Comment

by:anv
ID: 11951296
Shauli's code will work but if u store the data in an integer variable the value will again go as  2 removing leading 0's

yes for storing as a string it will work...
like

Private Sub Command1_Click()
dim str1 as string
Var1 = "000001"
str1 =  Format(Var1 + 1, "00000#")
End Sub
0
 
LVL 4

Expert Comment

by:VIkasumit
ID: 11952739
Hi..

Yes He is right but Again when ever you need You can use Format Like while displaying, Storing or even manupulating. And If you just want to use it as a ID field than better use the string datatype once generated u doesn't need to change it....

VIkasumit
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11952756
Var1 can be stored as String or as Varinat, as in:

Private Sub Command1_Click()
Dim Var1 As Variant, Var2 As Variant
Var1 = "000001"
Var2 = Format(Var1 + 1, "000000")
MsgBox Var2
End Sub

Either way will work. And there is no different if you go 00000# or 000000. The result is the same.

S
0
 

Author Comment

by:jozne
ID: 12003813
Thank you guys for your answers. My coordination system is up and running :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

758 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

17 Experts available now in Live!

Get 1:1 Help Now