VBA将数据从Excel单元格写入文本/批处理文件 [英] VBA Write Data From Excel Cell to Text/Batch File

查看:116
本文介绍了VBA将数据从Excel单元格写入文本/批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel文件,该文件存储单元格A1中的文件路径,并与函数TODAY()串联以更新该文件路径.

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

我还有一个批处理文件,它将文件从一个文件夹移动到另一个文件夹.我目前正在使用此更新的excel单元格来获取新的move batch命令.

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.

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

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

批处理文件已存储C:\ Desktop \ Batch \ update.bat

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

这是下面注释中的示例代码,但我只希望它复制工作表1和单元格A1中的数据

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")

推荐答案

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

这篇关于VBA将数据从Excel单元格写入文本/批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆