LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default rows.count property returns 2 even though only 1 row was imported by querytables.add

On Saturday, 9 June 2018 18:44:04 UTC+10, GS wrote:
Does your query table include a headers row?


Thanks for your follow ups.

This example is trimmed down to the barest of the bare essentials - the
destination worksheet is bare (no headers) and the file is pure text (not
even CSV), so .Add is just plonking the file contents straight into $A.

The cut-and-paste-ready VBA fragment is:

csvFike = "c:\temp\x.txt'
Set qrytable = ActiveSheet.QueryTables.ADD(Connection:="TEXT;" + csvFile,
Destination:=Cells(1,1))

With qrytable
.Refresh BackgroundQuery:=False
nValues = .ResultRange.Rows.Count
End With

While I certainly see what you're sayin', that's not what I'm seein' - if the
file contains only a string _without_ a CR/LF, .Count nonetheless returns 2:
it's as though it's silently assumed the trailing CR/LF anyway.

Yes I suppose bypassing .Add in favor of direct VBA I/O is the way to go but
it's a simple interface that works as expected save for this pestiferous
corner case. As it is I've since used the following Truly Excruciating
Workaround (quite acceptable for the application):

If (nValues = 2) Then
(WorksheetFunction.CountA(Rows(startrow + 1)) = 0) Then nValues = 1
End If

(Moreover, .Add also throws error 7 "Out of memory" if the file consists of
two or less (not just zero) characters with no trailing CR/LF.)


Ok! IMO, choosing an approach that doesn't work consistently and coorectly all
the time just isn't a viable option regardless of its simplicity!

Using VBA file I/O doesn't throw errors unless the file doesn't exist (using a
file dialog obviates that); AND you'll always get the correct record count (and
row count if no headers in the file).

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
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
Sum, Average, and Count from imported .txt file sdhi Excel Worksheet Functions 2 October 18th 09 03:44 PM
logic that returns a count d_kight Excel Discussion (Misc queries) 8 May 18th 06 12:30 AM
Some worksheet returns NULL for CodeName property. Why? Shigeo Morita Excel Programming 4 December 3rd 04 11:09 PM
Count Property jacqui Excel Programming 2 April 6th 04 11:40 AM


All times are GMT +1. The time now is 02:51 PM.

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

About Us

"It's about Microsoft Excel"