我有一个Excel工作表,我需要将范围A:1导出到A列中的最后一个单元格到xml文件。如何将导出的文件名设置为与我导出的文件相同?
Sub exportxmlfile()
Dim myrange As Range
Worksheets("xml").Activate
Set myrange = Range("A1:A20000")
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("C:\exports\2012\test.xml", True)
For Each c In myrange
a.WriteLine (c.Value)
Next c
a.Close
End Sub
答案 0 :(得分:0)
使用Workbook.Name
属性获取文件名。
FWIW,有一些改进代码的机会
Sub exportxmlfile()
' declare all your variables
Dim myrange As Range
Dim fs As Object
Dim a As Object
Dim dat As Variant
Dim i As Long
' No need to activate sheet
With Worksheets("xml")
' get the actual last used cell
Set myrange = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
' copy range data to a variant array - looping over an array is faster
dat = myrange.Value
Set fs = CreateObject("Scripting.FileSystemObject")
' use the excel file name
Set a = fs.CreateTextFile("C:\exports\2012\" & .Parent.Name & ".xml", True)
End With
For i = 1 To UBound(dat, 1)
a.WriteLine dat(i, 1)
Next
a.Close
End Sub