Guest User

Untitled

a guest
Apr 11th, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Private m_filepath As String
  4. Private m_delimiter As String
  5. Private m_numberOfLines As Long
  6. Private m_numberOfColumns As Long
  7. Private m_data() As String
  8. Public Sub initalize(filepath As String, delimiter As String)
  9. m_filepath = filepath
  10. m_delimiter = delimiter
  11. End Sub
  12. Public Sub read(filepath As String, delimiter As String)
  13. Call initalize(filepath, delimiter)
  14. Call readData
  15. End Sub
  16. Private Sub readData()
  17. Dim sDelimiter As String * 1
  18. Dim SingleLineString As String
  19. Dim ParsedSingleLineString() As String
  20. Dim ParsedLineStrings() As String
  21. Dim r As Long
  22. Dim fn As Long
  23. Dim NewText As String
  24.  
  25. On Error GoTo ErrorHandle
  26.  
  27. 'sSourceFile doesn't exist
  28. If Len(Dir(m_filepath)) = 0 Then GoTo ErrorHandle2
  29.  
  30. 'Identifies the delimiter
  31. If UCase(m_delimiter) = "TAB" Or UCase(m_delimiter) = "T" Then
  32. sDelimiter = Chr(9)
  33. Else
  34. sDelimiter = Left(m_delimiter, 1)
  35. End If
  36.  
  37. Call Correct_Lf_to_CrLf(m_filepath)
  38.  
  39. fn = openFile
  40. r = 0
  41. Do While Not EOF(fn)
  42. Line Input #fn, SingleLineString
  43. If SingleLineString = "" Then
  44. m_numberOfLines = r
  45. Exit Do
  46. Else
  47. r = r + 1
  48. End If
  49. Loop
  50.  
  51. m_numberOfLines = r
  52. ReDim m_data(1 To r, 1 To 1)
  53.  
  54. fn = openFile
  55. r = 0
  56.  
  57. For r = 1 To m_numberOfLines
  58. Line Input #fn, SingleLineString
  59. Call ParseDelimitedString(SingleLineString, sDelimiter, r)
  60. Next r
  61.  
  62. Close #fn
  63. Exit Sub
  64. ErrorHandle:
  65. MsgBox Err.Description & " Error while reading " + m_filepath
  66. End
  67. ErrorHandle2:
  68. MsgBox Err.Description & " File " & m_filepath & " does not exist!"
  69. End
  70. End Sub
  71. Private Function openFile() As Long
  72. Dim fn As Long, fLen As Long
  73. fn = FreeFile 'Gets a free file number from the operating system
  74. Open m_filepath For Input As #fn
  75. fLen = LOF(fn)
  76. openFile = fn
  77. End Function
  78. Private Sub Correct_Lf_to_CrLf(ByVal filepath As String)
  79. Dim DataLine As String, ReplaceFile As Boolean, fn1 As Long, fn2 As Long
  80.  
  81. ReplaceFile = False
  82. fn1 = FreeFile
  83. Open Environ("TEMP") & "\temp.txt" For Output As #fn1
  84. fn2 = FreeFile
  85. Open filepath For Input As #fn2
  86. While Not EOF(fn2)
  87. Line Input #fn2, DataLine
  88. If InStr(DataLine, vbLf) > 0 Then
  89. Print #fn1, Replace(DataLine, vbLf, vbNewLine)
  90. ReplaceFile = True
  91. End If
  92. Wend
  93. Close #fn2
  94. Close #fn1
  95.  
  96. 'NOTE: Your original file will be replaced with the new file where Lf are replaced with CrLf
  97. If ReplaceFile Then
  98. FileSystem.FileCopy Environ("TEMP") & "\temp.txt", filepath
  99. End If
  100. End Sub
  101. Private Function ParseDelimitedString(InputString As String, sDel As String, LineNumber As Long) As Variant
  102. 'Returns a variant array with every element in
  103. 'InputString separated by sDel.
  104.  
  105. Dim i As Integer, j As Integer, colCount As Long
  106. Dim sString As String, sChar As String * 1
  107. Dim ResultArray() As Variant
  108. Dim CheckForDelimiter As Boolean
  109. Dim RemoveDoubleQuotes As Boolean
  110.  
  111. On Error GoTo ErrorHandle
  112.  
  113. sString = ""
  114. colCount = 0
  115. RemoveDoubleQuotes = False
  116. CheckForDelimiter = True
  117.  
  118. For i = 1 To Len(InputString)
  119. sChar = Mid$(InputString, i, 1)
  120. If sChar = """" Then
  121. CheckForDelimiter = Not CheckForDelimiter
  122. sString = sString & sChar
  123. RemoveDoubleQuotes = True
  124. ElseIf sChar = sDel And CheckForDelimiter Then
  125. colCount = colCount + 1
  126. Call addToResultArray(sString, ResultArray, colCount, RemoveDoubleQuotes)
  127. sString = ""
  128. RemoveDoubleQuotes = False
  129. Else
  130. sString = sString & sChar
  131. End If
  132. If i = Len(InputString) Then
  133. colCount = colCount + 1
  134. Call addToResultArray(sString, ResultArray, colCount, RemoveDoubleQuotes)
  135. End If
  136. Next i
  137.  
  138. If LineNumber = 1 Then
  139. m_numberOfColumns = colCount
  140. ReDim m_data(1 To m_numberOfLines, 1 To m_numberOfColumns)
  141. End If
  142.  
  143. For j = 1 To m_numberOfColumns
  144. m_data(LineNumber, j) = ResultArray(j)
  145. Next j
  146.  
  147. Exit Function
  148. ErrorHandle:
  149. MsgBox Err.Description & " Error in function ParseDelimitedString."
  150. End Function
  151. Private Function addToResultArray(sString As String, ResultArray As Variant, colCount As Long, RemoveDoubleQuotes As Boolean)
  152. ReDim Preserve ResultArray(1 To colCount)
  153. If RemoveDoubleQuotes Then
  154. ResultArray(colCount) = Mid(sString, 2, Len(sString) - 2)
  155. Else
  156. ResultArray(colCount) = sString
  157. End If
  158. End Function
  159. Public Sub writeToTargetRange(targetRange As Range)
  160. Dim i As Long, j As Long
  161.  
  162. For i = 1 To m_numberOfLines
  163. For j = 1 To m_numberOfColumns
  164. targetRange.Cells(i, j).Value = m_data(i, j)
  165. Next j
  166. Next i
  167. End Sub
Advertisement
Add Comment
Please, Sign In to add comment