+ Reply to Thread
Results 1 to 9 of 9

Curious, .SaveAs with xlText ignores initial empty rows

  1. #1
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Curious, .SaveAs with xlText ignores initial empty rows

    Do this in the immediate window on a fresh book.
    cells(3,1) = "A"
    cells(3,2) = "B"
    cells(4,2) = "C"
    activesheet.saveas filename:="foo", fileformat:=xlText
    As you see in foo.txt only rows 3 and 4 get saved.

    How can I beat this? cells(1)="" is not sufficient to make the .saveas pick up the first two rows.

    cells(1)=" " (a blank) does work, but that's disgusting. Any better ideas?


    One more curiosity about xlText and xlCSV that seems related: an empty row in the "middle" of the data produces nothing but 0x0D, 0x0A. To see this add this
    cells(6,1) = "D"
    to the code above and do the .SaveAs. I kind of would have liked to see the next to last output row be
    0x09, 0x0D, 0x0A
    since the other rows with nonempty cells all have a tab. Even row 6 produces a tab at the end of the row.

    The last point is easier to see with xlCSV. If any cells at all in a row are nonempty, N columns always produce N-1 commas. However no commas occur if the row has all empty cells. That might suck when parsing for commas in the .CSV; some rows would have commas, and some wouldn't. Is there any way around that behavior?
    Chip, if you can read this, you're STILL making the world a better place with your immeasurably valuable gifts. Every week, you continue to help me (and untold numbers of others) tremendously. Practically no alternative outlets can match your effectiveness, even over the vast number of topics you demystify and empower usage of. You were, and still are, amazing.

  2. #2
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Curious, .SaveAs with xlText ignores initial empty rows

    In some cell:
    = COUNT(A1:Z8)

    I haven't tried it, but I assume any REFERENCED cells will be saved.

  3. #3
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: Curious, .SaveAs with xlText ignores initial empty rows

    Nice idea, but it didn't work (FYI I used AA6). Ideally I'd like to not alter any worksheet data (which is why altering the content of A1 with " " is undesired).

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Curious, .SaveAs with xlText ignores initial empty rows

    One way
    Please Login or Register  to view this content.
    Last edited by jindon; 06-25-2017 at 01:56 AM.

  5. #5
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: Curious, .SaveAs with xlText ignores initial empty rows

    (I am so pissed. I typed a long response WITHOUT doing it in Notepad and the board lost it, so I'm reconstructing something I cobbled together and tweaked and tweaked and tweaked over a 15 minute period. "Token has expired" issues.)

    That does appear to be an effective workaround for the first problem of empty leftmost columns and/or uppermost rows. Jamming ="" into A1 doesn't produce anything but delimiters in the output file, which is good.

    As to "interior" empty rows, this process could also be effective. With row 5 empty, jamming ="" into A5 causes the normal delimiters to appear on the output for row 5 (N-1 of them). Unfortunately I'd have to walk the whole sheet to do this, and cleanup (.ClearContents or .Clear) would be a mess to manage. (Actually, since the file is saved, I suppose that I could just close the newly created .TXT and not worry about cleanup.)

    It seems ambitious but here's a thought: goto specialcells of blanks
    selection.specialcells(xlCellTypeBlanks).select
    and For Each rng in Selection, change every one to be ="", do the save, and if necessary, change them all back. One theoretical downside would be if there was a preexisting cell that actually had the content ="", it would be erroneously cleared in the cleanup.

    It seems that would produce a file with the correct number of rows, and N-1 delimiters on each row.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Curious, .SaveAs with xlText ignores initial empty rows

    That was the same as my previous idea before I have edited the code...
    Please Login or Register  to view this content.

  7. #7
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: Curious, .SaveAs with xlText ignores initial empty rows

    Very nice, and thank you - again. It's all sensible code and works.

    A couple of general technical questions follow. (Other coding zen masters feel free to pitch in )
    (Note for anyone reading along, xlBlanks=4; xlByRows=1, xlByColumns=2)

    1. Why .Copy?

    2. Why did you choose to use .Range("a1", .Cells(LR, LC)) to get to the xlBlanks instead of
    selection.specialcells(xlCellTypeBlanks) or [a1].specialcells(xlCellTypeBlanks) or activecell.specialcells(xlCellTypeBlanks) ?

    3. What is the benefit of using Find(s) to locate last occupied cell as opposed to .UsedRange.rows.count and .UsedRange.columns.count, or as opposed to xlLastCell(after "correcting it" by issuing a .UsedRange first)? (A general question that I'm going to be refreshing myself on right after I post this. I'm about to google it as I post this)

    4. I might reverse the sequence of the two finds so that SearchOrder ends up at By Rows which I normally keep it at. Actually it would be correct to save it off and restore it. So this is another general question that came to mind.

    For LookIn, LookAt, SearchOrder, and MatchByte (the ones that I understand are set and saved every time you change them),
    a. Can I inquire what the existing (prior) values are? I.e. can I ask Excel what SearchOrder was saved off before I began execution?
    b. Once I've done my finds, is the way to set them back to run a meaningless search, just to restore the parameters; or can I set them directly?

    (Maybe AMOQ, I think that they're stored in Application.FindFormat, and I can just save them off into int variables from there, and restore to there)

    5. I switch around between some of these, almost whimsically: cells(1), cells(1,1), [a1], range("a1"). It seems that you vary among those too. When I have freedom to choose from them, my "rule of thumb" is probably to choose whichever is most visually compact, though if I already have (of can cheaply get) a numeric column I'm inclined to use that instead of an alphabetic letter (that is, I avoid quote marks on the assumption that they cost time to dereference). I wonder if you have a more practical or efficiency-driven point of view.

    TIA

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Curious, .SaveAs with xlText ignores initial empty rows

    1) Nobody knows the situation that you may have multiple worksheets in the workbook.

    2, 3) Alternatively Cells.SpecialCells(xlCellTypleLastCell) might be used, however, it would refers to the cell that you don't want.
    Last cell via SpecialCells will refer to the cell that have been ever changed before save, including format.
    So, find method would give you the last row/column that have actual data.

    4)
    a) it holds the setting last changed.
    b) it is always better to set them back to default for next use.

    5) I have no rule about it, only choose whichever suits to the situation.

    HTH.

  9. #9
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: Curious, .SaveAs with xlText ignores initial empty rows

    Yes, those are helpful explanations. Thank you for your help on this.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] NxtEmpCell ignores first 4 rows
    By Nitefox in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-25-2015, 02:22 PM
  2. Curious combobox behavior - Rows and Columns get transposed
    By tfurnivall in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-05-2013, 10:47 AM
  3. [SOLVED] Formula that add time but ignores empty rows
    By jaycee66 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-24-2013, 10:01 AM
  4. Replies: 2
    Last Post: 07-11-2012, 08:58 AM
  5. Workbook.SaveAs to CSV deletes the first row if its empty.
    By arunnn in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 04-07-2011, 09:10 PM
  6. Fileformat XLText
    By JeepNC in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-14-2005, 03:06 PM
  7. workbook saveas function, xlText file format
    By Massimo Bassini in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-26-2005, 01:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1