Solved

Formatting Excel from VB

Posted on 2001-08-15
9
171 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

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 I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

706 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

19 Experts available now in Live!

Get 1:1 Help Now