Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- Sub Init()
- Dim i, j As Long
- Dim ItemCode As String
- Dim Content, ItemCodes, Item As Variant
- Content = ReadFileToArray("test.csv")
- ReDim ItemCodes(0)
- For i = LBound(Content, 1) To UBound(Content, 1)
- ' For j = LBound(Content) To UBound(Content) - 1
- ' Next j
- ItemCode = Content(i, 0)
- If IsNumeric(ItemCode) And Not InArray(ItemCodes, ItemCode) Then
- AddToArray ItemCodes, ItemCode
- End If
- Next i
- For Each Item In ItemCodes
- ' Debug.Print Item
- Next Item
- Set Content = Nothing
- Set ItemCodes = Nothing
- End Sub
- Function ReadFileToArray(ByRef FileName As String) As Variant
- Dim ReturnArray() As String
- Dim NumRows, NumCols, Row, Col As Long
- Dim Lines, OneLine As Variant
- Lines = Split(ReadFile(FileName), vbCrLf)
- NumRows = UBound(Lines)
- OneLine = Split(Lines(0), ";")
- NumCols = UBound(OneLine)
- ReDim ReturnArray(NumRows, NumCols)
- For Row = 0 To NumRows
- If Len(Lines(Row)) > 0 Then
- OneLine = Split(Lines(Row), ";")
- For Col = 0 To NumCols
- ReturnArray(Row, Col) = OneLine(Col)
- Next Col
- End If
- Next Row
- ReadFileToArray = ReturnArray
- Set Lines = Nothing
- Set OneLine = Nothing
- End Function
- Function ReadFile(ByRef FileName As String) As String
- Dim WholeFile As String
- Dim FileNum As Integer
- FileNum = FreeFile
- If FileExists(FileName) Then
- Open GetFilePath(FileName) For Input As FileNum
- WholeFile = Input$(LOF(FileNum), #FileNum)
- Close FileNum
- Else
- MsgBox "File not exists!", vbCritical, "Error"
- End If
- ReadFile = WholeFile
- End Function
- Function GetFilePath(ByRef FileName As String) As String
- Dim Path As String
- Path = ThisWorkbook.Path
- If (Path <> vbNullString) And (Right$(Path, 1) <> "\") Then
- GetFilePath = Path & "\" & FileName
- End If
- End Function
- Function FileExists(ByRef FileName As String) As Boolean
- If Len(Dir$(GetFilePath(FileName))) > 0 Then FileExists = True
- End Function
- Function InArray(ByRef SearchArray As Variant, ByVal Item As String) As Boolean
- Dim ArrayItem As Variant
- If Not IsEmpty(SearchArray) Then
- For Each ArrayItem In SearchArray
- If Item = ArrayItem Then
- ArrayItem = True
- Exit Function
- End If
- Next ArrayItem
- End If
- Set ArrayItem = Nothing
- End Function
- Function AddToArray(ByRef SearchArray, ByVal Item As String)
- Dim i As Long
- If SearchArray(0) <> vbNullString Then
- i = UBound(SearchArray) + 1
- Else
- i = 0
- End If
- ReDim Preserve SearchArray(i)
- SearchArray(i) = Item
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement