[color=green]'Option Explicit[/color]
[color=darkblue]Sub[/color] PrintDataToTextFile()
[color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] TheEnd [color=green]'If anything goes wrong do not crash go to TheEnd.[/color]
[color=darkblue]Dim[/color] LastFileRow [color=darkblue]As[/color] [color=darkblue]Long[/color], FileRow [color=darkblue]As[/color] [color=darkblue]Long[/color], PPriceRow [color=darkblue]As[/color] [color=darkblue]Long[/color], LastPPriceRow [color=darkblue]As[/color] [color=darkblue]Long[/color] [color=green]'File Row,Last Row,Row and last Row for Set prices[/color]
[color=darkblue]Let[/color] LastPPriceRow = 1 [color=green]' Assume we have at least 1 price to start with![/color]
[color=darkblue]Let[/color] LastFileRow = Cells.Find(What:="*", After:=Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row [color=green]' work backwards from first cell(effectively start at last cell) to find end of Price and pick out last Row[/color]
[color=darkblue]Dim[/color] Price [color=darkblue]As[/color] [color=darkblue]String[/color], TextFilesFolder [color=darkblue]As[/color] [color=darkblue]String[/color], TextFilesFolderPath [color=green]' Each Price, Text Files Folder and Path[/color]
[color=darkblue]Let[/color] TextFilesFolder = "\TextFileFolder\cgreene87TextFiles" [color=green]'Chgange these two lines to suit..[/color]
[color=darkblue]Let[/color] TextFilesFolderPath = ThisWorkbook.Path & TextFilesFolder [color=green]'..where you want your text Files to go-[/color]
ThisWorkbook.Worksheets("Markdown list Mid season").Activate [color=green]'Activate relavent Price Sheet list in Your Excel File so that you are allways referencing there with calling up Cells etc.[/color]
[color=darkblue]For[/color] FileRow = 2 [color=darkblue]To[/color] LastFileRow [color=darkblue]Step[/color] 1 [color=green]'go along whole file[/color]
[color=darkblue]If[/color] Cells(FileRow, 2).Value <> Cells(FileRow + 1, 2).Value [color=darkblue]Then[/color] [color=green]'Check to see if next price is different[/color]
[color=darkblue]Let[/color] Price = Cells(FileRow, 2).Value [color=green]'Set price for last same prices[/color]
[color=darkblue]Open[/color] TextFilesFolderPath & "\PP Output" & Price & ".txt" [color=darkblue]For[/color] Output [color=darkblue]As[/color] 1 [color=green]'Open a Data "Highway" for taking the data string. Note: If no file is there it will be created[/color]
[color=darkblue]For[/color] PPriceRow = 1 [color=darkblue]To[/color] LastPPriceRow [color=green]'For the number of similar prices[/color]
[color=darkblue]Print[/color] #1, Cells(FileRow - LastPPriceRow + PPriceRow, 1).Text [color=green]'Print out the corresponding item number[/color]
[color=darkblue]Next[/color] PPriceRow [color=green]'[/color]
[color=darkblue]Close[/color] 1 'Allways close this "highway" when finished sending data or can get wiered problems.
[color=darkblue]Let[/color] LastPPriceRow = 1 [color=green]'Reset the Row count for similar prices[/color]
[color=darkblue]Else[/color] [color=green]'As the price and next price are similar we...[/color]
[color=darkblue]Let[/color] LastPPriceRow = LastPPriceRow + 1 [color=green]'Add 1 to the count for similar prices[/color]
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]Next[/color] FileRow [color=green]'Check the next File row to see if it still has the same Price[/color]
[color=darkblue]Exit[/color] [color=darkblue]Sub[/color] [color=green]'If no errors stop program[/color]
TheEnd: [color=green]'In the case of an error[/color]
MsgBox (Err.Description) [color=green]'Give message box with error description[/color]
[color=darkblue]Close[/color] 1 [color=green]'Important: If anything goes wrong make sure the data path is closedClose 1[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color] [color=green]'PrintDataToTextFile()[/color]