0

I am currently using the following VBA code to copy and paste a range of data in an excel file to create a Batch file. I am running into the following problems:

  1. Quotations are being placed around individual cell contents
  2. I am losing the character return

Here is what my data that is copied, each line listed below needs to copy on another line in the batch file:

@ftp -i -s:"%~f0"&GOTO:EOF
open ecggwprd.uhc.com
if1**79
W88***3k
bin
put \\Nas00233pn\ACP_Finance_Ops\FTP\Ced.201602.zip  /cedarhill/Ced.201602.zip
!ping -n 30 0.0.0.0 > NULL
CD /ced
dir Q__C_

Does anyone know how I can modify my VBA code to correct these items?

Here is a copy of my code:

Sub savebat()

Dim wbSource As Workbook
Dim wsSource As Worksheet
Dim wbDest As Workbook
Dim fName As String

Set wbSource = ActiveWorkbook
Set wsSource = ActiveSheet
Set wbDest = Workbooks.Add

wsSource.Range("A1:A51").Copy

'----------------------------
'Save in new workbook
wbDest.Worksheets(1).Cells(1, 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False

Application.CutCopyMode = False

'Get file name and location from user
fName = Application.GetSaveAsFilename(FileFilter:="bat (*.bat), *.bat", Title:="Save As")
If fName = "" Then Exit Sub '//user cancelled

'Save new bat file
wbDest.SaveAs fName, FileFormat:=xlText

wbDest.Close SaveChanges:=True
'----------------------------

End Sub

1 Answer 1

0

Just write to the file rather than going through another WB.

Option Explicit

Sub CreateBatch()
    Dim myPathTo As String
    myPathTo = "C:\path\to\mybatch.bat"
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim batchFile As Object
    Set batchFile = fso.CreateTextFile(myPathTo)

    Dim lastRow As Long
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Dim i As Long

        For i = 1 To lastRow
            batchFile.write Cells(i, 1) & vbNewLine
        Next
        batchFile.Close
    Set fso = Nothing
    Set batchFile = Nothing
End Sub
2
  • That works great for removing the "" but I am still losing the character returns from put to the end
    – Kthorson16
    Mar 31, 2016 at 18:55
  • then replace the vbnewline with chr 10 and 13 Mar 31, 2016 at 18:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .