+ Reply to Thread
Results 1 to 12 of 12

Text to Columns

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2008
    Posts
    5

    Text to Columns

    Hi

    I have a small problem, I am trying to write a micro code to split text which is copied into cell A1 into columns. I can do this fine by going to "data" the "text to Columns" and selecting the places i want to split the text (this is the same for every piece of data i copy in). The macro works perfectly every time. the problem is that the spreadsheet is shared and i want to protect certain cells on the sheet, when i protect the sheet the recorded macro does not work as the "data", "text to columns" is not available in a protected workbook.

    I was just wondering if someone could help me, so i can run a macro to split the text which also allows me to protect cells. In the "text to column" option the "fixed width" (column breaks) i choose are: 4, 25, 34 and 43.

    Any help would be much appreciated!

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    This line of code should do it.

    Columns(1).TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(4, 1), Array(25, 1), Array(34, 1), Array(43, 1))
    Martin

  3. #3
    Registered User
    Join Date
    06-17-2008
    Posts
    5
    I'm sorry i'm new to excel code. How do i add the code and once this is done how do i run it!

    Thanks!

  4. #4
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    Hit alt F11

    Insert - Module

    Paste the following into the new sheet.

    Sub MySplit
    Columns(1).TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(4, 1), Array(25, 1), Array(34, 1), Array(43, 1))
    End Sub
    Hit F11

    Tolls-Macros-MySplit

  5. #5
    Registered User
    Join Date
    06-17-2008
    Posts
    5
    The code works perfectly. Thanks very much. However to run it i have to go into 'tools' then 'macro' and hit run there, is there a way to assign a key command to the script, for example Ctrl + T.

    It also does not work when lock certain cells and I protect the sheet, is there a way around this?
    Last edited by cob; 06-18-2008 at 02:56 PM.

  6. #6
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    Go to Tools - macro - select the macro and press options to specify the shortcut key you wish to assign.

  7. #7
    Registered User
    Join Date
    05-16-2009
    Location
    Bangkok
    MS-Off Ver
    Excel 2007/2010
    Posts
    12

    Re: Text to Columns (how to define format?)

    Hi,

    I am searching for fixed width text to column macro and found this reply. However, when I apply above macro, the result of zeros in front of figures disappear.

    e.g. sample text to split to column: 000122042009ABCDEFG00567

    Required result:
    0001|22042009|ABCDEFG|00567


    when running below macro; result shows:
    1|22042009|ABCDEFG|567

    Sub MySplitB()
    Columns(1).TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(4, 1), Array(12, 1), Array(19, 1), Array(24, 1))
    End Sub
    Would anyone please help modify the macro to define result in some columns as text format?

    Thanks
    DT.
    Last edited by dusit_t; 05-19-2009 at 10:57 AM. Reason: enter [code] tag for macro section

  8. #8
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Text to Columns

    Try replacing 1 with 2 in the appropriate array.

    e.g.

    Array(0, 2)
    to make the first column text,

+ 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