Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2012, 05:27 AM
Registered User
 
Join Date: Jun 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Import data from csv file (VBA)

Hi Friends,

Need some help on below, i am new to VBA programming & i don't know how to get this correct.I tried myself since last 3/4 days but not resloved yet.
I have one excel file contains 3 sheets, outof that 1 sheets contains the data which is linked to others 2 sheets. Data comes from csv file. I want to import the lastest csv file from specific location into same sheet on daily basis (overwriting the exisiting data). Here is my code.

Code:
Sub unhide()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If ws.Name = "DATA" Then
ws.Visible = xlSheetVisible
End If
Next ws
End Sub

Sub DeleteRows()
Dim ws As Worksheet, lRow As Long
Set ws = Worksheets("DATA")
lRow = ws.Range("A65536").End(xlUp).Row
ws.Rows("1:" & lRow).Delete Shift:=xlUp
Set ws = Nothing
End Sub

Sub Home()
Application.Goto Sheets("DATA").Range("A1")
Application.SendKeys ("^{Home}")
End Sub

Sub LoadFromFile()
Dim fileName As String, folder As String
folder = "D:\temp\Summary_Report_*.csv" '=>Note (* means date)
ActiveCell.Offset(0, 0).Range("A1").Select
With ActiveSheet.QueryTables _
.Add(Connection:="TEXT;" & folder, Destination:=ActiveCell)
.Parent.Name = "DATA"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = True
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub

Sub hide()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If ws.Name = "DATA" Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
 
Old July 2nd, 2012, 03:26 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Either you can import the latest file. This might workwell in Excel 2003 but you need to tinker it for Excel 2007 and above

Else you can do that using Dir function and have a folder that contains the latest file. You need to write a routine which places the latest file in a same file name. By doing this all you need to do is to refresh the Excel sheet and you will have the latest data

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Import .csv file and process snufse WinForms/Console Application Design 3 October 23rd, 2009 10:51 AM
import data from csv/xsl file to sql bex WinForms/Console Application Design 0 July 9th, 2009 02:34 PM
Import data to CSV itHighway Classic ASP Basics 0 September 15th, 2006 10:31 AM
Import data in CSV file itHighway Classic ASP Professional 0 September 15th, 2006 10:29 AM
import data from text file to excel(VBA) topr Excel VBA 2 August 26th, 2004 11:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.