PDA

View Full Version : [SOLVED:] VBA import from csv - copy into existing cells rather than insert new



torquil
03-17-2020, 08:20 AM
Hi Guys,
I have a code that pulls through a csv file and imports the data into a sheet named "test".
what I want it to do is copy the data into the exisitng cells rather than adding new cells witht the data in.


Dim URL As String Dim destCell As Range

URL = "https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1426550400&period2=1584403200&interval=1d&events=history"

Set destCell = Worksheets("test").Range("A1")

With destCell.Parent.QueryTables.Add(Connection:="TEXT;" & URL, Destination:=destCell)
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh BackgroundQuery:=False
End With

destCell.Parent.QueryTables(1).Delete


End Sub



My goal with this is so i can have a set of 5 or 6 tables of data on a single sheet that will update depending on user search box.

p45cal
03-18-2020, 04:14 PM
try adding the red line:
With destCell.Parent.QueryTables.Add(Connection:="TEXT;" & URL, Destination:=destCell)
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.RefreshStyle = xlOverwriteCells
.Refresh BackgroundQuery:=False
End With

snb
03-19-2020, 04:15 AM
Sub M_snb()
URL = "TEXT;https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1426550400&period2=1584403200&interval=1d&events=history"

with sheets("test")
if .Querytables.count=0 Then .QueryTables.Add URL, .cells(1)
.Querytables(1).refresh False
end with
End Sub

torquil
03-24-2020, 07:40 AM
Perfect thanks guys.