Skip to content

Instantly share code, notes, and snippets.

@longtth
Created June 12, 2019 17:13
Show Gist options
  • Save longtth/4b3f248487ba7a19f8ad498dde42fa07 to your computer and use it in GitHub Desktop.
Save longtth/4b3f248487ba7a19f8ad498dde42fa07 to your computer and use it in GitHub Desktop.
'Export unicode text by Pagination.com
'is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
'http://creativecommons.org/licenses/by-nc-sa/4.0/
'
'Based on a work at pagination.com.
Public Const AUTO_SAVE = False
Public Const AUTO_SAVE_SECONDS = 0
Public Const AUTO_SAVE_MINUTES = 1
Public Const AUTO_SAVE_HOURS = 0
Public SystemDelimiter As String
Sub MacroExportSheet()
Dim ws As Worksheet
Dim wb As Workbook
Dim csvNameIn As String
Dim csvNameOut As String
Dim path As String
Dim os As String
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet
Application.DisplayAlerts = False
os = CheckOS
path = wb.path
If path = "" Then
If os = "win" Then
path = Environ$("USERPROFILE")
Else
path = CurDir()
End If
End If
textName = path & SystemDelimiter & ws.Name
textNameUTF8 = path & SystemDelimiter & ws.Name & "_utf8"
On Error GoTo Err
'EXPORT UNICODE TXT
ws.Copy 'creates a new workbook
ActiveWorkbook.SaveAs textName & ".txt", xlUnicodeText
ActiveWorkbook.Close False
Application.DisplayAlerts = True
If AUTO_SAVE = True Then
Application.OnTime VBA.TimeSerial(Hour(Now) + AUTO_SAVE_HOURS, Minute(Now) + AUTO_SAVE_MINUTES, Second(Now) + AUTO_SAVE_SECONDS), "MacroExportSheet"
End If
Exit Sub
Err:
MsgBox Err.Description, vbInformation, "Error"
Resume Next
End Sub
Private Function CheckOS()
If Not Application.OperatingSystem Like "*Mac*" Then
'Windows
SystemDelimiter = "\"
CheckOS = "win"
Else
'Mac
SystemDelimiter = ":"
CheckOS = "mac"
End If
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment