将一系列单元格中的文本保存到.txt文件 [英] Save text in a range of cells to .txt file

查看:75
本文介绍了将一系列单元格中的文本保存到.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将有效范围的文本保存到.txt文件中.

I need to save an active range of texts to a .txt file.

范围是工作表中名为报告"的F列.行数取决于报表生成的行数.F列包含电子邮件地址,我们需要将其通过.txt文件上传到另一个系统.

The range is a column F within the worksheet called "Reports". The number of rows depends on how many rows the report generates. This column F contains email addresses, which we need to upload to another system via a .txt file.

在.txt文件中,每个地址将位于不同的行中,而没有其他定界符.

In the .txt file, each address will be located in a different line without other delimiters.

我有代码,但文本文件的第一行从第二行开始留空.

I have code, but it leaves the first line of the text file blank, starting with the second line.

Sub Macro_Newsletter()
Dim c As Range
Dim r As Range
Dim output As String

For Each r In Worksheets("Reports").Range("F2:F10000").Rows
    For Each c In r.Cells
        output = output & vbNewLine & c.Value
    Next c
Next r
Open "C:\Users\joseph.lin\Desktop\Database\Newsletter" For Output As #1
Print #1, output
Close
End Sub

我只知道如何将它们输出到Outlook.请帮助我弄清楚如何使用VBA进行此操作.

I only know how to output them to outlook. Please help me figure out how to do this using VBA.

推荐答案

这可以解决问题:

Sub Macro_Newsletter()

    Dim wbText As Workbook
    Dim wsReports As Worksheet

    Set wbText = Workbooks.Add

    Set wsReports = ThisWorkbook.Worksheets("Reports")

    With wsReports

        Dim lRow As Long
        lRow = .Range("F" & .Rows.Count).End(xlUp).Row 'get last row of emails

        .Range("F2:F" & lRow).Copy wbText.Sheets(1).Range("A1")

    End With

    'turn off alerts so you don't see messages about compatibility and such
    Application.DisplayAlerts = False

    With wbText

        .SaveAs Filename:="C:\Users\joseph.lin\Desktop\Database\Newsletter\Emails.txt", _
            FileFormat:=xlText
        .Close False

    End With

    Application.DisplayAlerts = True

End Sub

这篇关于将一系列单元格中的文本保存到.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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