+ Reply to Thread
Results 1 to 24 of 24

Macro for save as as TXT

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Macro for save as as TXT

    Hi,

    I have use a macro to general text and add rows, but I would like to macro to also Save the result from excel to a text file. Can anyone help me to be able to automatically generate the .txt file into same directory after running the macro?
    Also, when applying the first macro, is it possible to also do:
    If character count is not 9 character, then add "_" underscore to data until it reaches 9 characters?

    Here is the macro and the final text file that should be like;
    HELP.xlsm

    HELP.txt
    Thanks anyone for help.
    Last edited by calvinle; 03-13-2014 at 11:56 AM.

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    Sub GenerateCode()
    Dim WS As Worksheet
    Dim I As Long
    Dim LastRow As Long
    Dim FF As Integer
    Dim Temp$
    
        Set WS = ActiveSheet
    
        FF = FreeFile
    
        Open ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ".txt" For Output As #FF
    
        Print #FF, "[HEADER]"
        Print #FF, "LANGUAGE=SCRIPT"
        Print #FF, "DESCRIPTION="
        Print #FF, "[SOURCE]"
        Print #FF, "OPTION"
        Print #FF, "autoadd.Name" & Chr(40) & "Work" & Chr(41)
        Print #FF, ""
        Print #FF, "REC Macro starts here"
        Print #FF, "sub_"
        Print #FF, ""
        Print #FF, "sub sub_" & Chr(40) & Chr(41)
        Print #FF, ""
        Print #FF, ""
    
        With WS
            For I = 1 To .Range("A" & .Rows.Count).End(3)(1).Row
                Temp$ = " " & Left(.Range("A" & I) & "_________", 9)
                Print #FF, "   autoadd.syntax.key " & Chr(34) & Temp$ & vbTab & Chr(34) & ", 23,47"
                Print #FF, "   autoadd.syntax.key " & Chr(34) & "[press]" & Chr(34) & vbTab & vbTab
                Print #FF, "   autoadd.syntax.key " & Chr(34) & "[word]" & Chr(34) & vbTab & vbTab
                Print #FF, "   autoadd.syntax.Wait 700" & vbTab & vbTab
            Next I
        End With
    
        Close #FF
    End Sub
    David
    (*) Reputation points appreciated.

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,772

    Re: Macro for save as as TXT

    Nice solution Tinbendr. This is better than building the file as a spreadsheet then saving it as .txt. If you try to save it as .txt, then Excel will put quotes around any fields containing a comma, even if you are saving it as tab-delimited.

    calvinle, why are you using Chr codes for these characters: " ( )
    Seems like the code would be more readable if you just used those characters.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi,

    @6StringJazzer: That excel will generate a code that will be useful for me to use in another platform. That is why I need the character " ( ). In the generated macro using excel, vba does not accept " ( ) in the vba code, it has to be char(x)...isnt it?

    @Tinbendr : Generating directly from excel will create those quote around field with comma, and I though I wud have complication about the vbtab but you even add it too. Thank you very much! You really help me!! Thanks again and very very much!

  5. #5
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi,

    The macro works perfectly, however, I would like to know the possibility to add more $Temp from other cell to the line.
    If I have data in cell B, C, D, E, and I want them to be include in some parameter of the result, how should I process it?
    Here is an example of result that I want it to be..

    HELP.xlsmHELP.xlsm.txt

    Thanks.

  6. #6
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    Sub GenerateCode()
    Dim WS As Worksheet
    Dim I As Long, J As Long
    Dim LastRow As Long, LCol As Long
    Dim FF As Integer
    Dim Temp$, Temp2$
    
        Set WS = ActiveSheet
    
        FF = FreeFile
    
        Open ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ".txt" For Output As #FF
    
    
        Print #FF, "[HEADER]"
        Print #FF, "LANGUAGE=SCRIPT"
        Print #FF, "DESCRIPTION="
        Print #FF, "[SOURCE]"
        Print #FF, "OPTION"
        Print #FF, "autoadd.Name" & Chr(40) & "Work" & Chr(41)
        Print #FF, ""
        Print #FF, "REC Macro starts here"
        Print #FF, "sub_"
        Print #FF, ""
        Print #FF, "sub sub_" & Chr(40) & Chr(41)
        Print #FF, ""
        Print #FF, ""
    
        With WS
            For I = 1 To .Range("A" & .Rows.Count).End(3)(1).Row
                LCol = .Cells(I, .Columns.Count).End(xlToLeft).Column
                Temp2$ = ""
                If LCol > 1 Then
                    For J = 2 To .Cells(I, .Columns.Count).End(xlToLeft).Column
                        Temp2$ = Temp2$ & .Cells(I, J)
                    Next
                End If
                
                Temp$ = " " & Left(.Range("A" & I) & "_________", 9)
                Print #FF, "   autoadd.syntax.key " & Chr(34) & Temp$ & vbTab & Chr(34) & ", 23,47"
                Print #FF, "   autoadd.syntax.key " & Chr(34) & "[press]" & Temp2$ & Chr(34) & vbTab & vbTab
                Print #FF, "   autoadd.syntax.key " & Chr(34) & "[word]" & Chr(34) & vbTab & vbTab
                Print #FF, "   autoadd.syntax.Wait 700" & vbTab & vbTab
            Next I
        End With
    
        Close #FF
    End Sub

  7. #7
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Ur fantastic!! Thank you so much!!

  8. #8
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi,

    Is it possible to add more field to the script? or can you explain how this really work on adding more Temp$?
    I want to add more detail to the scripting such as name, etc. But whenever I need it, I will just associate a Temp#$ to the script. I have tried to change so it starts by A5 because my new data started by A5 but it doesnt work.
    Here is the example:

    Example
    HELP.xlsm
    Result required
    HELP.txt

    Also, this file will be saved in the same folder, but what if I want to force it to be save in C:\FILE (if no folder if FILE, it will make one)

    A really big thanks for the help..

  9. #9
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    Just use a cell reference in the data list. So, in B7 enter =C1.

    Change the number one to whatever the data starting row you want.

    For I = 1 To .Range("A" & .Rows.Count).End(3)(1).Row
    Then the cell will be handled by Temp2$.

  10. #10
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    I guess I figure it out
                Temp3$ = (.Range("B2"))

  11. #11
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Sorry, I did not clearly explain the result. I want it to be included in each line of the generated result.

    Sub Generate_Code()
    Dim WS As Worksheet
    Dim I As Long
    Dim LastRow As Long
    Dim FF As Integer
    Dim Temp$, Temp$2, Temp$3
    
        Set WS = ActiveSheet
    
        FF = FreeFile
    
        Open ActiveWorkbook.Path & "\" & ActiveWorkbook.NAME & ".mac" For Output As #FF
    
        Print #FF, "[PCOMM SCRIPT HEADER]"
        Print #FF, "LANGUAGE=VBSCRIPT"
        Print #FF, "DESCRIPTION="
        Print #FF, "[PCOMM SCRIPT SOURCE]"
        Print #FF, "OPTION EXPLICIT"
        Print #FF, "autECLSession.SetConnectionByName" & Chr(40) & "ThisSessionName" & Chr(41)
        Print #FF, ""
        Print #FF, "REM This line calls the macro subroutine"
        Print #FF, "subSub1_"
        Print #FF, ""
        Print #FF, "sub subSub1_" & Chr(40) & Chr(41)
        Print #FF, ""
        Print #FF, ""
    
        With WS
            For I = 5 To .Range("A" & .Rows.Count).End(3)(1).Row
                LCol = .Cells(I, .Columns.Count).End(xlToLeft).Column
                Temp2$ = ""
                If LCol > 1 Then
                    For J = 2 To .Cells(I, .Columns.Count).End(xlToLeft).Column
                        Temp2$ = Temp2$ & .Cells(I, J)
                    Next
                End If
    
                Temp$ = Left(.Range("A" & I) & "_________", 9)
                Print #FF, "   autECLSession.autECLPS.SendKeys " & Chr(34) & Temp$ & Chr(34) & ", 23,47"
                Print #FF, "   autECLSession.autECLPS.SendKeys " & Chr(34) & "[enter]" & Temp2$ & Chr(34)
                Print #FF, "   autECLSession.autECLPS.SendKeys " & Chr(34) & "[printps]" & Temp3$ & Chr(34)
                Print #FF, "   autECLSession.autECLPS.Wait 700"
            Next I
        End With
        Print #FF, ""
        Print #FF, "end sub "
        Print #FF, ""
        Close #FF
    End Sub
    But the Temp3$, I want to associate it to the cell B2.
    So I can add Temp4$ to B3, and so on..

    Thanks.

  12. #12
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi,

    Another question about using this macro is about date.

    I am currently adding a date to a cell, and I would like to know when exporting to text, it becomes:
    28/03/2014, but I would like to make it become: 28MAR2014 instead. I have formatted the cell itself, but when exporting to text, it's still in 28/03/2014. How to force the cell to be in that DDMMMYYYY?

    I am using this code:
                PROCESS_DATE$ = (.Range("B3"))
    What should I add to this code to force the format date?

    Thanks.

  13. #13
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    PROCESS_DATE$ = Format(.Range("B3", "ddmmmyyyy")
    The extra parentheses around the range is unnecessary.

  14. #14
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    I did try this, but I didnt get any result, but now it's working. However, the date in B3 is:
    28mar2014, but when exporting to text, it become 28Mar1487?? Why is that?

  15. #15
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Sorry, nevermind, was inputting the wrong format code MMDDDYYYY instead of DDMMMYYYY. My bad..

  16. #16
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    Yep, that'll do it.

    Working with dates in Excel can be a headache. Many Excel MVP's have written extensively on the subject. Here's a link to one of my favorites.

  17. #17
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi Tinbendr,

    I have changed the save location of the script to be save in C:\MACRO, but when the folder does not exist, the macro won't work. How can I mod this part to make it prompt up to ask where to save if the designate folder cannot be found?

    Thanks.

  18. #18
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    Something like:

    'From Norie
    Dim rspCreate 
    If Dir("C:/WINDOWS/MyDirectory/", vbDirectory) = "" Then 
        rspCreate = MsgBox("Directory doesn't exist, do you wish to create it?", vbYesNo) 
         
        If rspCreate = vbYes Then 
            MkDir "C:/WINDOWS/MyDirectory/" 
        End If 
    End If

  19. #19
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi,

    Sorry for asking again, but what if this time I want to do this way, what should I modify in the code to associate each cell right beside the Temp1$ separately?

    Sub GenerateCode()
    Dim WS As Worksheet
    Dim I As Long
    Dim LastRow As Long
    Dim FF As Integer
    Dim Temp$
    
        Set WS = ActiveSheet
    
        FF = FreeFile
    
        Open ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ".txt" For Output As #FF
    
        Print #FF, "[HEADER]"
        Print #FF, "LANGUAGE=SCRIPT"
        Print #FF, "DESCRIPTION="
        Print #FF, "[SOURCE]"
        Print #FF, "OPTION"
        Print #FF, "autoadd.Name" & Chr(40) & "Work" & Chr(41)
        Print #FF, ""
        Print #FF, "REC Macro starts here"
        Print #FF, "sub_"
        Print #FF, ""
        Print #FF, "sub sub_" & Chr(40) & Chr(41)
        Print #FF, ""
        Print #FF, ""
    
        With WS
            For I = 1 To .Range("A" & .Rows.Count).End(3)(1).Row
                Temp$ = " " & Left(.Range("A" & I) & "_________", 9)
                Print #FF, "   autoadd.syntax.key " & Chr(34) & Temp$ & vbTab & Chr(34) & ", 23,47"
                Print #FF, "   autoadd.syntax.key " & Chr(34) & "[press]Temp2$" & Chr(34) & vbTab & vbTab
                Print #FF, "   autoadd.syntax.key " & Chr(34) & "[word]Temp3$" & Chr(34) & vbTab & vbTab
                Print #FF, "   autoadd.syntax.key " & Chr(34) & "Temp4$" & Chr(34) & vbTab & vbTab
                Print #FF, "   autoadd.syntax.Wait 700" & vbTab & vbTab
            Next I
        End With
        Print #FF, ""
        Print #FF, "end sub "
        Print #FF, ""
        Close #FF
    End Sub
    HELP.xlsmHELP.xlsm.txt

    Thanks..

  20. #20
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    I'm not understanding what you need.

    In #5, you asked that all columns from B to the end, be concatenated and inserted on the [press] line.

    I answered that with #6.

    Now you want more info added to the next line? Where does this data come from?

    Or are you wanting to breakout the data across each line?

    Col A Col B Col C Col D
    3198 MOUSE CAT ELEPHANT

    Temp$ = Col B
    Temp2$ = Col C
    Temp3$ = Col D
    Temp4$= ???
    Last edited by Tinbendr; 04-08-2014 at 08:51 AM.

  21. #21
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi. Exactly. I want to breakout the data across each line. Because I realise that instead of concatenate, I can just out them all together in 1 cell instead.

    I want to breakout exactly as you stated in ur topic.

    Thanks!

  22. #22
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    OK, try this.
    Sub GenerateCode2()
    Dim WS As Worksheet
    Dim I As Long, J As Long
    Dim LastRow As Long, LCol As Long
    Dim FF As Integer
    Dim Temp$
    
        Set WS = ActiveSheet
    
        FF = FreeFile
    
        Open ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ".txt" For Output As #FF
    
    
        Print #FF, "[HEADER]"
        Print #FF, "LANGUAGE=SCRIPT"
        Print #FF, "DESCRIPTION="
        Print #FF, "[SOURCE]"
        Print #FF, "OPTION"
        Print #FF, "autoadd.Name" & Chr(40) & "Work" & Chr(41)
        Print #FF, ""
        Print #FF, "REC Macro starts here"
        Print #FF, "sub_"
        Print #FF, ""
        Print #FF, "sub sub_" & Chr(40) & Chr(41)
        Print #FF, ""
        Print #FF, ""
    
        With WS
            For I = 1 To .Range("A" & .Rows.Count).End(3)(1).Row
                Temp$ = " " & Left(.Range("A" & I) & "_________", 9)
                Print #FF, "   autoadd.syntax.key " & Chr(34) & Temp$ & vbTab & Chr(34) & ", 23,47"
                
                LCol = .Cells(I, .Columns.Count).End(xlToLeft).Column
                If LCol > 1 Then
                    Print #FF, "   autoadd.syntax.key " & Chr(34) & "[press]" & .Range("B" & I) & Chr(34) & vbTab & vbTab
                    Print #FF, "   autoadd.syntax.key " & Chr(34) & "[word]" & .Range("C" & I) & Chr(34) & vbTab & vbTab
                    
                    If LCol > 3 Then
                        For J = 4 To LCol
                            Print #FF, "   autoadd.syntax.key " & Chr(34) & .Cells(I, J) & Chr(34) & vbTab & vbTab
                        Next
                    End If
                End If
                
                Print #FF, "   autoadd.syntax.Wait 700" & vbTab & vbTab
            Next I
        End With
    
        Close #FF
    End Sub

  23. #23
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    It is possible to not use IF in this function?

    My data will all have Column A, B, C, D and E, so I think we can eliminate that IF function no?
    A B C D E
    334 DOG CAT COX CHILD
    243 CAT DOG CHILD COX
    322 COX CAT CHILD DOG

    What do you think?

  24. #24
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    I mod it to this way and it seems to work, but can you check if it's good still?

    Sub GenerateCode2()
    Dim WS As Worksheet
    Dim I As Long, J As Long
    Dim LastRow As Long, LCol As Long
    Dim FF As Integer
    Dim Temp$
    
        Set WS = ActiveSheet
    
        FF = FreeFile
    
        Open ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ".txt" For Output As #FF
    
    
        Print #FF, "[HEADER]"
        Print #FF, "LANGUAGE=SCRIPT"
        Print #FF, "DESCRIPTION="
        Print #FF, "[SOURCE]"
        Print #FF, "OPTION"
        Print #FF, "autoadd.Name" & Chr(40) & "Work" & Chr(41)
        Print #FF, ""
        Print #FF, "REC Macro starts here"
        Print #FF, "sub_"
        Print #FF, ""
        Print #FF, "sub sub_" & Chr(40) & Chr(41)
        Print #FF, ""
        Print #FF, ""
    
        With WS
            For I = 1 To .Range("A" & .Rows.Count).End(3)(1).Row
                Temp$ = " " & Left(.Range("A" & I) & "_________", 9)
                Temp2$ = .Range("B" & I)
                Temp3$ = .Range("C" & I)
                Temp4$ = .Range("D" & I)           
                    Print #FF, "   autoadd.syntax.key " & Chr(34) & "[press]" & Temp2$ & Chr(34) & vbTab & vbTab
                    Print #FF, "   autoadd.syntax.key " & Chr(34) & "[word]" & Temp3$ & Chr(34) & vbTab & vbTab
                    Print #FF, "   autoadd.syntax.key " & Chr(34) & Temp4$ & Chr(34) & vbTab & vbTab
                    Print #FF, "   autoadd.syntax.Wait 700" & vbTab & vbTab
            Next I
        End With
    
        Close #FF
    End Sub

+ 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. how to specify a file save path to a save macro
    By Solidstan in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-04-2013, 05:33 PM
  2. Macro to disable SAVE and SAVE AS
    By Charmymay in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-04-2013, 10:06 AM
  3. Macro with cell reference as a name, but prompt for save location and save as csv
    By tomham in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-15-2012, 06:21 PM
  4. [SOLVED] Macro to save sheets as different workbooks on save
    By BridgeCat in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-09-2012, 08:10 AM
  5. where can i find macro codes for various functions such print, save and save as
    By fellayaboy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-23-2011, 02:06 AM

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