VBA中,将数组保存为txt文件

一、效果

1、获取数组

2、运行后生成的txt文件(如下图右侧所示)

二、代码


Sub 使用示例()
''    Application.ScreenUpdating = False
'    Application.DisplayAlerts = False
    arr = Range("A1").CurrentRegion
    WenjianM = "生成的文件"
    Call 数组生成文件(WenjianM, arr)
'    Application.DisplayAlerts = True
'    Application.ScreenUpdating = True

End Sub

Function 数组生成文件(wm, arr)
    On Error Resume Next
    
    Dim fso As Object
    Set fso = VBA.CreateObject("Scripting.FileSystemObject")
'    fso.DeleteFile (ThisWorkbook.Path & "\" & wm & ".TxT")
    
    '复制数据
    Dim i As Integer
    Dim data As String
    For Each ar In arr
        If ar = "" Then Exit For
        data = data & ar & vbCrLf
    Next
    
    '创建并保存txt文件
    Dim fileName As String
    fileName = ThisWorkbook.Path & "\" & wm & ".txt"
    Dim fileNum As Integer
    fileNum = FreeFile()
    Open fileName For Output As fileNum
    Print #fileNum, data
    Close fileNum
End Function

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值