excel宏定义问题,输出前三列到txt 5C

我想输出excel表中的前三列内容到txt中,中间用空格隔开。这里有宏定义只是输出前两列,怎么改成输出前三列的?麻烦各位大神帮忙看看了!谢谢!
Sub st()
Dim rg As Object
Dim Strtemp
Dim rc As Long
rc = Application.Max(Range("A" & Rows.Count).End(xlUp).Row, Range("B" & Rows.Count).End(xlUp).Row)

Strtemp = ""
For Each rg In Range("A1:B" & rc)
If rg.Row <= rc Then
If rg.Column = 1 Then
Strtemp = Strtemp & rg.Value & vbTab
Else
Strtemp = Strtemp & rg.Value & vbCrLf
End If

End If

Next
Open ThisWorkbook.Path & "\AB.txt" For Output As #1
Print #1, Strtemp
Close #1

End Sub

3个回答

Sub st()
    Dim rg As Object
    Dim Strtemp
    Dim rc As Long
    rc = Application.Max(Range("A" & Rows.Count).End(xlUp).Row, Range("B" & Rows.Count).End(xlUp).Row, Range("C" & Rows.Count).End(xlUp).Row)

    Strtemp = ""
    For Each rg In Range("A1:C" & rc)
        If rg.Row <= rc Then
            If rg.Column <> rc Then
                Strtemp = Strtemp & rg.Value & vbTab
            Else
                Strtemp = Strtemp & rg.Value & vbCrLf
            End If
        End If
    Next
    Open ThisWorkbook.Path & "\AB.txt" For Output As #1
    Print #1, Strtemp
    Close #1

End Sub
u014368400
一叶一追寻110 您好!这样输出到txt,各行之间没有回车换行,请问怎么能换行呀?麻烦了呀。 excel 内容如图: ![图片说明](http://img.ask.csdn.net/upload/201705/29/1496037933_373342.png)按照您说的执行后得到的txt是: ![图片说明](http://img.ask.csdn.net/upload/201705/29/1496037943_70182.png)我想要的TXT格式内容是: ![图片说明](http://img.ask.csdn.net/upload/201705/29/1496038325_675569.png)O(∩_∩)O谢谢!
3 年多之前 回复

您好!这样输出到txt,各行之间没有回车换行,请问怎么能换行呀?麻烦了呀。
excel 内容如图:
图片说明
按照您说的执行后得到的txt是:
图片说明
我想要的TXT格式内容是:
图片说明
O(∩_∩)O谢谢!

If rg.Column <> rc Then
改为
If rg.Column <> 3 Then

试试

Sub st()
    Dim rg As Object
    Dim Strtemp
    Dim rc As Long
    rc = Application.Max(Range("A" & Rows.Count).End(xlUp).Row, Range("B" & Rows.Count).End(xlUp).Row, Range("C" & Rows.Count).End(xlUp).Row)

    Strtemp = ""
    For Each rg In Range("A1:C" & rc)
        If rg.Row <= rc Then
            If rg.Column <> 3 Then
                Strtemp = Strtemp & rg.Value & vbTab
            Else
                Strtemp = Strtemp & rg.Value & vbCrLf
            End If
        End If
    Next
    Open ThisWorkbook.Path & "\AB.txt" For Output As #1
    Print #1, Strtemp
    Close #1

End Sub
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问