Date: Tue, 14 Nov 2006 14:58:39 -0600 (CST) From: Mark Jacobson To: 810-022-01-FALL@uni.edu Subject: SliceOf("ghostbusters, 4, 3) practice test question... I goofed in writing that question before class. It was NOT meant to be exactly the same as the VBA Mid() function, but that is what was shown and described for SliceOf() ?Mid("Ghostbusters",4,3) <--------- Mid() demonstrated stb ?SliceOf("Ghostbusters",4,3) <--------- SliceOf() demonstrated stb BORING!!!! Function SliceOf(theString As String, whereToStart As Integer, _ howMany As Integer) As String SliceOf = Mid(theString, whereToStart, howMany) End Function I guess it does show you how a very simple function is written, and helps you review the parameters and return type and syntax. Here is what I intended to be describing. A string function that does something NEW and DIFFERENT. The 3rd argument does NOT describe HOW MANY characters you want, it describes instead of the HOW MANY, something else. What is that something else? What Character To Stop At? SliceOfFromWhereToWhere says 2nd argument, just like Mid, says here is where I want to start in the string data item, but the 3rd argument says here is WHERE I WANT TO STOP in the string. ?SliceOfFromWhereToWhere("GHOSTBUSTERS", 4, 6) STB Start with the 4th character and stop with the 6th character of "GHOSTBUSTERS" 123456789012 11 123456789012 of "GHOSTBUSTERS" 4 6 STB --- ------ ?SliceOfFromWhereToWhere("GHOSTBUSTERS", 6, 11) BUSTER 678901 678901 6th to Where to start and 11th is Where to stop ?SliceOfFromWhereToWhere("GHOSTBUSTERS", 6, 8) BUS 6 8 678 8 is where to STOP, NOT how many. -------- Function SliceOfFromWhereToWhere(theString As String, _ whereToStart As Integer, _ whereToStop As Integer) As String answer = "" For i = whereToStart To whereToStop answer = answer & Mid(theString, i, 1) Next i SliceOfFromWhereToWhere = answer End Function This one requires a For Next loop, at least for the obvious solution to the problem. Function SliceOfNoLoop(theString As String, whereStart As Integer, _ whereStop As Integer) As String howMany = whereStop - whereStart + 1 answer = Mid(theString, whereStart, howMany) SliceOfNoLoop = answer End Function And View menu, Immediate window demonstration that it works too. - - ?SliceOfNoLoop("123456789ABCDEF",5,11) 11 - 5 + 1 = 7 = howMany... 56789AB ------- ?SliceOfNoLoop("123456789ABCDEF",8,12) 12 - 8 + 1 = 4 + 1 = 5 89ABC -----