附加范围到日志(Txt)文件

时间:2017-05-19 11:45:52

标签: vba excel-vba excel

我想将"A2:B" & LastRow的范围追加到带有;分隔符的log txt文件。

目前我知道如何从单独的单元格中读取,而不是从"A2:B"

读取

A =文字

专栏B2:B = =IF(A2="","",1)

我的代码:

Dim strData As String
Dim strLine As String
strData = ""
Open "\\x-ap01\Log.txt" For Input As #1
While EOF(1) = False
    Line Input #1, strLine
    strData = strData + strLine & vbCrLf
Wend

strData = strData + Cells(2, 1) & ";" & Cells(2, 2) & ";"    

Close #1
Open "\\x-ap01\Log.txt" For Output As #1
Print #1, strData
Close #1

1 个答案:

答案 0 :(得分:1)

你的意思是这样吗?

Sub x()

Dim strData As String
Dim strLine As String
Dim lastrow As Long
Dim r As Long

lastrow = Range("A" & Rows.Count).End(xlUp).Row

Open "\\x-ap01\Log.txt" For Append As #1

For r = 2 To lastrow
    If Cells(r, 1) <> vbNullString Then
        strData = strData & Cells(r, 1) & ";" & Cells(r, 2) & ";"
        strData = strData & strLine & vbCrLf
    End If
Next r

Print #1, strData
Close #1

End Sub