Consulting

Results 1 to 9 of 9

Thread: Abstract first 6 characters from a column

  1. #1

    Abstract first 6 characters from a column

    Hello - I have a spreadsheet column (C) in which the first 6 characters are a code number and the rest is text. I want to extract them into a column (B) which I have created with VBA.

    So what I want to do is

    read cell C2 and extract 6 characters - Left (contents of C2,6)
    put this in cell B2
    move to row 3
    read cell C3 and extract 6 characters - Left (contents of C3,6)
    put this in cell B3

    and so on until I reach the last row - I have found the number of rows used so my loop will be
    For n = 2 to (Number of rows)
    Code for above.
    Next n

    I can't get my head round excell syntax!

    Can anybody advise please?

    Thanks, Mel P

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    For n = 2 to (Number of rows)
    Cells(i,"B").Value = Left$(Cells(i,"C").Value,6)
    Next n
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thanks - the i should be n?
    Do i need the $ sign

    This works:

    Dim n As Integer
    For n = 2 To Num_rows
    Cells(n, "B").Value = Left(Cells(n, "C").Value, 6)
    Next n

    Thanks very much, Mel

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes, i should be n

    You don't need the $, but as it is a string function, I use the string ($) version.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    The spreadsheet forumula =LEFT(C2,6), placed in B2 and copied down sounds easier.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Except that the OP said they wanted to do it in VBA.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Mel,

    you want it in VBA, but looping is slow, so combining VBA with Excel formula is much faster for this type of problem. e.g.
    [VBA] With Range("C2", Range("C" & Rows.Count).End(xlUp)).Offset(0, -1)
    .Formula = "=Left(C2,6)"
    .Value = .Value
    End With[/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  8. #8
    Surely an Abstract of the first 6 characters would be @@@@@@
    2+2=9 ... (My Arithmetic Is Mental)

  9. #9

    Talking

    Ho Ho to last.

    I will try the combination from Johnske - it's easy to forget the obvious!

    Thanks to all.

    My first use of this site and 100% satisfaction!

    Mel

Posting Permissions

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