[color=darkblue]Sub[/color] M_snb5AlanMod()
ActiveSheet.Cells(1).CurrentRegion.Resize(, 2).Copy [color=green]'Copy all data (and headings) to clip board[/color]
[color=darkblue]Dim[/color] objClipboard [color=darkblue]As[/color] [color=darkblue]Object[/color] [color=green]'Clipboard Object: It has a Funny long name as it is ..not necerssarily anything to do with Excel or FileScripting[/color]
[color=darkblue]Set[/color] objClipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") [color=green]'..not necerssarily anything to do with Excel or FileScripting[/color]
[color=green]'The above two lines is a "Late Binding" approach to making Clipboard stuff available. An alternative "Early Binding" would be to first set a referrence to Microsoft Forms 2.0 Object Library - Tools>>References>>scroll down and check the box next to Microsoft Forms 2.0 Object Library..and then the next two lines.....[/color]
[color=green]' Dim objClipboard As DataObject[/color]
[color=green]' Set objClipboard = New DataObject[/color]
objClipboard.GetFromClipboard [color=green]' a necerssary inbetween step. Not quite sure wot it does, maybe sort of preparing the clipboard to give stuff[/color]
[color=darkblue]Dim[/color] TextFromClipboard [color=darkblue]As[/color] [color=darkblue]String[/color]
[color=darkblue]Let[/color] TextFromClipboard = objClipboard.GetText [color=green]'This is a bit subtle: It is actually a long string , a TextString comprising values and in this case carriage returns, between them[/color]
[color=darkblue]Dim[/color] sn() [color=darkblue]As[/color] [color=darkblue]String[/color] [color=green]'Array. We do not know the size: It will be set by the Split Function[/color]
[color=darkblue]Dim[/color] FirstLine() [color=darkblue]As[/color] [color=darkblue]String[/color] [color=green]'First Line picked out each time after filtering out last individual price lists[/color]
[color=darkblue]Dim[/color] ItemsAndPrices() [color=darkblue]As[/color] [color=darkblue]String[/color] [color=green]'Array for individual Item And Price lists for similar prices[/color]
[color=darkblue]Dim[/color] OutputTextString [color=darkblue]As[/color] String [color=green]'The text string to sent out as a Text Stream[/color]
[color=darkblue]Let[/color] sn = Split(TextFromClipboard, vbCrLf) [color=green]'Takes the [color=darkblue]String[/color] from Clipboard, breaks it at the carriagereturn with linefeed which effectively gives a 1 dimensional array which I think is a long horizontal thing, that is to say a list aslong columns as it were?!?[/color]
[color=green]' Dim fsoFileSystem As Object[/color]
[color=green]' Set fsoFileSystem = CreateObject("Scripting.FileSystemObject")[/color]
[color=darkblue]Dim[/color] fsoFileSystem [color=darkblue]As[/color] Scripting.FileSystemObject [color=green]'Useful object to do data and data handeling ("Early Binding" here))[/color]
[color=darkblue]Set[/color] fsoFileSystem = [color=darkblue]New[/color] Scripting.FileSystemObject [color=green]'This one is to do with writing but note in writing you often make something like a file[/color]
[color=green]'Note: the above is done with early binding and requires library reference to MS Scripting Runtime - Tools>>References>>scroll down and check the box next to Microsoft Scripting Runtime[/color]
[color=green]' we could replace the two lines above with -[/color]
[color=green]' Dim fsoFileSystem As Object[/color]
[color=green]' Set fsoFileSystem = CreateObject("Scripting.FileSystemObject")[/color]
[color=green]' but the next line only works with early binding to MS Scripting Runtime so stay with Early binding for this one.[/color]
[color=darkblue]Dim[/color] fsoTextFile [color=darkblue]As[/color] Scripting.TextStream [color=green]'A sort of file path or Highway for data I think. This appear only to be possible with Early binding[/color]
[color=darkblue]Dim[/color] j [color=darkblue]As[/color] [color=darkblue]Long[/color] [color=green]'Number for loop count[/color]
[color=darkblue]Dim[/color] c00 [color=darkblue]As[/color] String [color=green]'An Individual price to use in filtering and in this case identifying a file name.[/color]
[color=darkblue]For[/color] j = 0 [color=darkblue]To[/color] [color=darkblue]UBound[/color](sn) [color=green]' Use UBound to start with to be on the safe side - that would be maximium if no filtering. The j will give the number of files but otherwise is not much use so a Do while sn(0) <>"" Loop is probabbly better similar to wot snb did: better programming practice.[/color]
[color=darkblue]If[/color] sn(0) = "" [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]For[/color] [color=green]'sn will reduce in size due to the filtering and evtl nothing will be left. Would not be necerssary using the Do While Loop[/color]
[color=darkblue]Let[/color] FirstLine = Split(sn(0), vbTab) [color=green]'Split First line to a 1,2 Array[/color]
c00 = FirstLine(1) [color=green]'The price given in second column (1) of Array, 8First column is (0)[/color]
[color=darkblue]Set[/color] fsoTextFile = fsoFileSystem.createtextfile("C:\Users\Elston\Desktop\cgreene87TextFiles\PP Output" & c00 & ".txt") [color=green]'Some how by seting this the text file is created,[/color]
[color=darkblue]Let[/color] ItemsAndPrices = Filter(sn, vbTab & c00) [color=green]'1 Column Array of individual items and prices with similar prices[/color]
[color=darkblue]Let[/color] OutputTextString = Join(ItemsAndPrices, vbCrLf) [color=green]' This is a bit subtle: It is actually a long string, a TextString comprising values and a carriage return between them[/color]
[color=darkblue]Let[/color] OutputTextString = Replace(OutputTextString, vbTab & c00, "") [color=green]'Remove the Tab and Price[/color]
fsoTextFile.write OutputTextString
sn = Filter(sn, vbTab & c00, [color=darkblue]False[/color]) [color=green]'We filter out effectivelly up to the next price so that the first valie in second column is the next price to consider[/color]
[color=darkblue]Next[/color] j
[color=darkblue]End[/color] [color=darkblue]Sub[/color]