Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-13-2014, 08:43 AM
gebobs gebobs is offline Import data from CSV to table Windows 7 64bit Import data from CSV to table Office 2010 64bit
Expert
Import data from CSV to table
 
Join Date: Mar 2014
Location: Atlanta
Posts: 837
gebobs has a spectacular aura aboutgebobs has a spectacular aura about
Default Import data from CSV to table

Attached is a sanitized version of something I am working. The next objective is to automate importing and appending the data from a CSV with a few parameters:



1. the existing data in the Log table should be deleted
2. the data from the CSV should be appended to the existing Log table
3. there should be no blank rows when finished

Below was something I grabbed from another sheet I have. That one however just imports the data to a blank sheet so there's no existing table to deal with.

#1 is addressed. #3 can be addressed by simply deleting all but one row of data though I'm unsure how to do this in VBA. When pasting to a table, Excel automatically makes it large enough to accommodate all the data.

#2 is really where my meager VBA fails. Running as is, the data is inserted to the left of the table. I suppose what could be one is to have the table deleted entirely, import the data, and then create the table again. But if anyone has any ideas how I can eliminate that need, I'd appreciate it.

Sub CSV_Import()

Dim ws As Worksheet, strFile$

Selection.AutoFilter
Range("Log").Select
Selection.ClearContents

Set ws = ActiveWorkbook.Sheets("Log") 'set to current worksheet name

strFile = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please select text file...")

With ws.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=ws.Range("A1"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh
End With

End Sub
Attached Files
File Type: xlsm getdistinct macro.xlsm (17.2 KB, 11 views)
Reply With Quote
  #2  
Old 10-17-2014, 09:54 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: 21,956
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

Your specs 1 & 2 seem to be at odds. If the old data are deleted, how can the new data be appended? I suspect you want the old data replaced by the new data but, if so, you have a round-about way of saying so.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-20-2014, 09:49 AM
gebobs gebobs is offline Import data from CSV to table Windows 7 64bit Import data from CSV to table Office 2010 64bit
Expert
Import data from CSV to table
 
Join Date: Mar 2014
Location: Atlanta
Posts: 837
gebobs has a spectacular aura aboutgebobs has a spectacular aura about
Default

What I would like to do...

* delete the data in the table
* copy the new data to the table
Reply With Quote
  #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: 21,956
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
  #5  
Old 10-20-2014, 04:40 PM
gebobs gebobs is offline Import data from CSV to table Windows 7 64bit Import data from CSV to table Office 2010 64bit
Expert
Import data from CSV to table
 
Join Date: Mar 2014
Location: Atlanta
Posts: 837
gebobs has a spectacular aura aboutgebobs has a spectacular aura about
Default

Thanks! I'll check it out next week. This week if I find any time!

*looks under chair*

Nope, none there.
Reply With Quote
Reply

Thread Tools
Display Modes


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:17 PM.


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