VBA将数据从Excel单元格写入文本/批处理文件

时间:2016-06-29 16:49:44

标签: excel vba batch-file updates

我有一个Excel文件,它在Cell A1中存储文件路径,并与Function TODAY()连接以更新该文件路径。

我还有一个批处理文件,可以将文件从一个文件夹移动到另一个文件夹。我目前使用此更新excel单元格来获取新的移动批处理命令。

如何使用VBA自动将Cell A1中Sheet1的数据复制到批处理文件?

存储批处理文件 C:\桌面\批次\ update.bat

以下是以下评论中的示例代码,但我只想让它从Sheet 1和Cell A1中复制数据

Dim FilePath As String
Dim CellData As String
Dim LastCol As Long
Dim LastRow As Long

LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column

LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row

FilePath = "C:\Batch\copy.bat"

Open FilePath For Output As #2

For i = 1 To LastRow

    For j = 1 To LastCol

        If j = LastCol Then

            CellData = CellData + Trim(ActiveCell(i, j).Value)

        Else

            CellData = CellData + Trim(ActiveCell(i, j).Value) + ","

        End If

    Next j

    Write #2, CellData
    CellData = ""

Next i

Close #2

MsgBox ("Done")

1 个答案:

答案 0 :(得分:0)

Dim FilePath As String
Dim CellData As String

FilePath = "C:\Batch\copy.bat"

Open FilePath For Output As #2

r = Worksheet("Sheet1").Range("A1").Text

Print #2 r