Results 1 to 8 of 8

Thread: Generate TEXT Files from a Control File?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    744
    Location
    It can be made 'cooler' as below:
    [vba]Option Explicit
    Dim i As Integer
    Const csPath As String = "C:\Copy.bat"
    Dim lLastRow As Long
    Private Sub RunApp_Click()
    With Sheet1
    lLastRow = .Range("A" & Rows.Count).End(xlUp).Row
    For i = 4 To lLastRow
    .Range("E" & i).FormulaR1C1 = _
    "=""copy ""&""""""""&RC[-4]&""\""&RC[-3]&""""""""&"" ""&""""""""&RC[-2]&""\""&RC[-1]&"""""""""
    Next i
    End With
    CreateBatFile
    MsgBox "Files are copied to desired location!"
    Kill csPath
    End Sub
    Private Sub CreateBatFile()
    Dim FSO As Object, Writer As Object
    'Creating a DOS file
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Writer = FSO.CreateTextFile(csPath, True)
    'This help prevents DOS NOT to show the command
    Writer.WriteLine ("@ echo off")
    Writer.Close
    With Sheet1
    lLastRow = .Range("A" & Rows.Count).End(xlUp).Row
    Set Writer = FSO.OpenTextFile(csPath, 8)
    For i = 4 To lLastRow
    Writer.WriteLine (.Range("E" & i).Value)
    Next i
    Writer.Close
    End With
    RunBatFile
    End Sub
    Private Sub RunBatFile()
    Dim RunBat
    'Running the bat file from excel
    RunBat = Shell(csPath, 0)
    'Cleanup
    With Sheet1
    .Range("E4:E" & lLastRow).ClearContents
    End With
    End Sub
    [/vba]

    I am attaching the revised Excel file which has a button. One word (sentence maybe ) of caution: paste the path as it is like:
    C:\MyPath and not C:\Mypath\
    And it will perform the above mentioned manual steps.

    Kenneth's solution is really good. I just did this out of curiosity!

    PS Edit: I am also learning VBA just because I like it. There are two books that I've been referring to for quite a while:
    VBA and Macros for Microsoft Excel by Bill Jelen Aka MrExcel
    Excel 2007 VBA Programmer's Reference by John Green, Stephen Bullen, Rob Bovey and Michael Alexander (I saw a recommendation on XLD's website for its earlier edition)
    Attached Files Attached Files
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

Posting Permissions

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