Excel Table to CSV specific columns only

cerberus1845

New Member
Joined
Nov 14, 2023
Messages
23
Office Version
  1. 2021
Platform
  1. Windows
  2. MacOS
Hi,

I've found the following code:

VBA Code:
Sub saveTableToCSV()
    
    Dim tbl As ListObject
    Dim csvFilePath As String
    Dim fNum As Integer
    Dim tblArr
    Dim rowArr
    Dim csvVal

    Set tbl = Worksheets("YourSheetName").ListObjects("YourTableName")
    csvFilePath = "C:\Users\vmishra\Desktop\CSVFile.csv"
    tblArr = tbl.DataBodyRange.Value
    
    fNum = FreeFile()
    Open csvFilePath For Output As #fNum
    For i = 1 To UBound(tblArr)
        rowArr = Application.Index(tblArr, i, 0)
        csvVal = VBA.Join(rowArr, ",")
        Print #1, csvVal
    Next
    Close #fNum
    Set tblArr = Nothing
    Set rowArr = Nothing
    Set csvVal = Nothing
End Sub

Does anyone know how I would edit the above to not output the entire table - but instead all data in columns B to L?? - I've tried tweaking it - but can't seem to get it to work...
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try this.
VBA Code:
tblArr = tbl.Range.Columns("B:L").Value

...or this.
VBA Code:
tblArr = tbl.ListColumns(2).DataBodyRange.Resize(, 10).Value
 
Upvote 1
Solution
Try this.
VBA Code:
tblArr = tbl.Range.Columns("B:L").Value

...or this.
VBA Code:
tblArr = tbl.ListColumns(2).DataBodyRange.Resize(, 10).Value

awesome!! - went with the first option and works perfectly!!! - thanks for taking the time to reply and helping me with this.. marked your comment as the accepted solution! :)
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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