excel - 将一系列单元格中的文本保存到 .txt 文件

标签 excel vba

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

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

在 .txt 文件中,每个地址将位于不同的行中,没有其他分隔符。

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

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 做到这一点。

最佳答案

这可以解决问题:

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

关于excel - 将一系列单元格中的文本保存到 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37998555/

相关文章:

c# - 在 Silverlight 4 中下载后自动打开文件

vba - 在一个范围内选择大小写

arrays - 将 Variant 数组传递给 Application.Index 方法时,Excel VBA 运行时错误 '13' : Type mismatch,

vb.net - 在 Excel 范围内编辑值

excel - 循环遍历工作簿中的所有工作表,并将存储为文本的数字更改为数字

Excel VBA 将项目添加到动态创建的组合框

html - 如何使用vba填写html自动完成字段?

excel - 为什么从 Word doc 调用宏时出现错误 1004,而不是从 Excel 调用?

excel - 有没有办法从 CSV 中获取行而不加载整个文件?

正则表达式捕获 VBA 注释