VBA for exporting a Range of cells in a csv or txt

martinequilibrium

New Member
Joined
Feb 26, 2016
Messages
13
Dear Users,

I'm having trouble to export a selection via the following code in VBA.

Some of the rows actually do get exported but I get
Runtime error 13 Type mismatch.
Actually rCell.Value gives me error 2023 (xlErrRef 2023 #REF!)

I have played with the starting row of Set ExportRange = ws.Range("A200:W" & lastrow)
Smaller ranges do work. Ranges with 3000 rows do not seem to work. Maybe a memory issue

I want to export all the rows of ExportedRange.

Code:
Sub CreateCSV()

    Dim rCell As Range
    Dim rRow As Range
    Dim ExportRange As Range
    'Dim sOutput As Variant
    Dim sFname As String, lFnum As Long
    
    Const sDELIM As String = ";"
    
    'path = CurDir()
    Path = ActiveWorkbook.Path
    Name = udfWBFilename(ThisWorkbook.FullName)
        
    'Open a text file to write
    sFname = Path & "\" & Name & ".txt"
    lFnum = FreeFile
    Open sFname For Output As lFnum
    
    Set ws = Worksheets("WMS")
    lastrow = ws.Cells(Rows.Count, "E").End(xlUp).Row
    
    Set ExportRange = ws.Range("A200:W" & lastrow)
    MsgBox (ExportRange.Rows)
    'Loop through the rows
    For Each rRow In ExportRange.Rows
        'Loop through the cells in the rows
        For Each rCell In rRow.Cells
            sOutput = sOutput & rCell.Value & sDELIM
        Next rCell
         
        'remove the last comma
        'sOutput = Left(sOutput, Len(sOutput) – Len(sDELIM))
       
        'write to the file and reinitialize the variables
        Print #lFnum, sOutput
        sOutput = vbNullString
    Next rRow
    
   
    'Close the file
    Close lFnum
   
    
End Sub

Can provide sample table if needed.

Cheers
Martin
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Maybe copy range to new workbook, save new workbook as csv?
 
Upvote 0
Upvote 0
I cannot download files from my current location (security protocol), and I know that many others are unable or unwilling to download files for security reasons as well. I may be able to download the file tonight when I am home.
 
Upvote 0
I cannot download files from my current location (security protocol), and I know that many others are unable or unwilling to download files for security reasons as well. I may be able to download the file tonight when I am home.

Thanks

A low number of records seem to work fine with the code.
It is possible to try the above code with any long workbook with a similar number of rows like 300 or so.

Just change
Set ws = Worksheets("WMS") to Set ws = Worksheets(1)

Many thanks again for your time!
 
Upvote 0
OK. I downloaded your file and found two issues.

1. This is not a valid line of code:
Code:
MsgBox (ExportRange.Rows)
If you are trying to to return the total number of rows in the range, use this:
Code:
MsgBox (ExportRange.Rows.Count)

2. You have errors in column W of your WMS worksheet, starting on row 1431. You need to correct these errors, it cannot build the string with them in there.
 
Upvote 0
Thank You very much Joe4!
That solved my issue! ;). Kudos :cool:

Now can export the needed data!

Curious question.
A blank line gets inserted at the end of the file.
Why is that?

OK. I downloaded your file and found two issues.

1. This is not a valid line of code:
Code:
MsgBox (ExportRange.Rows)
If you are trying to to return the total number of rows in the range, use this:
Code:
MsgBox (ExportRange.Rows.Count)

2. You have errors in column W of your WMS worksheet, starting on row 1431. You need to correct these errors, it cannot build the string with them in there.
 
Upvote 0

Forum statistics

Threads
1,215,182
Messages
6,123,517
Members
449,102
Latest member
admvlad

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