Advertisement
nandordudas

ReadFromFile

Apr 24th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Sub Init()
  4.  
  5.   Dim i, j As Long
  6.   Dim ItemCode As String
  7.   Dim Content, ItemCodes, Item As Variant
  8.  
  9.   Content = ReadFileToArray("test.csv")
  10.   ReDim ItemCodes(0)
  11.  
  12.   For i = LBound(Content, 1) To UBound(Content, 1)
  13.  
  14. '    For j = LBound(Content) To UBound(Content) - 1
  15.  
  16. '    Next j
  17.  
  18.     ItemCode = Content(i, 0)
  19.  
  20.     If IsNumeric(ItemCode) And Not InArray(ItemCodes, ItemCode) Then
  21.       AddToArray ItemCodes, ItemCode
  22.     End If
  23.  
  24.   Next i
  25.  
  26.   For Each Item In ItemCodes
  27. '    Debug.Print Item
  28.  Next Item
  29.  
  30.   Set Content = Nothing
  31.   Set ItemCodes = Nothing
  32.  
  33. End Sub
  34.  
  35. Function ReadFileToArray(ByRef FileName As String) As Variant
  36.  
  37.   Dim ReturnArray() As String
  38.   Dim NumRows, NumCols, Row, Col As Long
  39.   Dim Lines, OneLine As Variant
  40.  
  41.   Lines = Split(ReadFile(FileName), vbCrLf)
  42.   NumRows = UBound(Lines)
  43.   OneLine = Split(Lines(0), ";")
  44.   NumCols = UBound(OneLine)
  45.  
  46.   ReDim ReturnArray(NumRows, NumCols)
  47.  
  48.   For Row = 0 To NumRows
  49.  
  50.     If Len(Lines(Row)) > 0 Then
  51.  
  52.       OneLine = Split(Lines(Row), ";")
  53.  
  54.       For Col = 0 To NumCols
  55.         ReturnArray(Row, Col) = OneLine(Col)
  56.       Next Col
  57.  
  58.     End If
  59.  
  60.   Next Row
  61.  
  62.   ReadFileToArray = ReturnArray
  63.  
  64.   Set Lines = Nothing
  65.   Set OneLine = Nothing
  66.  
  67. End Function
  68.  
  69. Function ReadFile(ByRef FileName As String) As String
  70.  
  71.   Dim WholeFile As String
  72.   Dim FileNum As Integer
  73.  
  74.   FileNum = FreeFile
  75.  
  76.   If FileExists(FileName) Then
  77.  
  78.     Open GetFilePath(FileName) For Input As FileNum
  79.       WholeFile = Input$(LOF(FileNum), #FileNum)
  80.     Close FileNum
  81.  
  82.   Else
  83.    
  84.     MsgBox "File not exists!", vbCritical, "Error"
  85.    
  86.   End If
  87.  
  88.   ReadFile = WholeFile
  89.  
  90. End Function
  91.  
  92. Function GetFilePath(ByRef FileName As String) As String
  93.  
  94.   Dim Path As String
  95.  
  96.   Path = ThisWorkbook.Path
  97.  
  98.   If (Path <> vbNullString) And (Right$(Path, 1) <> "\") Then
  99.     GetFilePath = Path & "\" & FileName
  100.   End If
  101.  
  102. End Function
  103.  
  104. Function FileExists(ByRef FileName As String) As Boolean
  105.  
  106.   If Len(Dir$(GetFilePath(FileName))) > 0 Then FileExists = True
  107.  
  108. End Function
  109.  
  110. Function InArray(ByRef SearchArray As Variant, ByVal Item As String) As Boolean
  111.  
  112.   Dim ArrayItem As Variant
  113.  
  114.   If Not IsEmpty(SearchArray) Then
  115.  
  116.     For Each ArrayItem In SearchArray
  117.  
  118.       If Item = ArrayItem Then
  119.         ArrayItem = True
  120.         Exit Function
  121.       End If
  122.  
  123.     Next ArrayItem
  124.  
  125.   End If
  126.  
  127.   Set ArrayItem = Nothing
  128.  
  129. End Function
  130.  
  131. Function AddToArray(ByRef SearchArray, ByVal Item As String)
  132.  
  133.   Dim i As Long
  134.  
  135.   If SearchArray(0) <> vbNullString Then
  136.     i = UBound(SearchArray) + 1
  137.   Else
  138.     i = 0
  139.   End If
  140.  
  141.   ReDim Preserve SearchArray(i)
  142.  
  143.   SearchArray(i) = Item
  144.  
  145. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement