Another attempt to geuss what fixer is askig for from here:
https://excelfox.com/forum/showthrea...ll=1#post13256
Code:Sub OpenTxtFiles_ValuesToBeSeperatedIntoExcelCells() ' Comma seperated values text files Call OpenA____SeperatedValuesTextFile("CommaSeperatedValues.csv", ",") Call OpenA____SeperatedValuesTextFile("CommaSeperatedValues.txt", ",") End Sub Sub OpenA____SeperatedValuesTextFile(ByVal Filname As String, ByVal Seprator As String) Rem 1 Get text file as long string. Dim FileNum As Long: Let FileNum = FreeFile(1) ' https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/freefile-function Dim PathAndFileName As String, TotalFile As String Let PathAndFileName = ThisWorkbook.Path & "\" & Filname ' Open PathAndFileName For Binary As #FileNum 'Open Route to data. Binary is a fundemental type data input... TotalFile = Space(LOF(FileNum)) '....and wot recives it hs to be a string of exactly the right length Get #FileNum, , TotalFile Close #FileNum Rem 2 Put values in Excel Dim Ws1 As Worksheet Set Ws1 = ThisWorkbook.Worksheets.Item(1) Ws1.Cells.ClearContents '2b) Split Total File text into a 1 Dimensional array into rows Dim RwTxt() As String: Let RwTxt() = Split(TotalFile, vbCr & vbLf, -1, vbBinaryCompare) Dim Clms() As String Let Clms() = Split(RwTxt(0), Seprator, -1, vbBinaryCompare) ' This will be the first row of data. Here we are doing it just to gat the column count. In the loop below, we will use it for every row, including initially this first row. We need it below to allow us to access each value seperately seperated via the seprator, seprator Dim HedClmsCnt As Long: Let HedClmsCnt = UBound(Clms) + 1 ' +1 is required , as , by default , a 1Dimensional array from split has first element indicie of 0 , so Ubound will be 1 less than the number of elements Dim arrOut() As String ' I must make this dynamic, since i must use the TReDim method to size it. This is because the Dim statement will not accept variables or non static values: It omly accepts actual integer hard coded numbers ReDim arrOut(1 To UBound(RwTxt) + 1, 1 To HedClmsCnt) ' +1 is required , as , by default , a 1Dimensional array from split has first element indicie of 0 , so Ubound will be 1 less than the number of elements Dim RwCnt As Long For RwCnt = 0 To UBound(RwTxt) '2c) Split each row into seperated values Let Clms() = Split(RwTxt(RwCnt), Seprator, -1, vbBinaryCompare) Dim ClmCnt As Long If Not UBound(Clms()) = -1 Then ' This might be the case fo extra rows in the text file with no seperators in s For ClmCnt = 1 To HedClmsCnt Let arrOut(RwCnt + 1, ClmCnt) = Clms(ClmCnt - 1) Next ClmCnt Else End If Next RwCnt Rem 2d) Put values from text file into first worksheet Ws1.Range("A1").Resize(UBound(arrOut(), 1), UBound(arrOut(), 2)) = arrOut() End Sub




Reply With Quote
Bookmarks