VBA - text import puts contents all in one cell, need comma delimit instead

Sayre

Board Regular
Joined
Apr 12, 2005
Messages
180
Hi, I found and modified the below code and corresponding custom function hoping it will work faster than Workbooks.OpenText on large csv file imports (500K rows+ in some cases). It works great but writes all the data to just one cell in the worksheet which obviously won't work for large files.

How do I modify it so it writes it to the sheet more like a proper comma delimited across man rows/columns? I found threads on split function but not sure if that's the way to go or how to incorporate it if it is. Hoping there's a few simple lines I can add to the below script or custom function. If its more complicated than that could anyone at least point me towards a potential solution? Any hep truly appreciated....

Here's the script...
Code:
Sub TxtImporter2()
Dim f As String, flPath As String
Dim i As Long, j As Long
    Dim sPath As String
Dim ws As Worksheet
Application.DisplayAlerts = False
Application.ScreenUpdating = False
flPath = Sheets("START").Cells(9, 1).Value & Application.PathSeparator
i = ThisWorkbook.Worksheets.Count
j = Application.Workbooks.Count
f = Dir(flPath & "*.csv")
Do Until f = ""
Workbooks.Add
 Cells(1, 1) = LoadTextFile2(flPath & f)
    Workbooks(j + 1).Worksheets(1).Copy After:=ThisWorkbook.Worksheets(i)
    ThisWorkbook.Worksheets(i + 1).Name = Left(f, Len(f) - 4)
    Workbooks(j + 1).Close SaveChanges:=False
    i = i + 1
    f = Dir
Loop
Application.DisplayAlerts = True
End Sub


Here's the corresponding function...
Code:
 ' \\ Function to return the full content of a text file as a string
Public Function LoadTextFile2(sFile As String) As String
    Dim iFile As Integer
     
Dim lineSplit As Variant

    ' \\ Use FreeFile to supply a file number that is not already in use
    iFile = FreeFile
     ' \\ ' Open file for input.
    Open sFile For Input As #iFile


     
     ' \\ Return (Read) the whole content of the file to the function
    LoadTextFile2 = Input$(LOF(iFile), iFile)
    'Split(Symbols, ".")
     
    Close #iFile
    
End Function
 
Hi..

If you have a heap of rows.. probably populating an array and writing that array to the sheet at the end will be faster...

Something like: (used on your sample comma separated data form above)

Code:
Sub Import()
    Dim i As Long, ii As Long, x, xx, s, strFile As String, y
    strFile = Application.GetOpenFilename("CSV Files,*.csv")
    If strFile = "False" Then Exit Sub
    s = Split(CreateObject("scripting.filesystemobject").getfile(strFile).openastextstream.readall, vbCrLf)
    ReDim y(1 To UBound(s), 1 To 1)
    For i = 1 To UBound(s) - 1
        xx = Split(s(i - 1), ",")
        ReDim Preserve y(1 To UBound(s), 1 To UBound(xx))
        For ii = 1 To UBound(xx)
            y(i, ii) = xx(ii - 1)
        Next ii
    Next i
    Sheets("Sheet1").Cells(1, 1).Resize(UBound(y, 1), UBound(y, 2)).Value = y
    With Sheets("Sheet1").UsedRange
        x = .Replace("""", "", xlPart)
    End With
End Sub
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi,

…………
In any case I ended up going with a very simplified version that works well enough for my needs:
…………!

Happy you have a working solution. But I take an interest as I am playing and learning with similar stuff……..


It varies. In this case the file is 9 columns and 73 rows but I need the macro to be able to import it regardless of the row/column count.

. My simplified program should have worked, as there were no specific dimensioning. (You would have needed to edit or comment out my first line Option Explicit ). Otherwise the full code simply with Dimensioning things bigger should work: Code:

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> BasicMacroTextFileToExcelLong()<br><SPAN style="color:#007F00">' Very Simply alternative to the Spreadsheet way using Wizzard, Opening as text File in Excel  etc</SPAN><br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">'First Bit  can  be ommited if fielnames are always known. It basically gets all the File info.</SPAN><br><SPAN style="color:#007F00">'Note text File And Excel File must not be in the same Directory as you are being asked for it here.</SPAN><br><SPAN style="color:#00007F">On</SPAN> Err <SPAN style="color:#00007F">GoTo</SPAN> TheEnd <SPAN style="color:#007F00">' if anything goes wrong go to the end rather than crasching!</SPAN><br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">'First Bit: TextDataFileName and seperator input</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> Sp <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> <SPAN style="color:#007F00">' Allocate memory for The seperater used by the Text file</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> DtaFleNm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN> <SPAN style="color:#007F00">'The DataFilename itself is a string. But a vasriant is specified as you may have a Boolean input in the Dialogue boy if the user cancels</SPAN><br>  DtaFleNm = Application.GetOpenFilename(FileFilter:="Hallo,*.txt,") <SPAN style="color:#007F00">'The Application Method GetOpenFilename Displays the standard <SPAN style="color:#00007F">Open</SPAN> dialog box. The optional arguments:= writes a name,limits type of files looked for</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> DtaFleNm = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'Stops Porgram if User Cancels (which gives the boolean False)</SPAN><br>  <SPAN style="color:#00007F">Let</SPAN> Sp = Application.InputBox("Enter a separator character.", Type:=2) <SPAN style="color:#007F00">'prompts user for separator character</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> Sp = vbNullString <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'Stops Porgram if User Cancels (which gives an empty string)</SPAN><br><SPAN style="color:#007F00">'End First Bit TextDataFileName and seperator input</SPAN><br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">'    'If you want to skip the first bit, delete it or comment it out and type in the text File path and seperator in lines like this:</SPAN><br><SPAN style="color:#007F00">'    Dim DtaFleNm As String</SPAN><br><SPAN style="color:#007F00">'    Let DtaFleNm = ThisWorkbook.Path & "\TextDataFile.txt"</SPAN><br><SPAN style="color:#007F00">'    Dim Sp As String</SPAN><br><SPAN style="color:#007F00">'    Let Sp = "|"</SPAN><br><SPAN style="color:#007F00">'    'Note: If you do it this way you should put the Active Excel File and Data Text File in the same Directory</SPAN><br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">'Start Main Bit: Get data from text file  line by line, then for each line go through every Column. As each data bit is found it is put in the approriate place in the Excel File</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> Rw <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, StCm  <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, Cm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> <SPAN style="color:#007F00">'Before you start You should select cell</SPAN><br><SPAN style="color:#00007F">Let</SPAN> StCm = ActiveCell.Column <SPAN style="color:#007F00">'where you want to start data to come in. Column is then reset in loop</SPAN><br><SPAN style="color:#00007F">Let</SPAN> Rw = ActiveCell.Row <SPAN style="color:#007F00">'Row increases downwards so the initial and loop caount can be same variable</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> DtaFleLn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> TxtPs <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> <SPAN style="color:#007F00">'text character position in text file looking form left</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> SpPs <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> <SPAN style="color:#007F00">'position of seperater lookinf from left</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> TxtDta <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> <SPAN style="color:#007F00">'String used temporarily for each Text data value</SPAN><br>  Open DtaFleNm <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Input</SPAN> <SPAN style="color:#00007F">As</SPAN> 3 <SPAN style="color:#007F00">' Sort of opens up a data highway and gives it route number (here 3) to distinguisch it from any others currently open. (If you do not know how many Highways are already open and have a number, then use instead of a number someting like #HgWyNm here, and HgWyNm everyhwere where the number is. This gives it the next available number (Remember to Dim it at the start also.))</SPAN><br>    <SPAN style="color:#00007F">Do</SPAN> <SPAN style="color:#00007F">Until</SPAN> EOF(3) <SPAN style="color:#007F00">'Do until going your at the end of the data highway</SPAN><br>    Line <SPAN style="color:#00007F">Input</SPAN> #3, DtaFleLn <SPAN style="color:#007F00">'Bring in a line from text file(Automatically goes to next line or next part of highway after this command. So we need mo row count for the data file.)</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> Right(DtaFleLn, 1) <> Sp <SPAN style="color:#00007F">Then</SPAN> DtaFleLn = DtaFleLn & Sp <SPAN style="color:#007F00">'Glue a seperator on the end of the data file line if there is not one there</SPAN><br>    <SPAN style="color:#00007F">Let</SPAN> Cm = StCm <SPAN style="color:#007F00">'go to first column</SPAN><br>    <SPAN style="color:#00007F">Let</SPAN> TxtPs = 1 <SPAN style="color:#007F00">' Initial Data Text Position is at 1 (Ist Left poisition in text file line</SPAN><br>    <SPAN style="color:#00007F">Let</SPAN> SpPs = InStr(TxtPs, DtaFleLn, Sp) <SPAN style="color:#007F00">'Find position of next seperator</SPAN><br>      <SPAN style="color:#00007F">While</SPAN> SpPs >= 1 <SPAN style="color:#007F00">'As long as a next Seperator position is found, we go througth columns looking for data</SPAN><br>      <SPAN style="color:#00007F">Let</SPAN> TxtDta = Mid(DtaFleLn, TxtPs, SpPs - TxtPs) <SPAN style="color:#007F00">'this gives the next data value</SPAN><br>      <SPAN style="color:#00007F">Let</SPAN> ActiveSheet.Cells(Rw, Cm).NumberFormat = "@" <SPAN style="color:#007F00">'This is not allways necerssary but can help avoid annoying problems with data turning into times or dates</SPAN><br>      <SPAN style="color:#00007F">Let</SPAN> ActiveSheet.Cells(Rw, Cm).Value = TxtDta <SPAN style="color:#007F00">'Put Text data in excel cell</SPAN><br>      <SPAN style="color:#00007F">Let</SPAN> ActiveSheet.Cells(Rw, Cm).Value = Trim(ActiveSheet.Cells(Rw, Cm).Value) <SPAN style="color:#007F00">'Most not necerssary but can help to clean up data if any extra spaces came in</SPAN><br>      <SPAN style="color:#00007F">Let</SPAN> TxtPs = SpPs + 1 <SPAN style="color:#007F00">'Start of next data is just after next seperator</SPAN><br>      <SPAN style="color:#00007F">Let</SPAN> Cm = Cm + 1 <SPAN style="color:#007F00">'Increase Excel Column number</SPAN><br>      <SPAN style="color:#00007F">Let</SPAN> SpPs = InStr(TxtPs, DtaFleLn, Sp) <SPAN style="color:#007F00">'Get the next Seperator position by starting at the current data and going right until the next seperator is found</SPAN><br>      <SPAN style="color:#00007F">Wend</SPAN> <SPAN style="color:#007F00">'go to next text data to the right in the data file Line</SPAN><br>    <SPAN style="color:#00007F">Let</SPAN> Rw = Rw + 1 <SPAN style="color:#007F00">'Increase row number for Excel(The tecxt file automatically goes to the next row after reading a line</SPAN><br>    <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#007F00">'Go to the next Row in the Data Text file</SPAN><br>TheEnd:<br>  <SPAN style="color:#00007F">Close</SPAN> 3 <SPAN style="color:#007F00">'Very important to do this. Shuts highway Off. There can be strange errors if you do not do that. (It is here so that even after an error it will be done</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'BasicMacroTextFileToExcelLong()</SPAN></FONT>


. I have a few other programs, and would gladly experiment a bit later, - as I said it helps me in learning.
. If it was possible to send me a text file, similar to that which did not work, I would be happy to try to see why it does not work in my program, and check it out in others. For example send over these 2 free things: FileSnack | Easy file sharing or Box Net,
Remember to select Share after uploading and give us the link they provide.

Or send me a personal message and I will supply you with my Email.



Alan
 
Upvote 0
It varies. In this case the file is 9 columns and 73 rows but I need the macro to be able to import it regardless of the row/column count.
Give this code a try... it should work for either type of file you showed us. I have not speed tested it, but I think the code should be pretty fast, even if your file is large. Note, though, it assumes a maximum of 20 columns of data... if that guess is wrong, you can change the blue colored number in the code below to a value that is guaranteed to be larger than the maximum number of columns your data has.
Code:
Sub ImportFile()
  Dim R As Long, C As Long, FileNum As Long
  Dim TotalFile As String, PathAndFileName As String, Delim As String
  Dim Lines As Variant, Data As Variant, Fields As Variant
  If Cells.Find("*", , xlFormulas, , xlRows, xlPrevious).Row > 1 Or _
     Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column Or _
     Len(Range("A1").Value) > 0 Then
    MsgBox "You have data on the active worksheet... this code can only be run " & _
           "when the active sheet is empty!", vbCritical, "Worksheet Not Empty"
    Exit Sub
  End If
  FileNum = FreeFile
  PathAndFileName = Application.GetOpenFilename("CSV Files(*.csv),*.csv,All Files (*.*),*.*", 1, "Open CSV File")
  Open PathAndFileName For Binary As #FileNum
    TotalFile = Space(LOF(FileNum))
    Get #FileNum, , TotalFile
  Close #FileNum
  Lines = Split(TotalFile, vbNewLine)
  ReDim Data(1 To UBound(Lines) + 1, 1 To [COLOR=#0000ff][B]20[/B][/COLOR])
  If InStr(TotalFile, vbTab) Then
    Delim = vbTab
  Else
    Delim = ","
  End If
  For R = 0 To UBound(Lines)
    Fields = Split(Lines(R), Delim)
    For C = 0 To UBound(Fields)
      Data(R + 1, C + 1) = Fields(C)
    Next
  Next
  Range("A1").Resize(UBound(Data), UBound(Data, 2)) = Data
End Sub
 
Upvote 0
Hi, Rick



Give this code a try... it should work for either type of file you showed us..........





. I was just working through your code (as I often do as I always learn from them, thanks!). I have a small problem, that is to say it does not work if the active sheet is empty. Either of these two conditions

Cells.Find("*", , xlFormulas, , xlRows, xlPrevious).Row > 1 Or _
Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column

return an error.

. But the modified code

Code:
Sub ImportFile()  Dim R As Long, C As Long, FileNum As Long
  Dim TotalFile As String, PathAndFileName As String, Delim As String
  Dim Lines As Variant, Data As Variant, Fields As Variant
  If Len(Range("A1").Value) > 0 Then
    MsgBox "You have data on the active worksheet... this code can only be run " & _
           "when the active sheet is empty!", vbCritical, "Worksheet Not Empty"
    Exit Sub
  End If
  FileNum = FreeFile
  PathAndFileName = Application.GetOpenFilename("CSV Files(*.csv),*.csv,All Files (*.*),*.*", 1, "Open CSV File")
  Open PathAndFileName For Binary As #FileNum
    TotalFile = Space(LOF(FileNum))
    Get #FileNum, , TotalFile
  Close #FileNum
  Lines = Split(TotalFile, vbNewLine)
  ReDim Data(1 To UBound(Lines) + 1, 1 To 20)
  If InStr(TotalFile, vbTab) Then
    Delim = vbTab
  Else
    Delim = ","
  End If
  For R = 0 To UBound(Lines)
    Fields = Split(Lines(R), Delim)
    For C = 0 To UBound(Fields)
      Data(R + 1, C + 1) = Fields(C)
    Next
  Next
  Range("A1").Resize(UBound(Data), UBound(Data, 2)) = Data
End Sub




. works fine for the CSV data given by the OP.
. It also gives sensible results for the Tab delimited file from the Op, (although they may not be the exact results he wants as some columns are somewhat offset in the final Excel table).

Alan Elston
 
Upvote 0
Hi, Rick

. I was just working through your code (as I often do as I always learn from them, thanks!). I have a small problem, that is to say it does not work if the active sheet is empty. Either of these two conditions

Cells.Find("*", , xlFormulas, , xlRows, xlPrevious).Row > 1 Or _
Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column

return an error.
Hmm! I could have sworn I tested for that, but obviously I did not. Thanks for pointing that out.



But the modified code

Code:
Sub ImportFile()  Dim R As Long, C As Long, FileNum As Long
  Dim TotalFile As String, PathAndFileName As String, Delim As String
  Dim Lines As Variant, Data As Variant, Fields As Variant
  [COLOR=#ff0000][B]If Len(Range("A1").Value) > 0 Then[/B][/COLOR]
    MsgBox "You have data on the active worksheet... this code can only be run " & _
           "when the active sheet is empty!", vbCritical, "Worksheet Not Empty"
    Exit Sub
  End If
  .... < snip > ....
End Sub

works fine for the CSV data given by the OP.
The line of code I highlighted in red above is not a sufficient test... A1 (the intersection of headers) could quite easily be left blank on purpose. This code should be more bullet-proof. I also took the liberty to add an additional line of code to protect against the user clicking the Cancel button on the file selection dialog box.
Code:
Sub ImportFile()
  Dim R As Long, C As Long, FileNum As Long
  Dim TotalFile As String, PathAndFileName As String, Delim As String
  Dim Lines As Variant, Data As Variant, Fields As Variant
  [COLOR=#ff0000][B]If Application.CountA(Cells) Then[/B][/COLOR]
    MsgBox "You have data on the active worksheet... this code can only be run " & _
           "when the active sheet is empty!", vbCritical, "Worksheet Not Empty"
    Exit Sub
  End If
  FileNum = FreeFile
  PathAndFileName = Application.GetOpenFilename("CSV Files(*.csv),*.csv,All Files (*.*),*.*", 1, "Open CSV File")
  Open PathAndFileName For Binary As #FileNum
    [COLOR=#0000ff][B]If PathAndFileName = False Then Exit Sub[/B][/COLOR]
    TotalFile = Space(LOF(FileNum))
    Get #FileNum, , TotalFile
  Close #FileNum
  Lines = Split(TotalFile, vbNewLine)
  ReDim Data(1 To UBound(Lines) + 1, 1 To 20)
  If InStr(TotalFile, vbTab) Then
    Delim = vbTab
  Else
    Delim = ","
  End If
  For R = 0 To UBound(Lines)
    Fields = Split(Lines(R), Delim)
    For C = 0 To UBound(Fields)
      Data(R + 1, C + 1) = Fields(C)
    Next
  Next
  Range("A1").Resize(UBound(Data), UBound(Data, 2)) = Data
End Sub



It also gives sensible results for the Tab delimited file from the Op, (although they may not be the exact results he wants as some columns are somewhat offset in the final Excel table).
I am not sure they are really incorrect offsets... if the file contains a double tab character, I would assume whatever program was used to create the file did so on purpose.
 
Last edited:
Upvote 0
Hi Rick,

…….could have sworn I tested for that, but obviously I did not. Than……..

….. many thanks for replying and clearing that up. It is very helpful because when one is learning one can easily get confused.

……The line of code I highlighted in red above is not a sufficient test... A1 (the intersection of headers) could quite easily be left blank on purpose. This code should be more bullet-proof. ……...

. The new condition line If Application.CountA(Cells) Then works very well, thanks.

...................

……. I also took the liberty to add an additional line of code to protect against the user clicking the Cancel button on the file selection dialog box……..

. I also have that in mine as well as asking for the separator / delimiter. I am often a bit unsure how user friendly to make an answer to an OP. Sometimes it is extremely useful to be given a full working solution especially when one is working under time pressure. On the other hand I learn the most when I struggle and persevere to understand how, why it works, modify it for my needs, “ruin it with ‘Green comment graffitiOR to try to find out why not if it does not work……….
……….Please correct me if I am wrong, but your latest code also does not work..

. If a valid file name is given, then the program crashes at this point
Code:
If PathAndFileName = False Then Exit Sub

. I expect because you have dimensioned PathAndFileName as a string. (So it does not except the False????). Replacing PathAndFileName As String with PathAndFileName As Variant seems to work.

. Your final working File with minor correction shown in Red


Code:
Sub ImportFile2()
  Dim R As Long, C As Long, FileNum As Long
  Dim TotalFile As String, PathAndFileName As[COLOR=#ff0000] Variant[/COLOR], Delim As String
  Dim Lines As Variant, Data As Variant, Fields As Variant
  If Application.CountA(Cells) Then
    MsgBox "You have data on the active worksheet... this code can only be run " & _
           "when the active sheet is empty!", vbCritical, "Worksheet Not Empty"
    Exit Sub
  End If
  FileNum = FreeFile
  PathAndFileName = Application.GetOpenFilename("CSV Files(*.csv),*.csv,All Files (*.*),*.*", 1, "Open CSV File")
  Open PathAndFileName For Binary As #FileNum
    If PathAndFileName = False Then Exit Sub
    TotalFile = Space(LOF(FileNum))
    Get #FileNum, , TotalFile
  Close #FileNum
  Lines = Split(TotalFile, vbNewLine)
  ReDim Data(1 To UBound(Lines) + 1, 1 To 20)
  If InStr(TotalFile, vbTab) Then
    Delim = vbTab
  Else
    Delim = ","
  End If
  For R = 0 To UBound(Lines)
    Fields = Split(Lines(R), Delim)
    For C = 0 To UBound(Fields)
      Data(R + 1, C + 1) = Fields(C)
    Next
  Next
  Range("A1").Resize(UBound(Data), UBound(Data, 2)) = Data
End Sub




……………………………


……….. gives sensible results for the Tab delimited file from the Op, (although they may not be the exact results he wants as some columns are somewhat offset in the final Excel table).

Alan Elston

……..not sure they are really incorrect offsets... if the file contains a double tab character, I would assume whatever program was used to create the file did so on purpose.

….. I am always unsure with spaces and Tabs as delimiters . I prefer to use something I can clearly see. I have the additional problem in Germany that a comer is taken as a Decimal Point! So a semi-colon ; or | is my favorite

Alan.
 
Upvote 0
Hi apo…
…..If you have a heap of rows.. probably populating an array and writing that array to the sheet at the end will be faster...

Something like: (used on your sample comma separated data form above)........


Wow. Super Code. Works on csv ( .txt ) files, like that given from the OP.
(My Homework tonight: Figure out how it works!)
Alan.
 
Upvote 0
Hi DocAElstein,

(My Homework tonight: Figure out how it works!)

Step through the code (F8) and make sur eyou have the Locals WIndow open.. that will help you understand how, first the s array and then the y array are being populated..
 
Upvote 0
Hi Rick,

……….Please correct me if I am wrong, but your latest code also does not work..

. If a valid file name is given, then the program crashes at this point
Code:
If PathAndFileName = False Then Exit Sub

. I expect because you have dimensioned PathAndFileName as a string. (So it does not except the False????). Replacing PathAndFileName As String with PathAndFileName As Variant seems to work.
Wow, I am beginning to wonder if I got anything right on that code!:LOL: You are correct (all I can do is blame my lapses on bad testing)... thanks for catching that.
 
Upvote 0
Hi DocAElstein,



Step through the code (F8) and make sur eyou have the Locals WIndow open.. that will help you understand how, first the s array and then the y array are being populated..

..Thanks.. F8 I use frequently, .. the Locals WIndow I should certainly get into, - I have probably wasted a lot time Debuging.Printing like mad, when I think in the Locals WIndow it can be set up to show me continually as variables and arrays are filled. I must make the effort to do that BEFORE I do my Homework. Good tip / reminder on that one,
Alan
 
Upvote 0

Forum statistics

Threads
1,216,046
Messages
6,128,489
Members
449,455
Latest member
jesski

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top