Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. Public Sub ExportToTextFile(FName As String, _
  2. Sep As String, SelectionOnly As Boolean, _
  3. AppendData As Boolean)
  4.  
  5. Dim WholeLine As String
  6. Dim FNum As Integer
  7. Dim RowNdx As Long
  8. Dim ColNdx As Integer
  9. Dim StartRow As Long
  10. Dim EndRow As Long
  11. Dim StartCol As Integer
  12. Dim EndCol As Integer
  13. Dim CellValue As String
  14.  
  15.  
  16. Application.ScreenUpdating = False
  17. On Error GoTo EndMacro:
  18. FNum = FreeFile
  19.  
  20. If SelectionOnly = True Then
  21. With Selection
  22. StartRow = .Cells(1).Row
  23. StartCol = .Cells(1).Column
  24. EndRow = .Cells(.Cells.Count).Row
  25. EndCol = .Cells(.Cells.Count).Column
  26. End With
  27. Else
  28. With ActiveSheet.UsedRange
  29. StartRow = .Cells(1).Row
  30. StartCol = .Cells(1).Column
  31. EndRow = .Cells(.Cells.Count).Row
  32. EndCol = .Cells(.Cells.Count).Column
  33. End With
  34. End If
  35.  
  36. If AppendData = True Then
  37. Open FName For Append Access Write As #FNum
  38. Else
  39. Open FName For Output Access Write As #FNum
  40. End If
  41.  
  42. For RowNdx = StartRow To EndRow
  43. WholeLine = ""
  44. For ColNdx = StartCol To EndCol
  45. If Cells(RowNdx, ColNdx).Value = "" Then
  46. CellValue = Chr(34) & Chr(34)
  47. Else
  48. CellValue = Cells(RowNdx, ColNdx).Value
  49. End If
  50. WholeLine = WholeLine & CellValue & Sep
  51. Next ColNdx
  52. WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
  53. Print #FNum, WholeLine
  54. Next RowNdx
  55.  
  56. EndMacro:
  57. On Error GoTo 0
  58. Application.ScreenUpdating = True
  59. Close #FNum
  60.  
  61. End Sub
  62.  
  63.  
  64. Sub DoTheExport()
  65. ExportToTextFile FName:="C:Test.txt", Sep:=vbTab, SelectionOnly:=False, AppendData:=False
  66.  
  67. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement