+ Reply to Thread
Results 1 to 2 of 2

Visual Basic Text Insert to Excel

  1. #1
    Registered User
    Join Date
    02-21-2005
    Posts
    1

    Visual Basic Text Insert to Excel

    I am programming a Visual Basic application that saves the data as an Excel file. I need to keep my leading 0's but Excel cuts them off. For instance, I insert a string of "00055" into Excel and it ends up being saved as 55. Here is my code.
    objXLWs - Excel worksheet
    fld - using a variant to move thru the recordset
    intRowCount and intColumnCount are to move from A1-A8 and then B,C,D ect

    objXLWs.Cells(intRowCount, intColumnCount).Value = CStr(Trim(fld.Value))

    As you can see, I tried CStr so ensure that my inserted value is text. I also tried storing the value in a temp string and inserting the temp string from Visual B. It still cut off my 0's. Any idea's?

  2. #2
    Registered User
    Join Date
    01-20-2004
    Location
    Western NY
    Posts
    99
    I have had pretty good success constructing a string that looks like what I would insert manually into an Excel cell when I want to retain leading zeros. For example, if the value is 00055 and you stuff that into an xl Cell, you will certainly get 55. If you stuff 00055 into a string and stuff that into an xl cell, you will often still get 55. However if your string is ="00055" which is how you would do it manually, that seems to work. In the examples below, 55, "00055" and the special string text are stuffed into 3 different cells:

    Cells(34, 4) = 55
    Cells(36, 4) = "00055"
    Cells(36, 4) = "=" + Chr(34) + "00055" + Chr(34)

    The first two cells display 55, the last cell displays 00055.

    Not very elegant, but it works.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1