通过 VBA 复制并粘贴到批处理文件时丢失字符

通过 VBA 复制并粘贴到批处理文件时丢失字符

我目前正在使用以下 VBA 代码复制并粘贴 Excel 文件中的一系列数据以创建批处理文件。我遇到了以下问题:

  1. 单个单元格内容被加引号
  2. 我正在失去角色回归

这是我复制的数据,下面列出的每一行都需要复制到批处理文件中的另一行:

@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_

有人知道我该如何修改我的 VBA 代码来纠正这些项目吗?

这是我的代码的副本:

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

只需写入文件,而不需要经过另一个 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

相关内容