+ Reply to Thread
Results 1 to 2 of 2

Importing data - problem with changing date format!

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-13-2011
    Location
    Havant, Hants, England
    MS-Off Ver
    Excel 2010
    Posts
    116

    Importing data - problem with changing date format!

    Hi, I'm importing data using a piece of code as shown below. However, when it's imported (using "MasterProcedurePhaseOne") the data format is changed from dd/mm/yyyy to mm/dd/yyyy so it is incorrect. Can anyone advise how I make it work correctly? thanks, Neil

    I've attached the file I am importing to (Importing File) and the one I am importing from (Data to Import).

    The code I"m using is as follows:

    Sub ImportData()
        Dim Filt As String
        Dim FilterIndex As Integer
        Dim Title As String
        Dim FileName As Variant         'Filename to be imported
        Dim sourceWorkbook As Workbook  'source file
        Dim targetWorkbook As Workbook  'Workbook to import data into (This one)
        Dim targetSheet As Worksheet    'worksheet to import data into
        Dim sourceSheet As Worksheet    'worksheet to import data from
        Dim WSD As Worksheet
        '---------------
        
        'Assumption that active workbook is the target
        Set targetWorkbook = Application.ActiveWorkbook
        
    '   Set up list of file filters
        Filt = "Text Files (*.txt),*.txt," & _
               "Lotus Files (*.prn),*.prn," & _
               "Comma Separated Files (*.csv),*.csv," & _
               "ASCII Files (*.asc),*.asc," & _
               "All Files (*.*),*.*"
    
    '   Display *.* by default
        FilterIndex = 5
    
    '   Set the dialog box caption
        Title = "Select a File to Import"
    
    '   Get the file name
        FileName = Application.GetOpenFilename _
            (FileFilter:=Filt, _
             FilterIndex:=FilterIndex, _
             Title:=Title)
    
    
    '   Exit if dialog box canceled
        If FileName = False Then
            MsgBox "No file was selected."
            Exit Sub
        End If
    '-------------------
        Set sourceWorkbook = Application.Workbooks.Open(FileName) 'workbook to get data from picked up from dialogue box
        Set WSD = targetWorkbook.Worksheets("Data") 'assign sheet named "Data" into which to copy from expense file
        Set sourceSheet = sourceWorkbook.Worksheets(1) 'Copy from first worksheet
        
        'Copy in the Used range from the source sheet and put into the current workbook in worksheet "Data"
        WSD.Range("A1:E" & Cells(Rows.Count, 1).End(xlUp).Row).Value = sourceSheet.UsedRange.Value
        
        ' Close expense workbook
        sourceWorkbook.Close
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    04-13-2011
    Location
    Havant, Hants, England
    MS-Off Ver
    Excel 2010
    Posts
    116

    Re: Importing data - problem with changing date format!

    I've just found out that it is because I"m using a CSV file to import the data from - the issue doesn't occur when I use the file as an XLSX file! Hence it won't show up as a problem in the file I've loaded! Hence, I have a solution, change the original file to an XLSX file. However, I'd still like to know why it happens with a CSV file.... if anyone can advise....?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Having a problem changing date format
    By ns5622 in forum Excel General
    Replies: 4
    Last Post: 05-29-2013, 01:45 AM
  2. Date Format Issue: Importing data into Excel 2007
    By harribry in forum Excel General
    Replies: 1
    Last Post: 08-18-2011, 11:17 AM
  3. Problem with importing multiple data files with fixed width format
    By sport_logo in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-21-2010, 07:34 AM
  4. [SOLVED] Imported Data Date Format Problem
    By Craig in forum Excel General
    Replies: 1
    Last Post: 12-20-2005, 10:30 AM
  5. Strange problem: Computer's date changes when importing data
    By Franco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-28-2005, 08:06 PM

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