![]() |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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] |
|
#3
|
|||
|
|||
|
What I would like to do...
* delete the data in the table * copy the new data to the table |
|
#4
|
||||
|
||||
|
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
|||
|
|||
|
Thanks! I'll check it out next week. This week if I find any time!
*looks under chair* Nope, none there. |
|
|
|
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 |
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 |
Outlook data import
|
Cunner | Outlook | 2 | 08-20-2010 02:03 AM |