+ Reply to Thread
Results 1 to 2 of 2

vba converts text to values?

Hybrid View

  1. #1
    Registered User
    Join Date
    03-23-2004
    Posts
    23

    vba converts text to values?

    Need help with vba in Excel. I have a list of zip codes many of which begin with zeroes so I have converted them to text using Excel's built-in Text function: Text(ZipCode,"00000"). When I run the vba command to assign the value of a zip to another cell on the spreadsheet, it converts it to a number, not as text, and thus my routine bombs on subsequent lines of code on a "type mismatch."

    ex: Range("NewZip").Value = Range("OldZip").Value

    where OldZip is the original zip code formatted as text and NewZip is the cell where the value of the zip code is assigned--with the leading zeroes stripped off. Is there some sort of Text command in vba whereby I could do something like:

    Range("NewZip").Value = Text(Range("OldZip).Value,"0000")

    you know, kind of like the built-in Excel text command?

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Bill


    You can overcome this problem several ways

    Method 1

    Range("NewZip").NumberFormat = "@"
    Range("NewZip").Value = Text(Range("OldZip).Value,"0000")


    Method 2

    Range("NewZip").Value = "'" & Text(Range("OldZip).Value,"0000")

+ 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