+ Reply to Thread
Results 1 to 9 of 9

Saving data selection as tab seperated txt file

  1. #1
    Registered User
    Join Date
    09-06-2012
    Location
    Denmark
    MS-Off Ver
    365 ProPlus
    Posts
    20

    Smile Saving data selection as tab seperated txt file

    First post here, be nice So I've been fighting for a while with this and finally have something worth showing and asking for help with.

    Task: Export the values in columns A:C to a tab seperated .txt file, then resave the workbook.

    So far reading around and looking things up I've got as far as the code below. The specific problem I have now is how to control the temporary sheet i need to hold the values while i save the workbook as a txt file. I've tried reusing the sheet 'For Keynote Export' but it just gets renamed after export each time, so it never works the next time. I've tried adding a new temporary sheet, but haven't been able to find a reliable way to select the newly created sheet to give it a name. Sometimes it gets called 'Sheet2' and sometimes 'Sheet3' or whatever. I can't seem to create and name it in one go.

    This is the start of a long journey for me, so feel free to redirect me to things I should have read or totally different ways of solving this task.

    Please Login or Register  to view this content.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,410

    Re: Saving data selection as tab seperated txt file

    First post here, be nice
    Third post here, actually: https://www.excelforum.com/excel-gen...t-filters.html

    Are you still using Excel 2010? If not, please update your profile accordingly. Thanks.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Saving data selection as tab seperated txt file

    Would you also clarify. Are you copying columns A:C from one sheet onto the "For Keynote Export" sheet and then wanting to save just that keynote export sheet as a single sheet additional workbook file?

    If so what name are you trying to give to the saved file? At the moment it's being given the same name as the file with which you start.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    09-06-2012
    Location
    Denmark
    MS-Off Ver
    365 ProPlus
    Posts
    20

    Re: Saving data selection as tab seperated txt file

    Updated my Excel version, thanks AliGW

    ... yes, but saving as a tab seperated csv txt file. Then resaving the original workbook again so I can keep working in it. The keynote file is being read by a different piece of software (Autodesk Revit) and gets reloaded often. Maybe there's a better way, but I couldn't find an export function.

    Quote Originally Posted by Richard Buttrey View Post
    At the moment it's being given the same name as the file with which you start.
    That's correct.

  5. #5
    Registered User
    Join Date
    09-06-2012
    Location
    Denmark
    MS-Off Ver
    365 ProPlus
    Posts
    20

    Re: Saving data selection as tab seperated txt file

    Quote Originally Posted by AliGW View Post
    Third post here, actually
    Does it still count if I found the solution myself? Let's say it's the first time I *really* need help.

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Saving data selection as tab seperated txt file

    Perhaps the following.

    The assumption is that the stuff you want to copy to the Keynote export sheet can be from any other sheet. Hence the code sets a reference to the sheet with the Set wsInitialSheetName syntax. Of course if it's always copied from the same sheet then there's no need for that.

    You could simply replace the
    wsInitialSheet.Columns("A:C").Copy Sheet2.Range("A1")

    with the VBA sheet code name - let's assume that's Sheet1. Hence the line of code would be

    Sheet1.Columns("A:C").Copy Sheet2.Range("A1")

    Note that I can't see any need for the SaveAs at the end unless you really do want to change the file name so I've commented it out.

    Finally, rarely is it necessary to use code like .Select or .Activate. That just slows macros down, particularly when you may be using a loop to do the same stuff many times. Just reference objects directly. In addition it's regarded as good practice to put all Dim statements at the top of any code and always define the type of variable with the 'As ...whatever'. e.g. As String, As Long, As Workbook...


    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    09-06-2012
    Location
    Denmark
    MS-Off Ver
    365 ProPlus
    Posts
    20

    Re: Saving data selection as tab seperated txt file

    Thanks Richard for that

    So let's see if I'm getting this. You got around my problems of creating a new sheet by not specifying before/after so it just makes a new workbook containing .Columns("A:C"). Because I wasn't doing that when I saved I was effectively renaming my workbook to ...txt which is why I was saving it again as .xlsm

    When that temporary workbook closes we're back in our original workbook, and I guess the extra ActiveWorkbook.Save is really not needed. I've removed it.

    How come you can set .CutCopyMode = False _after_ you've done the cut/copy? isn't that too late (I can see it works I just don't understand)

    I've removed sWorkbookPath = ThisWorkbook.Path as I can see it's not needed. I've decided to suppress the overwrite warning, but I guess it's good form to turn it on again as soon as possible.

    So this is the current code. Much tidier than I started with.

    Please Login or Register  to view this content.
    Thanks for your help. This is working great

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Saving data selection as tab seperated txt file

    Hi,

    It's the line
    wsInitialSheet.Columns("A:C").Copy Sheet2.Range("A1")

    that copies the stuff to the Keynote sheet in one action, unlike a Copy (into memory) and then Paste as your original was doing. AT that point there is nothing in memory.

    Similarly the SHeet2.Copy is a single action, and thinking a bit more about this the CutCopyMode = False isn't required since there is nothing in memory either

    Yes you are correct. At the end you are still in the original workbook. I left the Save in since you may feel you want an up to date copy that represents the precise position when you saved the .txt file. You can leave out the Save if you wish.

    You're also correct that it's generally good policy to reverse an instruction like Display Alerts as soon as possible unless there are reasons not to. The same thinking applies with stuff like On Error REsume Next....On Error Goto 0, Application.Calculation = xlCalculationManual... Application.Calculation =xlAutomatic, ...ScreeUpdating = False....ScreenUpdating = True, and no doubt several others

  9. #9
    Registered User
    Join Date
    09-06-2012
    Location
    Denmark
    MS-Off Ver
    365 ProPlus
    Posts
    20

    Re: Saving data selection as tab seperated txt file

    So I got some help to make the file portable by switching from ThisWorkbook to ActiveWorkbook. But now it's not working again and stops at wsInitialSheet.Columns("A:C").Copy Sheet2.Range("A1")

    Please Login or Register  to view this content.
    Last edited by Duncan Lithgow; 11-02-2020 at 05:28 PM.

+ 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. Saving User Form Data to Row Based on Combo Box Selection
    By Skurski84 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-04-2020, 01:09 PM
  2. [SOLVED] Saving selection with values separated by ; as CSV file in one column
    By drrazor in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-21-2014, 10:52 AM
  3. Breakdown Cells with values seperated by ; to row based on combobox selection
    By dooz1990 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-14-2013, 04:44 PM
  4. Saving selection from data validation list to table in different worksheet using VBA
    By anonDymous in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-13-2013, 01:38 PM
  5. Creating a comma seperated file
    By scotthomas in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-06-2012, 03:46 PM
  6. saveas (;) seperated csv file [Problem]
    By Murat Demir Hekimoðlu in forum Excel General
    Replies: 3
    Last Post: 07-20-2005, 08:05 AM
  7. [SOLVED] How do I change *.CSV excel file to column seperated file?
    By Pankaj in forum Excel General
    Replies: 2
    Last Post: 07-05-2005, 03:05 PM

Tags for this Thread

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