我需要将事件范围的文本保存到 .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/