VBA: how to skip a RANGE of columns when exporting to .TXT?

ogassav

New Member
Joined
Dec 7, 2019
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
Dear community,

I have an Excel table to be exported to TXT in Unicode encoding with data separated by tabs (these 2 criteria are important) and i want to skip the range of columns: from C to G. Everything that is before C and after G should be exported.

Which VBA code will be able to do that? It will be good if i'm able to choose the destination folder where the resulting TXT file will be saved.

Thank you!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Wouldn't it just be simpler to create a sheet with a table that excludes those columns and then export that table?
 
Upvote 0
Well, technically this is possible, but the idea is to avoid extra editing due to human factor risks.
 
Upvote 0
Try this.
VBA Code:
Sub Text_Exclude()
'_________________________________________________________________________________________________________________________
'USER DECIDES FILE PATH
'Text File created from the Active Sheet
'Columns you don't want to add to the text file should be added to the line Exclusion=split(,,,,)
'______________________________________________________________________________________________________________________
Dim FSTR As String, H As Long, ColumnN As Long, AR As Variant, Exclusion As Variant, _
Text_Path As String, Delimiter As String, Column_Offset As Long, T As Long, FS As Object, TXT As Object

Text_Path = Application.GetSaveAsFilename 'USER DECIDES FILE PATH

If Text_Path = vbNullString Or Text_Path = "False" Then
    MsgBox ("No input detected. Ending script")
    End
End If

Set FS = CreateObject("Scripting.FileSystemObject")
Set TXT = FS.CreateTextFile(Text_Path, True, True) 'Unicode text file and will overwrite

Delimiter = Chr(9) 'TAB used to delimit array elements
Exclusion = Split("C,D,E,F,G", ",") 'column letters you don't want to parse

With ActiveSheet.UsedRange 'Determine if excluded columns were offset to determine array pasring
    If .Column = 1 Then
        AR = .Value2
    Else
        AR = .Value2
        Column_Offset = .Column - 1 'default value is 0
    End If
End With

For T = LBound(Exclusion) To UBound(Exclusion) 'convert column letters to column numbers

    Exclusion(T) = ActiveSheet.Range(Exclusion(T) & 1).Column - Column_Offset
   
Next T

For H = 1 To UBound(AR, 1)
   
    For ColumnN = 1 To UBound(AR, 2)
   
        If IsError(Application.Match(CStr(ColumnN), Exclusion, 0)) Then 'if column number doesn't exist in the exclusion array
           
            If FSTR = vbNullString Then
                FSTR = AR(H, ColumnN)
            Else
                FSTR = FSTR & Delimiter & AR(H, ColumnN)
            End If
           
        End If
       
    Next ColumnN
   
    TXT.WriteLine (FSTR)
   
    FSTR = vbNullString
   
Next H

TXT.Close

End Sub
 
Upvote 0
You haven't specified which unicode encoding type you want. If UTF-16 LE is acceptable, here's another approach. The following code assumes that the sheet containing the data is the active sheet. First, it creates a temporary copy of the worksheet, then it deletes Columns C through G, inclusive, then it saves it as UTF-16 LE, and then it closes the workbook without saving it. You'll need to change the path and filename accordingly. Also, if a file with the same name already exists, the file will be overwritten. Note that all of this happens in the background, so you won't see any of it happening. Here's the code...

VBA Code:
Option Explicit

Sub Export_to_UTF_16_LE()

    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    ActiveSheet.Copy
    
    Columns("C:G").Delete
    
    ActiveWorkbook.SaveAs Filename:="C:\Users\Domenic\Desktop\sample.txt", FileFormat:=xlUnicodeText
    
    ActiveWorkbook.Close SaveChanges:=False
    
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
    
    MsgBox "Your sheet has been exported!", vbExclamation, "Sheet Exported"

End Sub

Hope this helps!
 
Upvote 0
MoshiM

Thank you so much! Everything works just fine. :)(y)There is only 1 thing that i forgot - this code should also ingore line 1 (not column!) which is a header with column names. Could you please kindly help me with that? (I'm a total noob in macros, sorry :cry:)

Domenic

Thank you for your help too! As for the destination file format i need exactly what is called Unicode Text (*.txt) in the list of formats available when the file is saved. Not sure whether this is UTF-16 LE or not, but this file format is just what i need.
 
Upvote 0
MoshiM

Thank you so much! Everything works just fine. :)(y)There is only 1 thing that i forgot - this code should also ingore line 1 (not column!) which is a header with column names. Could you please kindly help me with that? (I'm a total noob in macros, sorry :cry:)
change
VBA Code:
For H = 1 To UBound(AR, 1)
to
Code:
For H = 2 To UBound(AR, 1)
 
Upvote 0
Yes, the code will save it in that exact file format. Here's the code, which has been amended to include the deletion of the first row...

VBA Code:
Option Explicit

Sub Export_to_UTF_16_LE()

    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    ActiveSheet.Copy
    
    Rows(1).Delete
    
    Columns("C:G").Delete
    
    ActiveWorkbook.SaveAs Filename:="C:\Users\Domenic\Desktop\sample.txt", FileFormat:=xlUnicodeText
    
    ActiveWorkbook.Close SaveChanges:=False
    
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
    
    MsgBox "Your sheet has been exported!", vbExclamation, "Sheet Exported"

End Sub
 
Upvote 0
MoshiM

Thank you million of times, it works just perfectly fine! (y)(y)(y):love:

Domenic

Thank you! However, when macros is run i get the following message: Run-time error '1004': Method 'Save As' of object '_Workbook' failed. What do you think is wrong?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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