VBA Write Data From Excel Cell to Text/Batch File

别说谁变了你拦得住时间么 提交于 2019-12-25 07:17:12

问题


I have an Excel File that stores a file path within Cell A1 and concatenates with the Function TODAY() to update that file path.

I also have a batch file that will move files from one folder to another. I currently use this updating excel cell to get the new move batch command.

How can I use VBA to copy data from Sheet1 in Cell A1 to the batch file automatically?

Batch file is stored C:\Desktop\Batch\update.bat

Here is an example code from the comments below but I just want it to copy data from Sheet 1 and 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:


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


来源:https://stackoverflow.com/questions/38105691/vba-write-data-from-excel-cell-to-text-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!