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
 
Curious question.
A blank line gets inserted at the end of the file.
Why is that?
That really isn't a method that I typically use to create CSV files (typically, I just get the data the way I want and save as CSV as opposed to writing it out line-by-line), so I cannot say for sure.
Does the lastrow calculation look like it is correctly identifying the last row of data? You could always temporarily insert a MsgBox statement to check.
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,215,184
Messages
6,123,533
Members
449,106
Latest member
techog

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