![](/img/trans.png)
[英]VBA - For exporting data from Excel to text (cell by cell with separators)
[英]VBA Write Data From Excel Cell to Text/Batch File
我有一个Excel文件,它在单元格A1中存储文件路径,并与函数TODAY()串联以更新该文件路径。
我还有一个批处理文件,它将文件从一个文件夹移动到另一个文件夹。 我目前使用此更新的excel单元格来获取新的move batch命令。
如何使用VBA自动将数据从单元格A1中的Sheet1复制到批处理文件?
批处理文件存储在C:\\ Desktop \\ Batch \\ update.bat
这是下面注释中的示例代码,但我只希望它复制工作表1和单元格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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.