Error runtime 13 export a selected column to text file

RaidHack

New Member
Joined
Oct 14, 2018
Messages
5
HI
Can you help me?
This macro give me a error runtime 13 mismatch when the loop arrive to a blank cell
how can fix it?
i'm sorry for my english
Code:
Sub saveText2()
    Dim filename As String, lineText As String
    Dim myrng As Range, i, j
    
    filename = ThisWorkbook.Path & "\Command.txt"
    
    Open filename For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
    
    Set myrng = Range("M4:M120")
    




    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 [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , lineText
    Next i
    
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
 End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi, and welcome to MrExcel.

I don't think that a blank cell will cause an error. It's likely something else causing it. Maybe some sort of error value, such as #N/A, #DIV/0!, #NUM!, etc? Try running the macro again. This time, when the error occurs, click on Debug. This will highlight the line causing the error in yellow, which most likely will be this line...

Code:
[COLOR=#333333]lineText = IIf(j = 1, "", lineText & ",") & myrng.Cells(i, j)[/COLOR]

If so, place your cursor over myrng.Cells(i, j) . What does it display?

By the way, your English is fine.
;)
 
Last edited:
Upvote 0
Hi, and welcome to MrExcel.

I don't think that a blank cell will cause an error. It's likely something else causing it. Maybe some sort of error value, such as #N/A, #DIV/0!, #NUM!, etc? Try running the macro again. This time, when the error occurs, click on Debug. This will highlight the line causing the error in yellow, which most likely will be this line...

Code:
[COLOR=#333333]lineText = IIf(j = 1, "", lineText & ",") & myrng.Cells(i, j)[/COLOR]

If so, place your cursor over myrng.Cells(i, j) . What does it display?

By the way, your English is fine.
;)

ok,
i=24 and j=1
but i just fixed it.
can i ask you a thing?

Code:
Sub Command()On Error Resume Next
Dim filename As String, lineText As String
Dim myrng As Range, i, j
filename = ThisWorkbook.Path & "\Command.txt"
Open filename For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
Set myrng = Range("M4:M160")
finalrow = Cells(Rows.Count, "M").End(xlUp).Row
For i = 1 To myrng.Rows.Count
     For j = 1 To myrng.Columns.Count
     If myrng.Cells(i, j) = "Errore 2007" Then
    GoTo ritorno
     End If
            lineText = IIf(j = 1, "", lineText & ",") & myrng.Cells(i, j)


Next j
lineText = StrConv(lineText, vbUnicode)
Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , lineText
ritorno:
Next i
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
End Sub

how can i say to Macro to create a unicode text file?
i use a symbol which become "???"
 
Upvote 0
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">

Sub Command()On Error Resume Next
Dim filename As String, lineText As String
Dim myrng As Range, i, j
filename = ThisWorkbook.Path & "\Command.txt"
filename.Charset = "utf-8"

Open filename For Output As
#1
Set myrng = Range("M4:M160")
finalrow = Cells(Rows.Count, "M").End(xlUp).Row
For i = 1 To myrng.Rows.Count
For j = 1 To myrng.Columns.Count
If myrng.Cells(i, j) = "Errore 2007" Then
GoTo ritorno
End If
lineText = IIf(j = 1, "", lineText & ",") & myrng.Cells(i, j)


Next j
lineText = StrConv(lineText, vbUnicode)
Print
#1 , lineText
ritorno:
Next i
Close
#1
End Sub
</code></pre>
 
Upvote 0
Try the following...

Code:
Option Explicit

Sub Command()


    Dim stream As Object
    Dim outFileName As String
    Dim sourceRange As Range
    Dim currentValue As Variant
    Dim lineText As String
    Dim lastRow As Long
    Dim i As Long
    Dim j As Long
    
    outFileName = ThisWorkbook.Path & "\Command.txt"
    
    lastRow = Cells(Rows.Count, "M").End(xlUp).Row
    
    Set sourceRange = Range("M4:M" & lastRow)
    
    lineText = ""
    For i = 1 To sourceRange.Rows.Count
        For j = 1 To sourceRange.Columns.Count
            currentValue = sourceRange(i, j).Value
            If Not IsError(currentValue) Then
                lineText = lineText & IIf(j > 1, ",", "") & currentValue
            End If
        Next j
        lineText = lineText & vbCrLf
    Next i
    
    Set stream = CreateObject("ADODB.Stream")
    
    With stream
        .Charset = "UTF-8"
        .Mode = 3 'adModeReadWrite
        .Type = 2 'adTypeText
        .Open
        .WriteText lineText
        .SaveToFile outFileName, 2 '1=adSaveCreateNotExist, 2=adSaveCreateOverWrite
        .Close
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,755
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top