Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 11th, 2003, 10:08 AM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default Changing Cell-formating in Excel via VBA

Hi

I am creating an Excel-Worksheet threw VBA, but my problem is that 4 of the columns need to be formated as Text, but they end up as General. Does anyone know how to format all the cells in a column via an Excel Worksheet object?

Thanks in advance.

Gert
 
Old November 11th, 2003, 11:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Gert,

The way I find out things like this is to go into Excel, record a new Macro, then do whatever it is I want to find out about (in this case format a column to text). Then I open up the Excel VB Editor and look at the VBA in the macro I just recorded.

In this case you get:
Columns("B:B").Select
Selection.NumberFormat = "@"

which can be condensed to
Columns("B:B").NumberFormat = "@"

hth
Phil
 
Old November 11th, 2003, 11:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Gert, I should have added that "Columns" is a property of the Worksheet object, so you would probably need:
worksheetVariableName.Columns("B:B").NumberFormat = "@"

Additionally, you can refer to the column by number (starting at 1) instead of letter.

rgds
Phil
 
Old November 11th, 2003, 06:20 PM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Phil. I haven't had time to try it out yet, but it looks like a good sugestion. I thanks for the Macro-tip.

Gert
 
Old May 10th, 2005, 01:56 PM
Registered User
 
Join Date: May 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have same problem, but I want to know how to change from text format to number format.

I am generating a report with vb .net in a .xls sheet but my numbers are stored as text. how can I fix this?

The number of hours is saved as text and not as number. Any ideas?
[vb]
Code:
...
While i < dsetTask.Tables("tblTask").Rows.Count
            drowData = Data.NewRow()
            drowData("Name") = dsetTask.Tables("tblTask").Rows(i)("Name")
            drowData("Hour") = dsetTask.Tables("tblTask").Rows(i)("Hour")
End While
...
[/vb]

any ideas?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel VBA Check When Cell Has Changed tonyrosen Excel VBA 13 March 24th, 2015 05:15 PM
Excel cell Formating Using C# veeruu C# 5 September 21st, 2011 01:56 PM
editing datagrid and changing cell color upon edit matoaka VS.NET 2002/2003 0 August 11th, 2006 11:04 AM
Capture cell value before changing it chp Excel VBA 2 March 15th, 2006 03:40 PM
Dynamically Changing the content of a table cell asdasd Javascript 4 February 18th, 2005 01:44 PM





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