+ Reply to Thread
Results 1 to 10 of 10

Editing string based on length

  1. #1
    Registered User
    Join Date
    09-01-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    9

    Editing string based on length

    Good morning, please see spreadsheet below. I get all my results from a CSV file. This isn’t a problem except for the first nine days of the month where excel cuts off the first 0 so 01012009 reads 1012009 and cannot be properly concatenated.

    Date Name Item
    1012009 Bob Keyboard
    1012009 Sid Mouse
    1012009 Dave Monitor
    1012009 Ed Monitor
    1012009 Andrew Keyboard
    1012009 Dan Keyboard
    1012009 Bruce Keyboard

    What I would like to do is have VB read the contents of the date cell and if it is less than eight characters, edit the cell by adding a zero at the beginning of the string. This needs to be a one off process for all cells. Can anyone come up with code that might make this happen? Thanks, 0nyx
    Last edited by NBVC; 09-01-2009 at 01:36 PM.

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444

    Re: Editing string based on length

    Hi,

    highlight the cells you want to check and run this code

    Please Login or Register  to view this content.
    Which adds a 0 to the front of the data if it's 7 characters long. NB, the apostrophe is added to convert to text format to avoid truncating again.
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Editing string based on length

    If you save the original file as a .txt file instead of .csv.. then when you open it in Excel.. a text wizard will help you import the file... Click Delimited in first window, Select Comma in second window, select the column from the Data Preview pane in the next window that corresponds to your part numbers...and then select Text from the Column Data Format area.... then click Finish...

    Now you should have the data as you desired it.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  4. #4
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Editing string based on length

    Hi

    Do you intend for the cell contents to be seen by Excel as a string rather than as a numeric value? If so, the following should work and will be reasonably quick:

    Please Login or Register  to view this content.
    Richard
    Richard Schollar
    Microsoft MVP - Excel

  5. #5
    Registered User
    Join Date
    09-01-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Editing string based on length

    Im having most luck with Sweep's code but I cant seem to build in a way to select all the cells, any ideas with that?

  6. #6
    Registered User
    Join Date
    09-01-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Editing string based on length

    Ah nope I sorted it with

    Sub a()
    Range("E1:E1000").Select
    Dim rCell As Range
    For Each rCell In Selection
    If Len(rCell.Value) = 7 Then rCell.Value = "'0" & rCell.Value
    Next
    End Sub

    Thanks for everyone's help with this. I appreciate it.

    0nyx

  7. #7
    Registered User
    Join Date
    09-01-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Editing string based on length

    Ah no i didnt.. that brings up a runtime error six and highlights

    If Len(rCell.Value) = 7 Then

  8. #8
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Editing string based on length

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

  9. #9
    Registered User
    Join Date
    09-01-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Editing string based on length

    Apologies NBVC, i solved the problem so will go away now anyways.

    Many thanks.

  10. #10
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Editing string based on length

    Please remember to mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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