将合并的单元格视为一个

时间:2014-05-02 04:19:58

标签: vba excel-vba excel

我在excel中使用VBA代码将选定范围导出到文本文件,但是当我使用合并单元格(合并行)时,我在导出中添加了空白行。如何使代码将合并的单元格视为一个?

Sub writeCSV(ByVal thisRange As Range, ByVal filePath As String, Optional ByVal fileAppend As Boolean = False)
Dim cLoop As Long, rLoop As Long
Dim ff As Long, strRow As String

ff = FreeFile
If fileAppend Then
    Open filePath For Append As #ff
Else
    Open filePath For Output As #ff
End If

For rLoop = 1 To thisRange.Rows.Count
    strRow = ""
    For cLoop = 1 To thisRange.Columns.Count
        If cLoop > 1 Then strRow = strRow & ","
        strRow = strRow & thisRange.Cells(rLoop, cLoop).Value
    Next 'cLoop
    Print #ff, strRow
Next 'rLoop

Close #ff
End Sub

Sub Wri()

Dim myrng As Range
Dim Cell As Range

On Error Resume Next
Set myrng = Application.InputBox("Select range", Type:=8)
On Error GoTo 0

If myrng Is Nothing Then
    MsgBox "No cells selected"
    Exit Sub
Else
    writeCSV myrng, "C:\Users\HP\Documents\fil.txt", True
End If

End Sub

0 个答案:

没有答案