Export Rang To Text File With VBA

ho_gh_m

New Member
Joined
Oct 21, 2023
Messages
6
Office Version
  1. 2021
Platform
  1. Windows
I have a sheet that has some columns and rows.
I want to save a separate text file on the desktop with one click from the range of each column.
I have a code but it works incompletely
This code only stores the columns without the range in one file, but I need the range of each column to be stored in a separate text file.
Please Help
Thank you

My Code :

VBA Code:
Sub AnExample()
If ExportRange(Intersect(ActiveSheet.UsedRange, Columns("B")), "GetDesktop()" & _
Format(Date, "ddmmyy") & ".txt", Chr$(9)) Then MsgBox "File Saved"
End Sub
Function ExportRange(ByVal TheRange As Range, ByVal TheFile As String, Optional ByVal _
vDelimiter As String = ",") As Boolean
Dim URArr(), i As Long, j As Long, vFF As Long, ExpArr() As String
On Error GoTo QuitFunc
URArr = TheRange.Value
ReDim ExpArr(1 To UBound(URArr, 1))
For i = 1 To UBound(URArr, 1)
For j = 1 To UBound(URArr, 2) - 1
ExpArr(i) = ExpArr(i) & URArr(i, j) & vDelimiter
Next 'j
ExpArr(i) = ExpArr(i) & URArr(i, UBound(URArr, 2))
Next 'i
vFF = FreeFile
Open TheFile For Output As #vFF
For i = 1 To UBound(ExpArr)
Print #vFF, ExpArr(i)
Next 'i
Close #vFF
ExportRange = True
Exit Function
QuitFunc:
End Function
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I have a sheet that has some columns and rows.
I want to save a separate text file on the desktop with one click from the range of each column.
I have a code but it works incompletely
This code only stores the columns without the range in one file, but I need the range of each column to be stored in a separate text file.
Please Help
Thank you

My Code :

VBA Code:
Sub AnExample()
If ExportRange(Intersect(ActiveSheet.UsedRange, Columns("B")), "GetDesktop()" & _
Format(Date, "ddmmyy") & ".txt", Chr$(9)) Then MsgBox "File Saved"
End Sub
Function ExportRange(ByVal TheRange As Range, ByVal TheFile As String, Optional ByVal _
vDelimiter As String = ",") As Boolean
Dim URArr(), i As Long, j As Long, vFF As Long, ExpArr() As String
On Error GoTo QuitFunc
URArr = TheRange.Value
ReDim ExpArr(1 To UBound(URArr, 1))
For i = 1 To UBound(URArr, 1)
For j = 1 To UBound(URArr, 2) - 1
ExpArr(i) = ExpArr(i) & URArr(i, j) & vDelimiter
Next 'j
ExpArr(i) = ExpArr(i) & URArr(i, UBound(URArr, 2))
Next 'i
vFF = FreeFile
Open TheFile For Output As #vFF
For i = 1 To UBound(ExpArr)
Print #vFF, ExpArr(i)
Next 'i
Close #vFF
ExportRange = True
Exit Function
QuitFunc:
End Function
Do you want to select a cell in a column that exports just that column to a text file or select a cell in a range that then
exports each column in the range to a seperate text file?
 
Upvote 0
Do you want to select a cell in a column that exports just that column to a text file or select a cell in a range that then
exports each column in the range to a seperate text file?
Look at the picture
I want to save a separate text file from each range with the name of the column on the desktop with one click.
For example:
in column B with the name MIK from the range B4:B6 , in column C with the name EOW from the range C5:C7 and in column D with the name RWO from the range D8:D10
 

Attachments

  • Screenshot 2023-10-23 163053.png
    Screenshot 2023-10-23 163053.png
    32.1 KB · Views: 8
Upvote 0
Look at the picture
I want to save a separate text file from each range with the name of the column on the desktop with one click.
For example:
in column B with the name MIK from the range B4:B6 , in column C with the name EOW from the range C5:C7 and in column D with the name RWO from the range D8:D10
Just columns B to D or any column?

Where do the rows for the ranges come from?
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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