Results 1 to 4 of 4

Thread: format excel as text

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    13

    format excel as text

    I am having problems outputing to my excel spreadsheet. I need the entire sheet to all formatted as text. It is messing up my account numbers when formatted as a number.

    This is what my code looks like, but it doesn't seem to like it.
    VB Code:
    1. Dim MyExcel As New Excel.Application
    2.         Dim oWorkbooks As Excel.Workbooks = MyExcel.Workbooks
    3.         Dim theWorkbook As Excel.Workbook = oWorkbooks.Add
    4.         Dim oSheet As Excel.Worksheet
    5.         Dim rng As Excel.Range
    6.         Dim exFile As String
    7.         Dim i As Integer
    8.         Dim c As Integer

    This is where I am trying to format as text:

    VB Code:
    1. oSheet.Range("A2:Z999").Select()
    2.             oSheet.Selection.NumberFormat = "@"

    Thanks in advance!

  2. #2
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: format excel as text

    VB Code:
    1. oSheet.Range("A2:Z999").Select 'you dont need parenthesis after the select
    2.             oSheet.Selection.NumberFormat = "@"
    if you fail to plan, you plan to fail

  3. #3
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: format excel as text

    Did the post by Brian_g resolve your problem?

    What exactly do you mean by "messing up my account numbers"? That doesn't convey enough information.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  4. #4
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Re: format excel as text

    This

    The problem occurs when the format is changed after the data has already been added. Sometimes the data will automatically update to the new format, and sometimes not (I'm sure there's a reason - I'm just going off of my experience). To resolve this, you can do one of two things.

    1. You can set the format to text before adding the data. A shortened version of the code you had is:

    oSheet.Range("A2:Z999").NumberFormat = "@"
    (you don't need to Select it.)

    2. Set the format after adding the data, then update the contents of each cell. Excel then recognizes the new format. Here's an example:

    Dim c as Excel.Range
    Dim r as Excel.Range
    Dim txt as String

    Set r = oSheet.Range("A2:Z999")
    rng.NumberFormat = "@"
    For Each c in r
    txt = c.Text
    c.Text = txt
    Next

    Set c = Nothing
    Set r = Nothing

    If you have any data with leading 0's, then you will definitely want to set the format before adding the data to the cells.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width