+ Reply to Thread
Results 1 to 2 of 2

Vba output to a batch file

Hybrid View

  1. #1
    Registered User
    Join Date
    04-10-2014
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    39

    Vba output to a batch file

    Hi,
    I wrote a vba script that makes a batch file in column A in excel.
    Can you please tell me how I can write out column A to a batch file.
    I don't want to do a save as since I want to keep my excel program open for other processes.
    Thanks you

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Vba output to a batch file

    Hi learner99,

    The following macro will help you get started. It creates a .bat file using text from Column 'A' (starting at row 11) in the Active Sheet. You should be able to adapt it to your needs. Also see the attached sample file which implements the macro.
    Option Explicit
    
    Sub CreateDotBatFileFromDataInColumnA()
    
      Dim iFileNo As Long
      Dim iFirstRow As Long
      Dim iLastRow As Long
      Dim iRow As Long
      
      Dim bNeedMore As Long
      
      Dim sDataLine As String
      Dim sFileName As String
      Dim sFolder As String
      Dim sPathAndFileName As String
      
      
      '''''''''''''''''''''''''''''''''''''''''''''
      'Preparation for Export
      '''''''''''''''''''''''''''''''''''''''''''''
     
      'Create the Folder and File Name Combination from the Worksheet
      'Use the folder that contains the file that is running this code
      'Create an arbitrary file name
      sFolder = ThisWorkbook.Path
      sFileName = "abc.bat"
      
      'Make sure the Folder has a trailing BACKSLASH
      If Right(sFolder, 1) <> "\" Then
        sFolder = sFolder & "\"
      End If
    
      'Build the Path and File Name Combination
      sPathAndFileName = sFolder & sFileName
      
      'Verify that the file to be exported DOES NOT exist
      If LJMFileExists(sPathAndFileName) = True Then
        MsgBox "NOTHING DONE.  File EXISTS." & vbCrLf & _
               "Arbitrary rules DO NOT ALLOW file OVERRWRITE." & vbCrLf & _
               "Folder: '" & sFolder & "'" & vbCrLf & _
               "File: '" & sFileName & "'" & vbCrLf & _
               ""
        Exit Sub
      End If
    
      '''''''''''''''''''''''''''''''''''''''''''''
      'Export
      '''''''''''''''''''''''''''''''''''''''''''''
        
      'Allocate a file 'handle'
      iFileNo = FreeFile
      
      'Open the file
      Open sPathAndFileName For Output As #iFileNo
      
      'Create the starting and ending Row Numbers
      iFirstRow = 11
      iLastRow = ActiveSheet.Range("A:A").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
      
      For iRow = iFirstRow To iLastRow
        sDataLine = ActiveSheet.Cells(iRow, "A").Value
        Print #iFileNo, sDataLine
      Next iRow
      
      'Close the file
      Close #iFileNo
      
      
      '''''''''''''''''''''''''''''''''''''''''''''
      'Termination
      '''''''''''''''''''''''''''''''''''''''''''''
      
      MsgBox "Batch file creation completed." & vbCrLf & _
             "Folder: '" & sFolder & "'" & vbCrLf & _
             "File: '" & sFileName & "'" & vbCrLf & _
             ""
                  
    End Sub
    
    Public Function LJMFileExists(sPathAndFullFileName As String) As Boolean
      'This returns TRUE if a file exists and FALSE if a file does NOT exist
      
      Dim iError As Integer
      Dim iFileAttributes As Integer
    
      On Error Resume Next
      iFileAttributes = GetAttr(sPathAndFullFileName)
         
      'Check the internal error  return
      iError = Err.Number
      Select Case iError
        Case Is = 0
            iFileAttributes = iFileAttributes And vbDirectory
            If iFileAttributes = 0 Then
              LJMFileExists = True
            Else
              LJMFileExists = False
            End If
        Case Else
            LJMFileExists = False
      End Select
    
      On Error GoTo 0
    
    End Function
    To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. Dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx

    Lewis

+ 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. Need macro that opens .txt file in excel, run macros and save file in batch
    By khalidness in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-11-2014, 11:00 AM
  2. Replies: 0
    Last Post: 01-21-2014, 10:52 AM
  3. [SOLVED] Can't specify the folder for the output of the batch file invoked by Excel VBA
    By merthum in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-27-2012, 09:26 AM
  4. Can't specify the folder for the output of the batch file invoked by Excel VBA
    By merthum in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-25-2012, 07:41 AM
  5. getting vba shell command to execute batch and save output to a file
    By Mad Scientist Jr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-28-2006, 10:10 AM

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