Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 8th, 2012, 02:10 PM
Authorized User
Points: 43, Level: 1
Points: 43, Level: 1 Points: 43, Level: 1 Points: 43, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Int to Char and Char to Int Function

I am trying to convert an int to a three character string. I need to convert 1 into "001."

The built in INT() and CHAR() functions do not work.

I have created a workaround by using switch statements which works in the short term but is not an elegant solution.

Function Int_to_Char(temp As Integer) As String
    Select Case temp
    Case 1
        Int_to_Char = "001"
    Case 2
        Int_to_Char = "002"
    Case 3
        Int_to_Char = "003"

Function Char_to_Int(temp As String) As Integer
    Select Case temp
    Case "001"
        Char_to_Int = 1
    Case "002"
        Char_to_Int = 2
    Case "003"
Can anyone show me a quick way to do the above conversion?
Reply With Quote
  #2 (permalink)  
Old December 20th, 2012, 07:27 PM
Friend of Wrox
Points: 513, Level: 8
Points: 513, Level: 8 Points: 513, Level: 8 Points: 513, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Feb 2007
Location: Davenport, IA, USA.
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts

To 0 pad a number just do something like this within your code:

'The following line makes sure integer is 3 digits long and places it in a string
  Dim sMyString As String
  sMyString = Right("00" & iTemp, 3)
  MsgBox sMyString
To return a number is a very simple number
'The following line grabs integer value of a string
  Dim iMyInt As Long
  iMyInt = Val("001")
  MsgBox iMyInt
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Book pg 56 - char vs. signed char kkinderen BOOK: Ivor Horton's Beginning Visual C++ 2010 14 July 13th, 2012 12:28 AM
Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 09:09 PM
Convert a char to int!!!!?? vtvb C++ Programming 6 March 18th, 2006 05:51 AM
Want to reiterate on my char to int problem CNewbie C++ Programming 0 April 2nd, 2004 12:47 AM
convert char to int does not work yasaman C# 1 August 6th, 2003 10:39 AM

All times are GMT -4. The time now is 09:13 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.