+ Reply to Thread
Results 1 to 9 of 9

Losing the infamous leading 0

  1. #1
    Registered User
    Join Date
    02-14-2013
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2010
    Posts
    55

    Losing the infamous leading 0

    So I export a file manually, then have some vbs code to make some changes and then send off to another file folder. im having a problem losing the leading 0 in two columns. any ideas?? Column E:E is one of them. I tried a line below but that didnt do the trick.

    Thanks in advance!!

    Set objExcel = CreateObject("Excel.Application")

    objExcel.DisplayAlerts = FALSE
    objExcel.Visible = False

    varFile = inputbox ("Which file are you modifying? A or B")

    dteOldDate = Now()
    strNewDate = Year(dteOldDate) & Right("00" & Month(dteOldDate), 2) & Right("00" & Day(dteOldDate), 2)

    Set objWorkbook = objExcel.Workbooks.Open("C:\Users\malbre1\Desktop\Inovalon.csv")
    Set objWorksheet = objWorkbook.sheets(1)

    Set objRange = objWorksheet.UsedRange

    'ActiveSheet.Range("E:E").NumberFormat = "@"

    objRange.Replace Chr(34), "'"
    objRange.Replace Chr(44), ";"

    objWorkbook.SaveAs "C:\Users\malbre1\Desktop\Inovalon1.csv"
    objWorkbook.close (False)

    objexcel.quit

    'Set objFSO = CreateObject("Scripting.FileSystemObject")

    'objFSO.CopyFile "C:\Users\malbre1\Desktop\Inovalon.csv", "C:\Users\malbre1\Desktop\ScheduledAssessments" & "_" & strNewDate & varFile & ".csv"

    'objFSO.DeleteFile "C:\Users\malbre1\Desktop\Inovalon.xls"

  2. #2
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Losing the infamous leading 0

    If you just open the csv file as a workbook, you'll lose the leading zeroes then, so it's too late to change the format to text (but if you knew how many digits there were, you could apply a number format like "00000" for example). You could use Workbooks.OpenText which allows you to specify Field information, such as which ones to treat as Text.

    Note that that method doesn't return a workbook object, so you can't do:
    Please Login or Register  to view this content.
    You have to use:
    Please Login or Register  to view this content.
    Last edited by romperstomper; 09-04-2014 at 09:41 AM.
    Remember what the dormouse said
    Feed your head

  3. #3
    Registered User
    Join Date
    02-14-2013
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Losing the infamous leading 0

    Thank you, do you know the convention in how that field formatting is used? I dont believe this will work as I had outlined before 'ActiveSheet.Range("E:E").NumberFormat = "@"

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Losing the infamous leading 0

    (per the help) you pass an array of 2 item arrays where the first item is the column number and the second is the field type (2 or xlTextFormat for Text). For example:

    Please Login or Register  to view this content.
    The last part specifies that column 5 (i.e. E) should be treated as Text.

  5. #5
    Registered User
    Join Date
    02-03-2013
    Location
    N.Ireland
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Losing the infamous leading 0

    *1 will remove zeros. Have vba do it for you. Ideally you would correct this at the source from where you are exporting using SQL.

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Losing the infamous leading 0

    I believe the problem is that the OP doesn't want to lose leading zeroes.

  7. #7
    Registered User
    Join Date
    02-03-2013
    Location
    N.Ireland
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Losing the infamous leading 0

    Unclear from OP's post.

    If you want to keep say 5 digits including leading zeros.. Custom format > 00000

  8. #8
    Forum Contributor
    Join Date
    01-30-2013
    Location
    Wales
    MS-Off Ver
    Excel 2013
    Posts
    231

    Re: Losing the infamous leading 0

    If you have any control over creating the csv file then save it as an xls. As soon as you open a csv file with excel you have lost the leading zeros.
    If you can't save the file as an xls can you save it as a txt and when you open it with excel you can tell it to format the offending column as text.

    Or change the extension of the csv file to txt and then open with excel.

    As a rule try and save the original file as xls and csv that way if you need to make changes, do them in the xls and save as both formats when you have finished.

    From experience leading zeros from csv files is impossible, frustrating and definitely to be avoided at all costs. Any supplier that uses product codes as numbers with leading zeros should never be dealt with again.

    Best of Luck

    Chris
    Click * below if this answer helped

  9. #9
    Registered User
    Join Date
    02-14-2013
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Losing the infamous leading 0

    yes this is very difficult, I am going to try a couple of these options, however im stuck with the intial export in .csv format. If you are familiar with SalesForce at all - I only have access to the UI, which only allows me to export as .csv or .xls. The .xls is in some web format which will not allow me to modify through VBA.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Changing 5-digit Zip to 3-digit zip without losing leading zeros
    By Jennifer C in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-05-2014, 09:26 AM
  2. Copy custom format without losing leading zeros
    By ucyzgba in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2013, 09:47 AM
  3. [SOLVED] Losing Leading Zeros
    By timbo1957 in forum Excel General
    Replies: 7
    Last Post: 07-27-2012, 09:25 AM
  4. Replies: 0
    Last Post: 08-11-2011, 12:47 AM
  5. [SOLVED] How to export excel column into Access without losing the leading.
    By yesterdaytoday11 in forum Excel General
    Replies: 5
    Last Post: 03-23-2005, 12:06 PM

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