将Excel范围导出到TXT停在空白单元格 [英] Export Excel range to TXT stop at empty cell

查看:150
本文介绍了将Excel范围导出到TXT停在空白单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找到以下脚本

Sub saveText2()
Dim filename As String, lineText As String
Dim myrng As Range, i, j

filename = ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt"

Open filename For Output As #1

Set myrng = Range("Name")

For i = 1 To myrng.Rows.Count
    For j = 1 To myrng.Columns.Count
        lineText = IIf(j = 1, "", lineText & ",") & myrng.Cells(i, j)
    Next j
    Print #1, lineText
Next i
Close #1
End Sub

但是我似乎没有设法在第一行空白处停止循环.

But I don´t seems to manage to stop the loop at first empty row.

我尝试在myrng<>"循环中添加DO,但是我不知道在哪里应用代码.

I´ve tried to add a DO While myrng <> "" Loop but I don´t know where to apply the code.

推荐答案

不确定这是最聪明的方法,但我最终通过以下方法进行了管理:

Not sure it i the smartest way but I finally managedby doing this:

Sub saveText2()
Dim filename As String, lineText As String
Dim myrng As Range, i, j

filename = ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt"

Open filename For Output As #1

Set myrng = Range("A:B")

For i = 1 To myrng.Rows.Count
    For j = 1 To myrng.Columns.Count

    If IsEmpty(myrng.Cells(i, j)) = True Then
    Close #1
    Exit Sub
    End If

        lineText = IIf(j = 1, "", lineText & " ") & myrng.Cells(i, j)
    Next j
    Print #1, lineText
Next i
Close #1
End Sub

这篇关于将Excel范围导出到TXT停在空白单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆