Mandy_
asked on
Excel VBA Macro export worksheet to ;-separated CSV to user desktop
Dear expert,
i'm looking for a macro, export a worksheet called "export" into a semicolon separated CSV file
to User desktop
Pls see example and attachment
i've found this example. I think still needs to be adjusted a little ???
1. where the file is to be saved (user desktop)
2.Only the worksheet export should be saved
UserId;Name;Comp;LOC;ET;Id entNo;ANo; IF;Date;Ac tion;Produ ct;Version ;SMTP;SMTP 2
S12345;JONES, MIKE;GUL;TEST;I;2431102;O1 57658;S;20 13-05-17 11:52:33;Delete;PMAl;MS;mi ke.jones@g ul.com
S12345;JONES, MIKE;GUL;TEST;I;2431175;O1 57658;S;20 13-05-17 12:50:11;New;PMAL;mike.jon es@gul.com
S12345;JONES, MIKE;GUL;TEST;I;2431192;O1 57658;S;20 13-05-17 13:08:33;Delete;PMAL;MS;mi ke.jones@g ul.com
S12345;JONES, MIKE;GUL;TEST;I;2431193;O1 57658;S;20 13-05-17 13:08:33;Delete;PMAL;MS;;m ike.jones@ gul.com
Thank you so much for your help!!!
Mandy
Export.csv
i'm looking for a macro, export a worksheet called "export" into a semicolon separated CSV file
to User desktop
Pls see example and attachment
i've found this example. I think still needs to be adjusted a little ???
1. where the file is to be saved (user desktop)
2.Only the worksheet export should be saved
Option Explicit
Sub export2csv()
Dim lastColumn As Integer
Dim lastRow As Integer
Dim strString As String
Dim i As Integer, j As Integer
lastColumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
lastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
Open "export.csv" For Output As #1
For i = 1 To lastRow
Cells(i, 1).Select
strString = ""
For j = 1 To lastColumn
If j <> lastColumn Then
strString = strString & Cells(i, j).Value & ";" ' Use semicolon instead of pipe.
Else
strString = strString & Cells(i, j).Value
End If
Next j
Print #1, strString
Next i
Close #1
End Sub
UserId;Name;Comp;LOC;ET;Id
S12345;JONES, MIKE;GUL;TEST;I;2431102;O1
S12345;JONES, MIKE;GUL;TEST;I;2431175;O1
S12345;JONES, MIKE;GUL;TEST;I;2431192;O1
S12345;JONES, MIKE;GUL;TEST;I;2431193;O1
Thank you so much for your help!!!
Mandy
Export.csv
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry... I was mid-editing my comment when the other expert (als315) posted so the edit process failed.
Please find attached an updated workbook will a small amendment.
Q-28520034.xls
Please find attached an updated workbook will a small amendment.
Q-28520034.xls
ASKER
dear fanpages,
thanks for your help. I'm sorry but its still not running. this code below has to be switch to 64bit code. Declare should be mark with ptr_save attribut.
thanks for your help. I'm sorry but its still not running. this code below has to be switch to 64bit code. Declare should be mark with ptr_save attribut.
Private Declare Sub CoTaskMemFree _
Lib "ole32.dll" _
(ByVal pvoid As Long)
Private Declare Function SHGetPathFromIDList _
Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" _
(ByVal Pidl As Long, _
ByVal pszPath As String) As Long
Private Declare Function SHGetSpecialFolderLocation _
Lib "shell32.dll" _
(ByVal hWnd As Long, _
ByVal nFolder As Long, _
ByRef ppidl As Long) As Long
Hi Mandy,
Sorry (again), I was not aware you required 64-bit compliant code.
Please disregard my input. I will not be able to progress with the necessary changes.
I suggest you continue with als315's contribution in preference to my own.
Sorry (again), I was not aware you required 64-bit compliant code.
Please disregard my input. I will not be able to progress with the necessary changes.
I suggest you continue with als315's contribution in preference to my own.
ASKER
dear als315,
your code working fine so far. Would it still possible to remove the spaces? For example sometimes i have data in row 2 to 30 and 151 to 180 and 186 to 220. The empty rows, the spaces between, should be removed. Thanks in advance.
your code working fine so far. Would it still possible to remove the spaces? For example sometimes i have data in row 2 to 30 and 151 to 180 and 186 to 220. The empty rows, the spaces between, should be removed. Thanks in advance.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks and respect . Great work!
I have attached a workbook containing the following code:
Open in new window
A row from your example data is also missing an entry for the "Version" column:
S12345;JONES, MIKE;GUL;TEST;I;2431175;O1
That is...
S12345;JONES, MIKE;GUL;TEST;I;2431175;O1
Also, I believe the contents of the "export.csv" file should actually be as follows:
---
UserId;Name;Comp;LOC;ET;Id
S12345;JONES, MIKE;GUL;TEST;I;2431102;O1
S12345;JONES, MIKE;GUL;TEST;I;2431175;O1
S12345;JONES, MIKE;GUL;TEST;I;2431192;O1
S12345;JONES, MIKE;GUL;TEST;I;2431193;O1
---
That is the first three rows of data do end with a semi-colon because of the absence of the "SMTP2" column data for those rows.
Q-28520034.xls
export.csv