+ Reply to Thread
Results 1 to 10 of 10

Use VBA to run "text to columns' Loop on first row to convert from text to date format

  1. #1
    Registered User
    Join Date
    05-22-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    11

    Use VBA to run "text to columns' Loop on first row to convert from text to date format

    I have a CSV file that downloads approximately 20-30 columns formatted as text. Here is an example of the origianl data that comes from the CSV file:

    Week starting 04/22/13 - Resources - Percent historical utilization


    I recorded a macro to remove the text from the cells, and then recorded a macro to run text to columns on each column. While the text to columns macro works for the current file, it does not work for all files since the number of columns changes. Can someone assist me with creating a macro that will loop through each column, run text to column (text format to date format) until an empty column is reached? The first column is always E1, but the last column can vary with each download. Here is a copy of the recorded macro:

    Sub D_MatrixUtil()
    '
    ' D_MatrixUtil Macro
    ' Convert Line 1 dates from text to date format
    '

    '

    Selection.TextToColumns Destination:=Range("E1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("F1").Select
    Selection.TextToColumns Destination:=Range("F1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("G1").Select
    Selection.TextToColumns Destination:=Range("G1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    ActiveWindow.SmallScroll ToRight:=6
    Range("H1").Select
    Selection.TextToColumns Destination:=Range("H1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("I1").Select
    Selection.TextToColumns Destination:=Range("I1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("J1").Select
    Selection.TextToColumns Destination:=Range("J1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("K1").Select
    Selection.TextToColumns Destination:=Range("K1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("L1").Select
    Selection.TextToColumns Destination:=Range("L1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("M1").Select
    Selection.TextToColumns Destination:=Range("M1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    ActiveWindow.SmallScroll ToRight:=6
    Range("N1").Select
    Selection.TextToColumns Destination:=Range("N1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("O1").Select
    Selection.TextToColumns Destination:=Range("O1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("P1").Select
    Selection.TextToColumns Destination:=Range("P1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("Q1").Select
    Selection.TextToColumns Destination:=Range("Q1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("R1").Select
    Selection.TextToColumns Destination:=Range("R1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("S1").Select
    Selection.TextToColumns Destination:=Range("S1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    ActiveWindow.SmallScroll ToRight:=6
    Range("T1").Select
    Selection.TextToColumns Destination:=Range("T1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("U1").Select
    Selection.TextToColumns Destination:=Range("U1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("V1").Select
    Selection.TextToColumns Destination:=Range("V1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("W1").Select
    Selection.TextToColumns Destination:=Range("W1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("X1").Select
    Selection.TextToColumns Destination:=Range("X1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("Y1").Select
    Selection.TextToColumns Destination:=Range("Y1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    ActiveWindow.SmallScroll ToRight:=4
    Range("Z1").Select
    Selection.TextToColumns Destination:=Range("Z1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("AA1").Select
    Selection.TextToColumns Destination:=Range("AA1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("AB1").Select
    Selection.TextToColumns Destination:=Range("AB1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("AC1").Select
    Selection.TextToColumns Destination:=Range("AC1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    ActiveWindow.SmallScroll ToRight:=4
    Range("AD1").Select
    Selection.TextToColumns Destination:=Range("AD1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("AE1").Select
    Selection.TextToColumns Destination:=Range("AE1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("AF1").Select
    Selection.TextToColumns Destination:=Range("AF1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("AG1").Select
    Selection.TextToColumns Destination:=Range("AG1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    ActiveWindow.SmallScroll ToRight:=6
    Range("AH1").Select
    Selection.TextToColumns Destination:=Range("AH1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("AI1").Select
    Selection.TextToColumns Destination:=Range("AI1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("AJ1").Select
    Selection.TextToColumns Destination:=Range("AJ1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("AK1").Select
    Selection.TextToColumns Destination:=Range("AK1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    Range("AL1").Select
    Selection.TextToColumns Destination:=Range("AL1"), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 5), TrailingMinusNumbers:=True
    End Sub

  2. #2
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Use VBA to run "text to columns' Loop on first row to convert from text to date format

    Try one of the codes below on a copy of your data. The more Column's the more I'd use the 2nd code...

    Please Login or Register  to view this content.
    OR

    Please Login or Register  to view this content.
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  3. #3
    Registered User
    Join Date
    05-22-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Use VBA to run "text to columns' Loop on first row to convert from text to date format

    Thank you WasWodge for the quick reply, the TTC code works with my data. Is there anyway to limit the code to the first row only?

  4. #4
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Use VBA to run "text to columns' Loop on first row to convert from text to date format

    A bit of a giveaway but text to columns works on columns. Can you are provide a sample of your data?
    .

  5. #5
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Use VBA to run "text to columns' Loop on first row to convert from text to date format

    Duplicate post. Sorry but it didn't seem to have gone through when posting on my mobile.
    .
    Last edited by WasWodge; 05-31-2013 at 04:15 PM.

  6. #6
    Registered User
    Join Date
    05-22-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Use VBA to run "text to columns' Loop on first row to convert from text to date format

    I updated the code to:

    Sub TTC()
    Dim LstCo As Long, i As Long
    Application.ScreenUpdating = False
    LstCo = Cells.Find(What:="*", SearchOrder:=xlColumns, SearchDirection:=xlPrevious, LookIn:=xlValues).Column
    For i = 1 To LstCo
    Rows("1:1").Select
    Columns(i).TextToColumns Destination:=Cells(1, i), DataType:=xlFixedWidth, _
    FieldInfo:=Array(0, 3), TrailingMinusNumbers:=True
    Next i
    Application.ScreenUpdating = True
    End Sub


    Selecting the first row fixed my problem. Thanks again WasWodge!

  7. #7
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Use VBA to run "text to columns' Loop on first row to convert from text to date format

    I have no doubt that it has fixed whatever issue you have but you need to be aware that text to columns is still applying to all the columns in the selection.

    To demonstrate put '1 in cell A1 drag across to cell E1.

    Select that range and drag down so that the data is covering say A1 TO E20 and then run your code.

    You will find that the whole range changes to true numbers and not just Row1.

  8. #8
    Registered User
    Join Date
    05-22-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Use VBA to run "text to columns' Loop on first row to convert from text to date format

    I see what you are saying. After taking a look at my data and requirements, I will need to update several macros to account for varying number of columns in imported CSV file. Please see sample data attached (requirements are on the original data tab in italics).
    Attached Files Attached Files

  9. #9
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Use VBA to run "text to columns' Loop on first row to convert from text to date format

    I will need to update several macros to account for varying number of columns in imported CSV file
    csmiley, you don't really need to update several macro's with the full macro (as long as they are in the same workbook) Just call the macro from the others.
    For instance if you want the code to apply the code for the activesheet from another macro.
    Put both subs ShowTTC and TTC in modules in the same workbook and then run ShowTTC.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    or to loop through all the sheets in the workbook try
    Please Login or Register  to view this content.
    The columns in the code will automatically adjust to a number of columns in each sheet.

    The rest of your requirements really need to go in a new thread as they are all a different problem to this thread

  10. #10
    Registered User
    Join Date
    05-22-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Use VBA to run "text to columns' Loop on first row to convert from text to date format

    Thank you, call function worked like a charm. I'll create another post for my other questions.

+ 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