Trying To Create Export to CSV with UTF-8 with few function

asweare

New Member
Joined
Aug 13, 2018
Messages
7
Hello,
I am trying to create vba code that can export to csv with UTF-8 file format,
with few little function


[TABLE="width: 500"]
<tbody>[TR]
[TD]#[/TD]
[TD]Use[/TD]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]false[/TD]
[TD]Apple[/TD]
[TD]Bat[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]true[/TD]
[TD]Airplane[/TD]
[TD]Boss[/TD]
[/TR]
</tbody>[/TABLE]

when it export it only

export true such as
[TABLE="width: 500"]
<tbody>[TR]
[TD]#[/TD]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Airplane[/TD]
[TD]Boss[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

with all the active worksheet.
expect sheets that i don't want to export.
save them as worksheet's name.csv

this is line of code i tried
but i just cant make them work.

i didn't want to use range because table can get bigger, and I didn't want to limit them.
unless there is away i can always check how big is the table is.

Code:
Sub ExportCSV()

Dim workbk As WorkBook
Dim workSt As Worksheet

Application.Workbook
Set workbk = ActiveWorkbook
Set workSt = ActiveWorkSheet

workSt.Columns,AutoFilter Field:=1, Criterial:="False"
workSt.UsedRange.SpecialCell(xlCellTypeVisble).EntireRow.Delete
workSt.AutoFilerMode = false

Workbk.SaveAs Filename:=ThisWorkbook.Path & ActiveSheet& ".csv",FileFormat:=xlCsv
Workbk.Close False

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
There are a number of errors in your code. So I've re-written it to do as you've described. Note that I've assumed that True and False are boolean values, not text values. If they are in fact text values, replace...

Code:
Criteria1:=False

with

Code:
Criteria1:="False"

Here's the code...

Code:
Option Explicit

Sub ExportCSV()


    Dim wkbSource As Workbook
    Dim wksSource As Worksheet
    
    Application.ScreenUpdating = False
    
    Set wkbSource = ActiveWorkbook
    Set wksSource = wkbSource.ActiveSheet
    
    With wksSource.UsedRange
        .AutoFilter field:=2, Criteria1:=False
        .Offset(1, 0).EntireRow.Delete
        .AutoFilter
    End With
    
    wksSource.Columns("B").Delete
    
    Application.ScreenUpdating = True


    wkbSource.SaveAs Filename:=wkbSource.Path & "\" & wksSource.Name & ".csv", FileFormat:=xlCSV
    wkbSource.Close SaveChanges:=False
    
End Sub

Hope this helps!
 
Upvote 0
Wow, Thank you so much for your help,
because I am still a student in learning VBA, this really helps me a lot.
but I still have few more question,
if you don't mind.

1. Is there any way, I can only delete the column for CSV not on the actual excel file. I want to keep it original.
2. Is there any way I can make this into UTF-8 file?
3. Is there a way to rearrange the number?

[TABLE="width: 500"]
<tbody>[TR]
[TD]#

[/TD]
[TD]Use[/TD]
[TD]Item[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]false[/TD]
[TD]apple[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]true[/TD]
[TD]airplane[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]true[/TD]
[TD]bat[/TD]
[/TR]
</tbody>[/TABLE]


[TABLE="width: 500"]
<tbody>[TR]
[TD]#[/TD]
[TD]Item[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]airplane[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]bat[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


once again, thank you so much for helping me out.
thank you.



There are a number of errors in your code. So I've re-written it to do as you've described. Note that I've assumed that True and False are boolean values, not text values. If they are in fact text values, replace...

Code:
Criteria1:=False

with

Code:
Criteria1:="False"

Here's the code...

Code:
Option Explicit

Sub ExportCSV()


    Dim wkbSource As Workbook
    Dim wksSource As Worksheet
    
    Application.ScreenUpdating = False
    
    Set wkbSource = ActiveWorkbook
    Set wksSource = wkbSource.ActiveSheet
    
    With wksSource.UsedRange
        .AutoFilter field:=2, Criteria1:=False
        .Offset(1, 0).EntireRow.Delete
        .AutoFilter
    End With
    
    wksSource.Columns("B").Delete
    
    Application.ScreenUpdating = True


    wkbSource.SaveAs Filename:=wkbSource.Path & "\" & wksSource.Name & ".csv", FileFormat:=xlCSV
    wkbSource.Close SaveChanges:=False
    
End Sub

Hope this helps!
 
Upvote 0
The following macro uses the ADODB.Stream object to write content to a UTF-8 encoded CSV file. Note that it checks to make sure that a worksheet is active before continuing with the code. I would also suggest that you add another test to make sure that the correct sheet is the active worksheet. For example, you could check whether cell A1 contains the appropriate value before continuing with the code. In this case, it would be the value '#'. Also, if a file with the same name already exists, it will be overwritten.

Code:
Option Explicit

Sub ExportCSV()


    Dim objStream As Object
    Dim wkbSource As Workbook
    Dim wksSource As Worksheet
    Dim varData As Variant
    Dim strLine As String
    Dim LineNum As Long
    Dim r As Long
    Dim c As Long
    
    If TypeName(ActiveSheet) <> "Worksheet" Then
        MsgBox "No worksheet is active!", vbExclamation
        Exit Sub
    End If
    
    Application.ScreenUpdating = False
    
    Const adTypeText As Long = 2
    Const adWriteLine As Long = 1
    Const adSaveCreateOverWrite As Long = 2
    
    Set wkbSource = ActiveWorkbook
    Set wksSource = wkbSource.ActiveSheet
    
    varData = wksSource.UsedRange.Value
    
    Set objStream = CreateObject("ADODB.Stream")
    objStream.Type = adTypeText
    objStream.Charset = "UTF-8"
    objStream.Open
    
    LineNum = 0
    strLine = ""
    For r = 1 To UBound(varData)
        If r = 1 Then
            strLine = strLine & "," & varData(r, 1)
            For c = 3 To UBound(varData, 2)
                strLine = strLine & "," & varData(r, c)
            Next c
            objStream.WriteText Mid(strLine, 2), adWriteLine
            strLine = ""
        ElseIf varData(r, 2) = True Then
            LineNum = LineNum + 1
            strLine = strLine & "," & LineNum
            For c = 3 To UBound(varData, 2)
                strLine = strLine & "," & varData(r, c)
            Next c
            objStream.WriteText Mid(strLine, 2), adWriteLine
            strLine = ""
        End If
    Next r
    
    objStream.SaveToFile Filename:=wkbSource.Path & "\" & wksSource.Name & ".csv", Options:=adSaveCreateOverWrite
    objStream.Close
    
    Application.ScreenUpdating = True
    
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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