Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > MSFlexgrid to Excel save numbers as text


Reply
 
Thread Tools Display Modes
  #1  
Old 01-24-2005, 11:50 AM
shezan74 shezan74 is offline
Newcomer
 
Join Date: Jan 2005
Posts: 2
Angry MSFlexgrid to Excel save numbers as text


Hi there, i've a problem with a small apps i'm working on.
I use this code to move a 2D MSFlexGrid to an excel file:

Code:
For i = 0 To MSFlexGrid1.Rows - 1 For j = 0 To MSFlexGrid1.Cols - 1 Spreadsheet1.Cells(i + 1, j + 1) = MSFlexGrid1.TextMatrix(i, j) Next Next Spreadsheet1.Columns.AutoFit nfile = FreeFile Open "filea" For Output As #nfile Close #nfile FileCopy "filea", App.Path & CStr(date) & ".xls" Spreadsheet1.Export (App.Path & CStr(date) & ".xls")

All is working very well, except for the fact that i have some columns filled with numbers in my MSFlexGrid and those numbers, once moved to excel file, are stored as STRINGS and it's impossible to convert those to number again except by using a trick with copy "1" then paste special on all interested columns with "multiply" option...
I've searched all MS kb but i can't find a way to get rid of this problem...

Someone can please save me?

Last edited by shezan74; 01-24-2005 at 11:57 AM.
Reply With Quote
  #2  
Old 01-24-2005, 03:04 PM
herilane's Avatar
herilane herilane is offline
Unashamed geek

Retired Moderator
* Expert *
 
Join Date: Jul 2003
Location: London, England
Posts: 8,988
Default

Change the cell format to General, and then re-enter those numbers.
Code:
Selection.NumberFormat = "General" Selection.Formula = Selection.Formula
Reply With Quote
  #3  
Old 01-24-2005, 03:10 PM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,884
Default

Some other method, which is also better when working with Excel 2003 (to prevent those green markers in the cells):

Code:
For i = 0 To MSFlexGrid1.Rows - 1 For j = 0 To MSFlexGrid1.Cols - 1 If IsNumeric(MSFlexGrid1.TextMatrix(i, j)) Then Spreadsheet1.Cells(i + 1, j + 1) = CDbl(MSFlexGrid1.TextMatrix(i, j)) Else Spreadsheet1.Cells(i + 1, j + 1) = MSFlexGrid1.TextMatrix(i, j) End If Next Next
Reply With Quote
  #4  
Old 01-25-2005, 01:16 AM
shezan74 shezan74 is offline
Newcomer
 
Join Date: Jan 2005
Posts: 2
Default

Quote:
Originally Posted by Flyguy
Some other method, which is also better when working with Excel 2003 (to prevent those green markers in the cells):

Code:
For i = 0 To MSFlexGrid1.Rows - 1 For j = 0 To MSFlexGrid1.Cols - 1 If IsNumeric(MSFlexGrid1.TextMatrix(i, j)) Then Spreadsheet1.Cells(i + 1, j + 1) = CDbl(MSFlexGrid1.TextMatrix(i, j)) Else Spreadsheet1.Cells(i + 1, j + 1) = MSFlexGrid1.TextMatrix(i, j) End If Next Next
Astonishing and exceptionally simple
Thank you a lot (this was my first attempt to make vb talk with MSOffice )
Thank you
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->