Results 1 to 12 of 12

Thread: [RESOLVED] Help needed with Delimiting

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    167

    Resolved [RESOLVED] Help needed with Delimiting

    Dear all,



    I have writen some code that takes a CSV file and converts it into an Excel format.



    My problem is, when I perform this function manually through Excel the excel sheet formats correctly with all dates in UK format.

    But when I perform the exact same function via VBA any dates that can be read in US version format that way.



    Example: - Manually ,01/12/2008, becomes 01/12/2008

    VBA ,01/12/2008, becomes 12/01/2008



    Can you enlighten me as to what is going wrong when performed via VBA?



    Code:
    ChDir "I:\H925 Buying\Scott Atkinson\Projects\Import Tracker\DHL reports"
        Workbooks.Open Filename:= _
            "I:\H925 Buying\Scott Atkinson\Projects\Import Tracker\DHL reports\Booked not Shipped Report.xls"
        Columns("A:A").Select
        Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
            :=Array(1, 4), TrailingMinusNumbers:=True
        Cells.Select
        Cells.EntireColumn.AutoFit
        ActiveWorkbook.SaveAs Filename:= _
            "I:\H925 Buying\Scott Atkinson\Projects\Import Tracker\DHL reports\Booked not Shipped Report.xls" _
            , FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
            ReadOnlyRecommended:=False, CreateBackup:=False
        ActiveWindow.Close

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Help needed with Delimiting

    two things...

    1) How is it stored in the CSV? 01/12/2008 or 12/01/2008?

    2) In which columns are the date stored in?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: Help needed with Delimiting

    one way that may work is:

    if you have a template file you are adding the data to you formate the cell that will hold the date and this should fix it.

    the issue you are having is due to the fact the MS believe that everyone uses the non UK date format when they create their wizard functions.

    you may be better reading the file into an array and then on the output to excel format the date using the format function and processing in line by line.

    David
    This is Blank

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    167

    Re: Help needed with Delimiting

    Quote Originally Posted by koolsid
    two things...

    1) How is it stored in the CSV? 01/12/2008 or 12/01/2008?

    2) In which columns are the date stored in?
    The data is stored as 01/12/2008.

    The column is as per the column in the Columns(..:..) command.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    167

    Re: Help needed with Delimiting

    Quote Originally Posted by Davadvice
    one way that may work is:

    if you have a template file you are adding the data to you formate the cell that will hold the date and this should fix it.

    the issue you are having is due to the fact the MS believe that everyone uses the non UK date format when they create their wizard functions.

    you may be better reading the file into an array and then on the output to excel format the date using the format function and processing in line by line.

    David
    David,

    I am already using a template that is formatted correctly, but still does not work.

    The last statement interested me, how would I achieve the array function?

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Help needed with Delimiting

    Okay, Can I see your csv file? let me see if I can whip up a quick code for you
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    167

    Re: Help needed with Delimiting

    Quote Originally Posted by koolsid
    Okay, Can I see your csv file? let me see if I can whip up a quick code for you
    Kool,

    One of my CSV files.
    Attached Files Attached Files

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Help needed with Delimiting

    I just tried it and shows the date correctly...

    Here is the code

    Code:
        Workbooks.OpenText Filename:="P:\AdHocReport[1].txt", Origin:=xlMSDOS, _
            StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=True, _
            Space:=False, Other:=False, FieldInfo:=Array(Array(1, 4), Array(2, 1), Array( _
            3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10 _
            , 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array(16, 1), _
            Array(17, 1)), TrailingMinusNumbers:=True
        Cells.Select
        Cells.EntireColumn.AutoFit
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    167

    Re: Help needed with Delimiting

    Quote Originally Posted by koolsid
    I just tried it and shows the date correctly...

    Here is the code

    Code:
        Workbooks.OpenText Filename:="P:\AdHocReport[1].txt", Origin:=xlMSDOS, _
            StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=True, _
            Space:=False, Other:=False, FieldInfo:=Array(Array(1, 4), Array(2, 1), Array( _
            3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10 _
            , 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array(16, 1), _
            Array(17, 1)), TrailingMinusNumbers:=True
        Cells.Select
        Cells.EntireColumn.AutoFit

    Kool,

    I have eight reports all CSV files with differing information in.

    How does the Array function work in conjunction to the file formats, so that I can modify it for my other files?

    For example, I assume that each array statment is referring to each data set divided by the commas.

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Help needed with Delimiting

    How does the Array function work in conjunction to the file formats, so that I can modify it for my other files?
    It works on the headers that the text file has and which you want to have in the xls file for example, this particular text file has 17 headers...

    1 Order
    2 Item
    3 Partner ID
    4 Partner Name
    5 Shp Qty
    6 Container
    7 Origin
    8 Early Ship Date
    9 Late Ship Date
    10 Sailing
    11 Est Disch
    12 ETA Dest
    13 Vehicle Name
    14 Actual Disch Date
    15 Commercial Invoice
    16 Invoice Value
    17 BOOKING REF
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    167

    Re: Help needed with Delimiting

    And what does the second number in each array relate to?

  12. #12
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Help needed with Delimiting

    And what does the second number in each array relate to?
    Before I explain this... you need to first understand on how can you format the data in the Text to Columns. There are 3 formats 1 (GeneralFormat), 2(TextFormat) and 4(DMYFormat) for the data in the columns.

    So the second number in each array relates to these formats for example let's individually assing formats for the above text file... see the pic attached...

    Also we need to remember that when Excel VBA sees a *.csv filename, it ignores the FieldInfo data. Now, the dates in your file are in dd-mm-yyyy format, however Excel is seeing them as mm-dd-yyyy. Consequently, where a date is a valid mm-dd-yyyy date, Excel formats it in mm/dd/yyyy date format, otherwise it formats it as dd-mm-yyyy in general format.

    If you rename the .csv to .txt (or almost anything else), then it will
    format the dates as expected.
    Attached Images Attached Images  
    Last edited by Siddharth Rout; Oct 28th, 2008 at 08:39 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width