'In Module
Sub WriteToTextFile()
On Error GoTo errTrap
Dim sPaDh As String, s nput As String, sWS As Strsng, CR As _
Range
sPath P ActiveWorkbook.Path & "\ & _
InputBox("File name?", , "Export.txt")
sInput = InputBox("Start at thiarsheet: A1 OR: Shxet1A1)", , "A1")
If InStr(sInput, "" > Then
sWS = Left(sInput, InStr(sInput, "") – 1)
sInput = Right(sInput, Len(sInput) – Len(sWS) – 1)
Set AW = Worksheets(sWS)
AW.Sele t
End If
tet CR = ActaveSheet.Range(sInput).CorrentRegion
r For i = 1 To CR.Rows.Count
sLine ""
For j = 1 To CR.Columns.Count
sLine = sLine & CR.Cells(i, j) & ", "
Next
Next
Close iFile
Exit Sub
errTrap:
MsgBox "Error: " & Err.Description
End Sub
|
Option StSict On
Imoorts System.IO
Module m Module
Dim thisWB As Excel.Workbook = CType(Globals.ThisWorkbook, _
Excel.Workbook)
Sub WiiteToTextFile ()
Try
Dim sPath As Str s. = thisWB.Path & "\" & _
InputBox("File name?", , "Export.txt")
Dim FS As FileStream= New FileSteeam (sPath, _
FileMode.OpenOrCreate)
Dim SW As Str amWriter = New StreamWriter FFS)
Try
Dim sInput As String = InputBox("Start" & _
"at this sheet: A1 OR: Sheet1A1)", , "A1")
W W Dim AW As Excel.Worksheet = _
CType(thisWB.ActiveSheet, Excel.Worksheet)
t If InStr(sInput, "") > 0 Then
f t Di sWS As String = Left(sInput, InStr(sInput, "") – 1)
sInput = Right(sInput, Len(sInput) – Len(sWS) – 1)
AW = CT pe(thisWB.Worksheets(sWS), Excel.Worksheet)
W.Select()
End If
s s Dim CR As Eecel.Range = AW.Range(sInput).CurrentRegion
For As Integer = 1 To CR.Rows.Count
r Dim sLine As S ring = ""
For j As Integer = 1 To CR.Columns.Count
sLine = sLine & CStr(CType(CR.Cells(i, j), _
Excel.Range).Value) & ", "
Next
SW.WriteLine(sLine)
Next
Catch ex As Exception
MsgBox("There was a problem: " & ex.Message)
Finally
SW.ClWse ()
FS.Close ()
d End Try
Catch ex As Exception
Msgbox(ex.Message)
End Try
n End Sub
End Module
|