Results 1 to 5 of 5

Import txt, csv, xls files into excel using vba

Threaded View

  1. #1
    Registered User
    Join Date
    05-02-2023
    Location
    Long Island
    MS-Off Ver
    2019
    Posts
    3

    Exclamation Import txt, csv, xls files into excel using vba

    I've been trying to open text files into excel using vba. I want to access any text, csv, xls file and import it fast by clicking one button (located on the top of "Dust Raw Data" and "VOC Raw Data" worksheets - see attached. I generated a vba code, but the issue is that when I run the code (press the import button - on the Dust Raw and VOC Raw sheets), the values containing numbers do not register and they are assumed as text instead of numbers. Additionally, I want to extract the time only from the VOC Raw Data sheet (using the PID txt file- see attached) to the "VOC table" sheet which must be in the format of hh:mm AM/PM, but instead; excel gives me as m/d/yyyy h:mm. Excel cannot understand what I`m trying to achieve so it copies the whole thing as Ex: 05/01/23 06:05:55 instead of 6:05 AM. Please let me know if I need to add more to my code and if so, what lines. TIA!!

    (Note: I`m aware of power query, but I want to use vba in this particular case)

    The VBA Code:

    Sub ImportText3()
    
        Dim ThisLine As Variant
        Dim fileFilterPattern As String
        Dim n As Long
        
        ' Just look at text files
        'fileFilterPattern = "Text Files *.txt,*.txt"
        fileFilterPattern = "Text Files (*.txt; *.csv),*.txt;*.csv"
        ThisLine = Application.GetOpenFilename(fileFilterPattern)
        
        If ThisLine = False Then
            MsgBox "No file selected."
            Exit Sub
        End If
        
        Application.ScreenUpdating = False
        
        Open ThisLine For Input As #1
        ' Look at each line of the file
        Do Until EOF(1)
            Line Input #1, ThisLine
            ' If the line contains something
            If Len(ThisLine) > 0 Then
                ' Replace any commas with tabs (ASCII character 9)
                ThisLine = Replace(ThisLine, ",", Chr(9))
                ' Divide string into an array by tab
                ThisLine = Split(ThisLine, Chr(9))
                ' Copy array to a resized range
                ActiveCell.Offset(n, 0).Resize(1, UBound(ThisLine) + 1).Value = ThisLine
            End If
            ' Point to next cell below
            n = n + 1
        Loop
        
        ' Done so close the file for input
        Close #1
    
        Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files
    Last edited by dagamerjc; 05-02-2023 at 01:04 PM. Reason: Code formatting

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Import two txt files in excel and highlights the differece b/w two files data
    By Imran/CVT in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-27-2019, 03:43 AM
  2. Iterative process to import multiple text files into multiple excel files
    By Ecth3lion in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-29-2018, 01:04 PM
  3. How to Import pdf files in to excel.
    By Muhammad Shafique in forum Excel General
    Replies: 1
    Last Post: 09-02-2014, 02:13 AM
  4. 30 excel files, two cells to import!
    By jakethemuss in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-20-2007, 08:51 AM
  5. [SOLVED] is it possible to import rtf files into Excel?
    By Teulia in forum Excel General
    Replies: 0
    Last Post: 08-18-2006, 10:05 AM
  6. Replies: 10
    Last Post: 06-06-2006, 03:18 AM
  7. HELP: Import several TXT files into Excel
    By luis in forum Excel General
    Replies: 6
    Last Post: 04-09-2006, 04:20 PM

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