Solved

Formatting Excel from VB

Posted on 2001-08-15
9
178 Views
Last Modified: 2012-05-04
Hi all,
 I have written a VB program that generates a formatted excel file from the recordset. Here are 2 of its columns
------------------------------------------------------
THIS IS THE HEADING PART

clmNumber = Len(Str(Int(3)))
clmName = Right(Str(Int(3)), clmNumber - 1)
clmRange = "a" & clmName
xls.Range(clmRange).Value = "Account"
   
clmNumber = Len(Str(Int(3)))
clmName = Right(Str(Int(3)), clmNumber - 1)
clmRange = "b" & clmName
xls.Range(clmRange).Value = "Shares"
-----------------------------------------------------
THIS IS THE PART WHERE DATA IS POPULATED
WITHIN A LOOP I HAVE

clmNumber = Len(Str(Int(i)))
clmName = Right(Str(Int(i)), clmNumber - 1)
clmRange = "g" & clmName
xls.Range(clmRange).HorizontalAlignment = xlHAlignLeft
xls.Range(clmRange).Borders.Weight = xlThin
xls.Range(clmRange).Value = rs!Account

clmNumber = Len(Str(Int(i)))
clmName = Right(Str(Int(i)), clmNumber - 1)
clmRange = "h" & clmName
a = Format(rs!Shares, "$###,###,###,###,##0.00")
xls.Range(clmRange).Style = "Currency"
xls.Range(clmRange).NumberFormat = "$###,###,###,##0.00"
xls.Range(clmRange).HorizontalAlignment = xlHAlignRight
xls.Range(clmRange).Borders.Weight = xlThin
xls.Range(clmRange).Value = a
--------------------------------------------------------
PROBLEM
 The shares part is displayed correctly ie. if 100 in the recordset then it is representes as $100.00.Perfect
 The account part is not ie. 123e67 becomes 1.23E+69 and 789e12 as 7.89E+14. It does formatting on its own and i think it is because of the "e". I was thinking of first converting the account to a text and then displaying it.
Got this idea because when you right-click on a cell and then click format-cell you get the option of displaying it as text and it works. But I dont know how to get that property to work from my code as I got it for displaying currency.

Any help
0
Comment
Question by:aspguru
9 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 6390611
xls.Range(clmRange).NumberFormat = "Text"
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6390616
From the help:   The format code is the same string as the Format Codes option in the Format Cells dialog box.
0
 

Expert Comment

by:cpearl_s
ID: 6391711
hai,
did u try converting the out coming recordset value to  CStr(value)
hope this works
regards
cpearl
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:aspguru
ID: 6392754
To PaulHews,
 I added the line to my code but still it does not work. Is there anything wrong that I am doing

-------------------------------------------------------
clmNumber = Len(Str(Int(i)))
clmName = Right(Str(Int(i)), clmNumber - 1)
clmRange = "g" & clmName
xls.Range(clmRange).NumberFormat = "Text"
xls.Range(clmRange).HorizontalAlignment = xlHAlignLeft
xls.Range(clmRange).Borders.Weight = xlThin
xls.Range(clmRange).Value = rs!Account
--------------------------------------------------------
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6392846
Oops.  Tried it myself; didn't work.  Opened an excel worksheet with a text formatting and found this.

xls.Range(clmRange).NumberFormat = "@"
0
 
LVL 2

Expert Comment

by:Lunchy
ID: 6392853
<ping>
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 100 total points
ID: 6392878
Looks like the formatting codes are actually the ones in the Custom format box.
0
 

Author Comment

by:aspguru
ID: 6393026
Thanks paul.  You were very helpful.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6393041
Happy to help.  Thanks for the points.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

839 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