Sub CopySortAndExportToTxt()
Dim sourceSheet As Worksheet
Dim sourceRange As Range
Dim newWorkbook As Workbook
Dim sortedRange As Range
Dim lastDataRow As Long
Dim filePath As String
' Define the source sheet and range in the current workbook
Set sourceSheet = [Link]("Sheet1") ' Update with your sheet name
lastDataRow = [Link]([Link], "A").End(xlUp).Row
Set sourceRange = [Link]("A1:B" & lastDataRow)
' Create a new workbook
Set newWorkbook = [Link]
' Copy the source range to the new workbook
[Link] Destination:=[Link](1).Range("A1")
' Sort the data in the new workbook (assuming sorting by the first column)
With [Link](1).Sort
.[Link] Key:=Range("A1"), Order:=xlAscending
.SetRange [Link](1).UsedRange
.Header = xlYes
.Apply
End With
' Define the sorted range
Set sortedRange = [Link](1).UsedRange
' Define the file path and name for the text file
filePath = "C:\Path\To\Your\[Link]" ' Update this with your desired file path
' Export the sorted range to a text file
[Link] filePath, xlTextWindows
' Close the new workbook without saving changes
[Link] SaveChanges:=False
MsgBox "Data has been sorted and exported to " & filePath, vbInformation
End Sub