Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,560
Default QueryTable import keeps appending instead of overwriting.

Hi,
I'm using Excel 2003 sp3. I've written a vba macro to import text
from a text file :

Sub Create_excel()
Dim WS As Worksheet
Dim QT As QueryTable

Set WS = Worksheets("Sheet1")
Set QT = WS.QueryTables.Add("TEXT;C:\temp\tmp.csv", WS.Range("A1"))
QT.TextFileSpaceDelimiter = True
QT.Refresh 'BackgroundQuery:=False
QT.Delete
End Sub

The first time I run it it runs fine. But the next time the data gets
appended adjacent to the last cell that was written on the previous run, and
not A1. Also even if I modify the text file and and re-run the changes don't
take effect. Can anyone help ?

Thanks.
-david.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,805
Default QueryTable import keeps appending instead of overwriting.

If there is nothing else which you want to retain on the sheet then use
I have just added the statement
Worksheets("Sheet1").UsedRange.ClearContents

'-------------------
Sub Create_excel()
Dim WS As Worksheet
Dim QT As QueryTable
Worksheets("Sheet1").UsedRange.ClearContents
Set WS = Worksheets("Sheet1")
Set QT = WS.QueryTables.Add("TEXT;C:\Documents and
Settings\Vinamra\Desktop\T1.txt", WS.Range("A1"))
QT.TextFileSpaceDelimiter = True
QT.Refresh 'BackgroundQuery:=False
QT.Delete
End Sub

"David" wrote:

Hi,
I'm using Excel 2003 sp3. I've written a vba macro to import text
from a text file :

Sub Create_excel()
Dim WS As Worksheet
Dim QT As QueryTable

Set WS = Worksheets("Sheet1")
Set QT = WS.QueryTables.Add("TEXT;C:\temp\tmp.csv", WS.Range("A1"))
QT.TextFileSpaceDelimiter = True
QT.Refresh 'BackgroundQuery:=False
QT.Delete
End Sub

The first time I run it it runs fine. But the next time the data gets
appended adjacent to the last cell that was written on the previous run, and
not A1. Also even if I modify the text file and and re-run the changes don't
take effect. Can anyone help ?

Thanks.
-david.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,560
Default QueryTable import keeps appending instead of overwriting.

Hi Sheeloo,
Thanks for the reply. Yes that works. It wasn't really appending - instead
it was always writing to A1 but the previous data was being pushed to the
right.
Also I found that the refresh does take place
when I update the file (I was referring to the wrong file :( ).

"Sheeloo" wrote:

If there is nothing else which you want to retain on the sheet then use
I have just added the statement
Worksheets("Sheet1").UsedRange.ClearContents

'-------------------
Sub Create_excel()
Dim WS As Worksheet
Dim QT As QueryTable
Worksheets("Sheet1").UsedRange.ClearContents
Set WS = Worksheets("Sheet1")
Set QT = WS.QueryTables.Add("TEXT;C:\Documents and
Settings\Vinamra\Desktop\T1.txt", WS.Range("A1"))
QT.TextFileSpaceDelimiter = True
QT.Refresh 'BackgroundQuery:=False
QT.Delete
End Sub

"David" wrote:

Hi,
I'm using Excel 2003 sp3. I've written a vba macro to import text
from a text file :

Sub Create_excel()
Dim WS As Worksheet
Dim QT As QueryTable

Set WS = Worksheets("Sheet1")
Set QT = WS.QueryTables.Add("TEXT;C:\temp\tmp.csv", WS.Range("A1"))
QT.TextFileSpaceDelimiter = True
QT.Refresh 'BackgroundQuery:=False
QT.Delete
End Sub

The first time I run it it runs fine. But the next time the data gets
appended adjacent to the last cell that was written on the previous run, and
not A1. Also even if I modify the text file and and re-run the changes don't
take effect. Can anyone help ?

Thanks.
-david.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,805
Default QueryTable import keeps appending instead of overwriting.

Thanks for the feedback. I also did not realize that it was pushing the
existing data to the right...

"David" wrote:

Hi Sheeloo,
Thanks for the reply. Yes that works. It wasn't really appending - instead
it was always writing to A1 but the previous data was being pushed to the
right.
Also I found that the refresh does take place
when I update the file (I was referring to the wrong file :( ).

"Sheeloo" wrote:

If there is nothing else which you want to retain on the sheet then use
I have just added the statement
Worksheets("Sheet1").UsedRange.ClearContents

'-------------------
Sub Create_excel()
Dim WS As Worksheet
Dim QT As QueryTable
Worksheets("Sheet1").UsedRange.ClearContents
Set WS = Worksheets("Sheet1")
Set QT = WS.QueryTables.Add("TEXT;C:\Documents and
Settings\Vinamra\Desktop\T1.txt", WS.Range("A1"))
QT.TextFileSpaceDelimiter = True
QT.Refresh 'BackgroundQuery:=False
QT.Delete
End Sub

"David" wrote:

Hi,
I'm using Excel 2003 sp3. I've written a vba macro to import text
from a text file :

Sub Create_excel()
Dim WS As Worksheet
Dim QT As QueryTable

Set WS = Worksheets("Sheet1")
Set QT = WS.QueryTables.Add("TEXT;C:\temp\tmp.csv", WS.Range("A1"))
QT.TextFileSpaceDelimiter = True
QT.Refresh 'BackgroundQuery:=False
QT.Delete
End Sub

The first time I run it it runs fine. But the next time the data gets
appended adjacent to the last cell that was written on the previous run, and
not A1. Also even if I modify the text file and and re-run the changes don't
take effect. Can anyone help ?

Thanks.
-david.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
QueryTable Null Import Quirk / Problem Mike Iacovou Excel Programming 0 July 20th 07 10:38 PM
Import data using QueryTable somehow change sheet cells references JackCali Excel Programming 2 April 6th 07 05:34 PM
overwriting formulas enyaw Excel Programming 0 June 6th 06 01:30 PM
Changing only source file of pre-existing text import QueryTable? EBrowne Excel Programming 3 August 23rd 04 03:31 AM
Using Querytable.add to import data from a closed workbook Markus Stolle[_5_] Excel Programming 8 August 3rd 04 08:51 PM


All times are GMT +1. The time now is 09:46 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"