Need help new to VBA, working with exporting to csv of course row 1 has more cells then other rows which causes trailing commas for rest of rows

SWAY14

New Member
Joined
Jul 27, 2022
Messages
15
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
  2. MacOS
Hello, Im new to VBA and I am trying to export sheet 1 to csv but since im importing to a system they do not like trailing commas.
I have my code which does a great job of selecting rows with information but since I am using row 1 as headers and it goes all the way to CQ
but I know that all the other records starting at row 2 will end at CN which leaves me with all rows from row 2 forward with 3 trailing commas at the end. The thing is I really only need 1 of the trailing commas for the system to validate my file. Any help will be amazing!


My code
Sub Thistheone()
'
' Macro3 Macro
'

'
Application.DisplayAlerts = False
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.SaveAs FileName:= _
"/Users/sway/Desktop/cleint uploads /Testing2.csv", FileFormat:=xlCSVUTF8, _
CreateBackup:=False
Application.DisplayAlerts = False
ActiveWorkbook.Close True



End Sub

what I am trying to fix when exported
from this
1065,,,,,2,,,
to this
1065,,,,,2,
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Copy the wanted range to a new worksheet via vba , export and then delete the generated worksheet

Or
  1. Read the contents of the csv into a string variable
  2. Split the string into an array with vbcrlf/vbnewline as the delimiter
  3. Iterate over the each character in the first element of the array starting from the right
  4. replace with an empty string until you find something that isn't a comma
  5. Join array back together and overwrite the csv file
 
Upvote 0
Copy the wanted range to a new worksheet via vba , export and then delete the generated worksheet

Or
  1. Read the contents of the csv into a string variable
  2. Split the string into an array with vbcrlf/vbnewline as the delimiter
  3. Iterate over the each character in the first element of the array starting from the right
  4. replace with an empty string until you find something that isn't a comma
  5. Join array back together and overwrite the csv file
Thank you for your input thing is the range will always change can be 2 rows or 50 rows and when I do like 50 to be on the safe side I get 50 rows or just commas trailing at the end
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,013
Members
448,935
Latest member
ijat

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