Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 10-20-2014, 01:12 PM
macropod's Avatar
macropod macropod is offline Import data from CSV to table Windows 7 64bit Import data from CSV to table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Public Sub DataImport()
Application.ScreenUpdating = False
Dim FName As Variant, Sep As String
FName = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please select text file...")
If FName = False Then
  MsgBox "You didn't select a file"
  Exit Sub
End If
Sep = InputBox("Enter a single delimiter character.", "Import Text File", ",")
If Sep = "" Then
  Sep = Chr(9)
End If
ActiveSheet.Range(Replace(ActiveSheet.Range("A1").CurrentRegion.Address, "$A$1", "$A$2")).ClearContents
Call ImportTextFile(CStr(FName), Sep, 1, 2)
Application.ScreenUpdating = True
End Sub
 
Public Sub ImportTextFile(FName As String, Sep As String, iCol As Long, iRow As Long)
Dim RowNdx As Long, ColNdx As Long, SaveColNdx As Long, Pos As Long, NextPos As Long
Dim TempVal As Variant, WholeLine As String
On Error GoTo ErrExit:
SaveColNdx = iCol
RowNdx = iRow
Open FName For Input Access Read As #1
While Not EOF(1)
  Line Input #1, WholeLine
  If Right(WholeLine, 1) <> Sep Then
    WholeLine = WholeLine & Sep
  End If
  ColNdx = SaveColNdx
  Pos = 1
  NextPos = InStr(Pos, WholeLine, Sep)
  While NextPos >= 1
    TempVal = Mid(WholeLine, Pos, NextPos - Pos)
    Cells(RowNdx, ColNdx).Value = TempVal
    Pos = NextPos + 1
    ColNdx = ColNdx + 1
    NextPos = InStr(Pos, WholeLine, Sep)
  Wend
  RowNdx = RowNdx + 1
Wend
ErrExit:
On Error GoTo 0
Close #1
End Sub
Note that the code allows you to specify the separator (in case the file doesn't use commas - if you leave the separator spec empty, tabs will be assumed) and the starting row/column for the data import. As coded row 2 column A is the assumed starting point.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
import .mht file data to powerpoint Rubini MJ PowerPoint 0 07-24-2013 10:24 PM
Import data from CSV to table Data query import Gandalf Excel 9 02-12-2013 03:03 AM
Import excel data in to SQL Server DavidBrown Excel 0 08-08-2011 04:49 AM
Import Pics and Excel Data into PP? jawillyams PowerPoint 0 03-13-2011 01:03 PM
Import data from CSV to table Outlook data import Cunner Outlook 2 08-20-2010 02:03 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:01 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft