Remove the header (column names) in vba before saving the sheet in csv file

Soly_90

New Member
Joined
Sep 4, 2024
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hello

I am saving the table from specific sheet as csv file by macro vba .
But I am still looking to saving the table without headers column name .
Is there any easy way for that ?
Thanks for help
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Simply slip in the following before the save instruction. Change Table2 to your table name.
VBA Code:
ActiveSheet.ListObjects("Table2").ShowHeaders = False
 
Upvote 0
Simply slip in the following before the save instruction. Change Table2 to your table name.
VBA Code:
ActiveSheet.ListObjects("Table2").ShowHeaders = False
I have tried it but it does not work with me .
I got the table from power query but I did not find a method to remove header names (column names) in power query so I would like to do it in macro vba before saving the csv file .


I have tried Activesheet.ListObjects(“results”).ShowHeaders = False
After the line of ws,Copy
But it does not work ,
Could you please edit this code .thanks a lot for help

VBA Code:
Set Ws=Sheets(“results”)
ws.Copy
ActiveWorkbook.SaveAs file name :=fullpath, FileFormat:=xlCSV, CreateBackup:=False,Local:=True
 
Upvote 0
It works .i found my mistake ,i have to write the name of table instead of name of my sheet .thanks a lot
 
Upvote 0
Hello

I am saving the table from specific sheet as csv file by macro vba .
But I am still looking to saving the table without headers column name .
Is there any easy way for that ?
Thanks for help

Does this work for you?

You will need to change the lines indicated

Public Sub subCopyTableToCSVFile()
Dim Ws As Worksheet
Dim strPath As String

' Change this line.
Set Ws = Worksheets("TheTable")

strPath = ActiveWorkbook.Path

Ws.Copy

With ActiveSheet
With .ListObjects(1)
.Unlist
End With
.Rows(1).Delete
End With

' Change this line.
ActiveWorkbook.SaveAs Filename:=strPath & "\" & Format(Now(), "HHMMSS"), _
FileFormat:=xlCSV, _
CreateBackup:=False, _
Local:=True

End Sub
 
Upvote 0
Does this work for you?

You will need to change the lines indicated

Public Sub subCopyTableToCSVFile()
Dim Ws As Worksheet
Dim strPath As String

' Change this line.
Set Ws = Worksheets("TheTable")

strPath = ActiveWorkbook.Path

Ws.Copy

With ActiveSheet
With .ListObjects(1)
.Unlist
End With
.Rows(1).Delete
End With

' Change this line.
ActiveWorkbook.SaveAs Filename:=strPath & "\" & Format(Now(), "HHMMSS"), _
FileFormat:=xlCSV, _
CreateBackup:=False, _
Local:=True

End Sub

' Change this line.
Set Ws = Worksheets("TheTable")

Thanks a lot for your script .
I have noticed now that I have to mention the table not the whole sheet .
But when I mention the name of table ,I got error .(debug)
How could I display ,what exactly the mistake here
I tried set ws =Worksheets(“name of table”)
And I have tried set ws =Sheets(“name of table”)
Both of them does not let the code to run .
It mention a mistake at this line .
But when I use
Set ws= Sheets(“results”)
name of sheet ,it works
I understood that I am saving the whole sheet not only the specific table .but I am lucky ,I have only one table .
But it is still interesting to solve the problem of this line .
 
Upvote 0
' Change this line.
Set Ws = Worksheets("TheTable")

Thanks a lot for your script .
I have noticed now that I have to mention the table not the whole sheet .
But when I mention the name of table ,I got error .(debug)
How could I display ,what exactly the mistake here
I tried set ws =Worksheets(“name of table”)
And I have tried set ws =Sheets(“name of table”)
Both of them does not let the code to run .
It mention a mistake at this line .
But when I use
Set ws= Sheets(“results”)
name of sheet ,it works
I understood that I am saving the whole sheet not only the specific table .but I am lucky ,I have only one table .
But it is still interesting to solve the problem of this line .
Try this. More explanation.

Read the notes. No need to state the table name.

VBA Code:
Public Sub subCopyTableToCSVFile()
Dim Ws As Worksheet
Dim strPath As String
Dim intRow As Integer

  ActiveWorkbook.Save

  ThisWorkbook.Activate

  ' Change this line.
  ' This line assigns a worksheet object to the
  ' worksheet object variable Ws.
  ' Substitute 'TheTable' with you worksheet name, not the table name.
  Set Ws = Worksheets("TheTable")
  
  Ws.Activate
  
  ' Change this to where you want the CSV file saved.
  ' Omit the '\' at the end.
  strPath = ActiveWorkbook.Path
  
  ' Thsi copies the worksheet to a new workbook with just one worksheet.
  Ws.Copy
  
  ' The new workbook and single sheet with the table is now the active sheet.
  With ActiveSheet
  
    ' This makes reference to the FIRST table on the new worksheet
    ' so the table name is NOT required.
    With .ListObjects(1)
      ' Convert the table to a simple range.
      .Unlist
    End With
    
    ' Delete the first row of the worksheet.
    .Rows(1).Delete
       
  End With
 
  With ActiveWorkbook
    
    ' Change this line to state the CSV filename.
    ' This bit "\" & Format(Now(), "HHMMSS")
    .SaveAs Filename:=strPath & "\" & Format(Now(), "HHMMSS"), _
      FileFormat:=xlCSV, _
      CreateBackup:=False, _
      Local:=True

    .Close False
  
  End With

End Sub
 
Upvote 0
Try this. More explanation.

Read the notes. No need to state the table name.

VBA Code:
Public Sub subCopyTableToCSVFile()
Dim Ws As Worksheet
Dim strPath As String
Dim intRow As Integer

  ActiveWorkbook.Save

  ThisWorkbook.Activate

  ' Change this line.
  ' This line assigns a worksheet object to the
  ' worksheet object variable Ws.
  ' Substitute 'TheTable' with you worksheet name, not the table name.
  Set Ws = Worksheets("TheTable")
 
  Ws.Activate
 
  ' Change this to where you want the CSV file saved.
  ' Omit the '\' at the end.
  strPath = ActiveWorkbook.Path
 
  ' Thsi copies the worksheet to a new workbook with just one worksheet.
  Ws.Copy
 
  ' The new workbook and single sheet with the table is now the active sheet.
  With ActiveSheet
 
    ' This makes reference to the FIRST table on the new worksheet
    ' so the table name is NOT required.
    With .ListObjects(1)
      ' Convert the table to a simple range.
      .Unlist
    End With
   
    ' Delete the first row of the worksheet.
    .Rows(1).Delete
      
  End With
 
  With ActiveWorkbook
   
    ' Change this line to state the CSV filename.
    ' This bit "\" & Format(Now(), "HHMMSS")
    .SaveAs Filename:=strPath & "\" & Format(Now(), "HHMMSS"), _
      FileFormat:=xlCSV, _
      CreateBackup:=False, _
      Local:=True

    .Close False
 
  End With

End Sub
Did it works with you ?
I have tried it but it did not work .
It is okey no problem ,thanks .
Which code that i have to use to overwrite the file , if it has the same name .
 
Upvote 0
Did it works with you ?
I have tried it but it did not work .
It is okey no problem ,thanks .
Which code that i have to use to overwrite the file , if it has the same name .
Can you post your code using the VBA Quick Wrap selection on the Mr Excel ribbon?

What filename are you giving the file?

My code used the time as that changes every second but only to demonstrate that it works to you.

Add these first three lines of code above the With ActiveWorkbook line.

This will delete the file before it is recreated.

VBA Code:
 On Error Resume Next
  Kill (strPath & "\" & Format(Now(), "HHMMSS"))
  On Error GoTo 0
    
  With ActiveWorkbook
 
Upvote 0
Can you post your code using the VBA Quick Wrap selection on the Mr Excel ribbon?

What filename are you giving the file?

My code used the time as that changes every second but only to demonstrate that it works to you.

Add these first three lines of code above the With ActiveWorkbook line.

This will delete the file before it is recreated.

VBA Code:
 On Error Resume Next
  Kill (strPath & "\" & Format(Now(), "HHMMSS"))
  On Error GoTo 0
 
  With ActiveWorkbook
VBA Code:
Sub SaveSheetAsCSV()
    Dim ws As Worksheet
    Dim folderPath As String
    Dim fileName As String
    Dim fullpath As String
 
    ' Read the folder path and file name from specified cells
    folderPath = Range("R29").Value
    fileName = Range("R30").Value
 
    ' Ensure the folder path ends with a backslash
    If Right(folderPath, 1) <> "\" Then
        folderPath = folderPath & "\"
    End If
    dt = Format(CStr(Now), "dd.mm.yyyy_hh.mm")
    ' Set the full path for the CSV file
    fullpath = folderPath & dt & "_" & fileName & ".csv"
 
    ' Save the active sheet as a CSV file
    Set ws = Sheets("output")
    ws.Copy
    ActiveSheet.ListObjects("results").ShowHeaders = False
    ActiveWorkbook.SaveAs fileName:=fullpath, FileFormat:=xlCSV, CreateBackup:=False, Local:=True
    ActiveWorkbook.Close SaveChanges:=False
 
 
End Sub

This is above my code and it works well

I am using a button at specific sheet but it implements this code on another sheet called output .
output is my sheet name
results is my table name

But you have suggestions that I have to use

Set ws = Sheets("output")

My table name instead of sheet name but I have tested it and it does not work with me .

Also could you please add the over write file method to save the csv file . I am not sure where should I add lines in my code ?
and edit the code with table name .

Thanks a lot
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,447
Messages
6,172,203
Members
452,448
Latest member
Tupacandres

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