Results 1 to 11 of 11

Thread: [RESOLVED] Reading Excel sheet in VB, need to read the leading zeros

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    19

    Resolved [RESOLVED] Reading Excel sheet in VB, need to read the leading zeros

    Hi, I am quite newbie to excel and vb, but trying to make something with them, I need help now though.
    My excel contains a Cell value of say "0342345534", and I want to read it like this :

    Dim f As String
    f = objDepReqSheet.Cells(1,2).Value()

    the thing is that f keeps getting the value "342345534" rather than "0342345534".
    I tried also :

    f = objDepReqSheet.Cells(1,2).Value().ToString() ' This does the same

    and:
    f = objDepReqSheet.Cells(1,2).ToString() ' This yields System._comObject

    How can I make string f get the correct value ?
    thank a bunch in advance.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    19

    Re: Reading Excel sheet in VB, need to read the leading zeros

    btw, forgot to tell about some initializations:

    Dim objDepReqSheet As Object

    Dim ObjInputRefWorkBook As Object
    Dim ObjInputRefSheet As Object
    Dim objRefSheet As Object

    ObjInputXLS = CreateObject("Excel.Application")

    ObjInputDepReqWorkBook = ObjInputXLS.Workbooks.Open(DepReq.Text)
    ObjInputDepReqSheet = ObjInputDepReqWorkBook.Worksheets(1)
    objDepReqSheet = ObjInputDepReqWorkBook.Sheets(WorksheetNum)

    That's the method I use to work with Excel...
    Tx again

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Reading Excel sheet in VB, need to read the leading zeros

    check to make sure there is no Formatting on the cell (that adds the zero)

    to do this: Select the cell.. look in the forumla bar.. does the value MATCH whats displayed in the cell?

    Example:

    put the number: 342345534 in a cell
    now click Format -> Cells from the menus
    select the Number tab... then Custom
    enter 0######### in the textbox
    then click ok.

    now.. see? the CELL has the 0 but the actual value doesnt

    u would need to use Format() to get the zero back

    f = Format(objDepReqSheet.Cells(1,2).Value(),"0#########")
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    19

    Re: Reading Excel sheet in VB, need to read the leading zeros

    very interesting... !
    but what if not all numbers begin with a zero, and their length is unknown ?

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Reading Excel sheet in VB, need to read the leading zeros

    in the example I posted.. if the lenght of the cell is less than 10 numbers a zero will be added

    anyway.. try this:


    Dim f As String
    f = objDepReqSheet.Cells(1,2).Text

    worked in my test
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    19

    Re: Reading Excel sheet in VB, need to read the leading zeros

    Is the formatting of the cell General/Custom/Text ?

  7. #7
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Reading Excel sheet in VB, need to read the leading zeros

    formatting can be General, number, text, custom, date, etc.. there are tons..
    BUT, as far as getting the contents

    TEXT will get whats displayed
    VALUE will get the actual Value
    FORMULA will get the formula
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    19

    Re: Reading Excel sheet in VB, need to read the leading zeros

    Seems to work !
    Just how did you get to that .Text property ? Intellisense doesn't ring me a bell about .Text or .Format or anything like it... ?

  9. #9
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Reading Excel sheet in VB, need to read the leading zeros

    well tha would make sense.. intellisense probably doesnt show u anything because of how u are declaring it

    VB Code:
    1. Dim ObjInputRefWorkBook As Object
    2. Dim ObjInputRefSheet As Object
    3. Dim objRefSheet As Object

    if u add a reference to MS Excel x.0 Object Library and change the references to this

    VB Code:
    1. DIm ObjInputXLS as Excel.Application
    2. Dim ObjInputRefWorkBook As Workbook
    3. Dim ObjInputRefSheet As Worksheet
    4. Dim objRefSheet As Worksheet

    Set ObjInputXLS = New Excel.Application

    that will give u all the intellisense...
    now. 1 issue with this is that its not as compatible.
    if u use this method (Early binding) then people with earlier versions of excel wont be able to use this.

    so.. here is what I do.

    Program this way.. get everything running. then switch it BACK to the object/create object(Late binding) method. (Dont forget to remove the reference)


    Another way is to go into excel and program it then move your code over to VB
    u can even record a macro to figure out how to do something, then copy the code over. once copied, just add the object reference back in and u are good to go.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    19

    Re: Reading Excel sheet in VB, need to read the leading zeros

    Tx bro, this is very useful

  11. #11
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Reading Excel sheet in VB, need to read the leading zeros

    no problem!

    can u go to Thread Tools and select "Mark thread resolved"?

    Thanks!!
    oh yeah and
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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