将选定的单元格导出到.txt文件的末尾

时间:2019-01-14 07:23:01

标签: excel

我正在使用此代码将台式机中的txt文件导出,效果很好,但是每次使用此代码时,我都需要将所选单元格导出到txt文件的末尾

Sub ExportToTxtFile()
Dim myFile As String, txtname As Range, rng As Range, cellValue As Variant, i As Integer, j As Integer
Set txtname = Range("H1")
myFile = Environ("USERPROFILE") & "\Desktop\" & txtname & ".txt"
Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
    For j = 1 To rng.Columns.Count
    cellValue = rng.Cells(i, j).Value
    If j = rng.Columns.Count Then
    Print #1, cellValue
Else
    Print #1, cellValue,
End If
    Next j
Next i
Close #1
End Sub

0 个答案:

没有答案