|
本帖最后由 maydbl 于 2016-6-14 07:55 编辑
工作需要,在隔壁论坛中获得一段代码,其作用是用鼠标选择单元格范围、运行宏后,所选择的内容自动保存为txt文件。
我想修改代码(见下),把指定单元格范围(如:B5:D20,不用鼠标选择)的内容自动保存为txt文件。求指点!谢谢!
Sub mytext()
Dim arr, str$, i As Long, j As Long
Dim fso As Object, myfile As Object
Set fso = CreateObject("scripting.filesystemobject")
With fso
Set myfile = .opentextfile(ThisWorkbook.Path & "\" & ActiveSheet.Name & ".txt", 8, True)
End With
arr = Selection.Value
For i = 1 To UBound(arr)
str = ""
For j = 1 To UBound(arr, 2)
str = str & "," & arr(i, j)
Next j
str = Right(str, Len(str) - 1)
myfile.writeline (str)
Next i
myfile.Close
Set myfile = Nothing
Set fso = Nothing
End Sub
arr = Selection.Value
改成
arr = [b5:d20]
|
|