[color=blue]Option[/color] [color=blue]Explicit[/color]
[color=lightgreen]' next 2 lines needed for 'Charley Williams Micro Timer Code[/color]
[color=blue]Private[/color] [color=blue]Declare[/color] [color=blue]Function[/color] getFrequency [color=blue]Lib[/color] "kernel32" Alias "QueryPerformanceFrequency" (cyFrequency [color=blue]As[/color] [color=blue]Currency[/color]) [color=blue]As[/color] [color=blue]Long[/color]
[color=blue]Private[/color] [color=blue]Declare[/color] [color=blue]Function[/color] getTickCount [color=blue]Lib[/color] "kernel32" Alias "QueryPerformanceCounter" (cyTickCount [color=blue]As[/color] [color=blue]Currency[/color]) [color=blue]As[/color] [color=blue]Long[/color]
[color=lightgreen]'[/color]
'
'
'
'
[color=blue]Sub[/color] TransferDebiNetToDumpArrays()
[color=lightgreen]'Initila Sheet Names / File Names[/color]
[color=blue]Dim[/color] wksL2 [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wksL2 = ThisWorkbook.Worksheets("Leith2") [color=lightgreen]'Main Sheet for Imported Nutrients[/color]
[color=blue]Dim[/color] FileName [color=blue]As[/color] String: [color=blue]Let[/color] FileName = ThisWorkbook.Name [color=lightgreen]'Need to do a bit of mucking about to get the window name[/color]
[color=blue]Dim[/color] WindowFileName [color=blue]As[/color] [color=blue]String[/color] [color=lightgreen]'File name as typically seen displayed without last bit after dot[/color]
[color=blue]Let[/color] WindowFileName = Left(FileName, (InStrRev(FileName, ".") - 1)) [color=lightgreen]'Take off the bit after the dot[/color]
[color=blue]Dim[/color] objWin [color=blue]As[/color] Object: [color=blue]Set[/color] objWin = Windows("11 172x49ExtRowsGruppiertDumpAbDec2014ISSC") [color=lightgreen]'Mainly for Conveneince[/color]
[color=lightgreen]'Bring both sheet data into various Arrays. Set ## to last column wanted in Master to Fill in[/color]
objWin.Activate [color=lightgreen]'ActiveCell below will not work without this[/color]
[color=blue]Dim[/color] ARow [color=blue]As[/color] [color=blue]Long[/color], Acolumn [color=blue]As[/color] [color=blue]Long[/color], lc [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Current Active Cell[/color]
[color=blue]Let[/color] ARow = ActiveCell.Row: [color=blue]Let[/color] Acolumn = ActiveCell.Column [color=lightgreen]'Get cell coordinates for selected range. (For a range, the top left cell coordinates are returned)[/color]
ActiveSheet.Range("a" & ARow & ":CU" & ARow & "").Select [color=lightgreen]'##CU (Vitamine) Extended selection range to include everything wanted.[/color]
wksL2.Activate [color=lightgreen]'Mainly to see what is going on, and the next line still works!?[/color]
[color=lightgreen]'The Selected Food (and also the Heading (NutritionArray) by .offset.resizemethod[/color]
[color=blue]Dim[/color] RngFood [color=blue]As[/color] Range: [color=blue]Set[/color] RngFood = objWin.Selection
[color=blue]Dim[/color] ArrOrigEntries() [color=blue]As[/color] Variant: [color=blue]Let[/color] ArrOrigEntries() = RngFood.Value2 [color=lightgreen]'Value2 is the underlying value, quickes and most accurate to get to http://www.excelforum.com/excel-new-users-basics/1058406-range-dimensioning-range-and-value-referencing-and-referring-to-arrays.html[/color]
[color=blue]Dim[/color] RngNut [color=blue]As[/color] Range: [color=blue]Set[/color] RngNut = RngFood.Offset(-ARow + 1, 0).Resize(20)
[color=blue]Dim[/color] ArrFood() [color=blue]As[/color] [color=blue]Variant[/color], ArrNut() [color=blue]As[/color] [color=blue]Variant[/color] [color=lightgreen]'CVhoose variant both to allow for "" and also to allow one liner throgh Range Object to work.[/color]
[color=blue]Let[/color] ArrFood() = RngFood.Value2: [color=blue]Let[/color] ArrNut() = RngNut.Value2 [color=lightgreen]'VBA Capture possibility to bring in values in one liner[/color]
[color=blue]Let[/color] lc = [color=blue]UBound[/color](ArrFood, 2) [color=lightgreen]': Let lc = 99'=CU[/color]
[color=lightgreen]'The Selected Food from Kcal[/color]
objWin.Activate [color=lightgreen]'ActiveCell below will not work without this[/color]
ActiveSheet.Range("H" & ARow & ":CU" & ARow & "").Select
[color=blue]Dim[/color] RngFoodAbKcal [color=blue]As[/color] Range: [color=blue]Set[/color] RngFoodAbKcal = objWin.Selection
[color=blue]Dim[/color] ArrOrigAbKcaEntries() [color=blue]As[/color] Variant: [color=blue]Let[/color] ArrOrigAbKcaEntries() = RngFoodAbKcal.Value2 [color=lightgreen]'Variant is not only necerssary for seeing Range Object, but also if it was for example a number than it will have 0s in place of empty cells[/color]
[color=lightgreen]'The DebiNet imported range. May be advisable to set this Manually sometimes[/color]
[color=blue]Dim[/color] lDbNr [color=blue]As[/color] Long: [color=blue]Let[/color] lDbNr = wksL2.Cells.Find(What:="*", After:=wksL2.Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row [color=lightgreen]'Get last Row with entry anywhere for Sheet1. Method: You start at first cell then go backwards (which effectively starts at end of sheet), sercching for anything ( = * ) by rows, then get the row number. This allows for different excel versions with different available Row numbers) Just a different method that finds last row in sheet rather than row for last entry in particular cell. Better to use that here as we are not sure which columns are full[/color]
[color=blue]Dim[/color] rngDebiNet [color=blue]As[/color] Range: [color=blue]Set[/color] rngDebiNet = wksL2.Range("A21:D" & lDbNr & "")
[color=blue]Dim[/color] ArrDebiNet() [color=blue]As[/color] Variant: [color=blue]Let[/color] ArrDebiNet() = rngDebiNet.Value2 [color=lightgreen]'VBA Capture possibility to bring in values in one liner[/color]
[color=lightgreen]'Now for Producing output Array, looping through looking for match conditions[/color]
[color=blue]Dim[/color] Results() [color=blue]As[/color] Variant: [color=blue]ReDim[/color] Results(1 [color=blue]To[/color] 1, 1 [color=blue]To[/color] lc) [color=lightgreen]'Output results.. must use ReDim as [color=blue]Dim[/color] only takes actual numbers. variant must be used for text as well as Number entries, but also so that empty cells will remain as empty rather than beinng replaced by 0s[/color]
[color=blue]Dim[/color] r [color=blue]As[/color] [color=blue]Long[/color], c [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Row and column in Debinet imported data[/color]
[color=blue]Dim[/color] DumpColumn [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Column of Master Dump[/color]
[color=blue]Dim[/color] Headr [color=blue]As[/color] Long [color=lightgreen]'Heading Row, (1 to 20)[/color]
[color=blue]For[/color] r = 21 - 20 [color=blue]To[/color] lDbNr - 20 [color=lightgreen]'Each debinet imported row. -20 must bbe used as starts at 1 due to the capture method[/color]
[color=blue]If[/color] ArrDebiNet(r, 1) <> "" [color=blue]Then[/color] [color=lightgreen]'Efficiently only goint further if there is a Nutrition from Food Product[/color]
[color=blue]For[/color] DumpColumn = 8 [color=blue]To[/color] lc [color=blue]Step[/color] 2 [color=lightgreen]'Go along each Master DumpColumn[/color]
[color=blue]For[/color] Headr = 1 [color=blue]To[/color] 20 [color=lightgreen]'Going throght the Headings...[/color]
[color=blue]If[/color] ArrDebiNet(r, 1) = ArrNut(Headr, DumpColumn) [color=blue]Then[/color] [color=lightgreen]'Have heading match condition[/color]
[color=lightgreen]'MsgBox ArrDebiNet(r, 1) & " " & ArrNut(Headr, DumpColumn) & " " & ArrDebiNet(r, 4)'Debug hilfe[/color]
[color=blue]Let[/color] Results(1, DumpColumn) = ArrDebiNet(r, 4) [color=lightgreen]'Important match condition so put in Forth column In appropriate colum indiciee for output[/color]
[color=blue]Else[/color] [color=lightgreen]'No heading match[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]Next[/color] Headr [color=lightgreen]'Try next heading for match[/color]
[color=blue]Next[/color] DumpColumn [color=lightgreen]'back to next Master DumpColumn[/color]
[color=blue]Else[/color] [color=lightgreen]'Not doing any sorting for no Nurition from Food Product[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]Next[/color] r [color=lightgreen]'Go to next imported debinet Row[/color]
[color=lightgreen]'Produce the output, making check and possibility to put original values in. 3 Alternatives[/color]
Dim AvResults() [color=blue]As[/color] Variant: [color=blue]ReDim[/color] AvResults(1 [color=blue]To[/color] 1, 8 To lc) [color=lightgreen]'First alternative looping for average results. Avv Results must be variant or empty cells will be replaced by 0s![/color]
[color=blue]For[/color] DumpColumn = 8 To lc [color=lightgreen]'Go from Kcal[/color]
[color=blue]If[/color] ArrOrigEntries(1, DumpColumn) <> "" And Results(1, DumpColumn) <> "" [color=blue]Then[/color]
AvResults(1, DumpColumn) = Application.WorksheetFunction.Round((ArrOrigEntries(1, DumpColumn) + Results(1, DumpColumn)) / 2, 7)
[color=blue]Else[/color] [color=lightgreen]'Case empty cell[/color]
AvResults(1, DumpColumn) = Results(1, DumpColumn) [color=lightgreen]'Stay again in Variants to allow transfering "" for "" in Results[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]Next[/color] DumpColumn
[color=blue]Let[/color] RngFoodAbKcal.Value2 = AvResults()
[color=lightgreen]' 'Let RngFood.Value2 = Results() 'second Alternative one Liner to bypass above loop if all is empty and nothing in A-G[/color]
[color=lightgreen]'[/color]
' 'Msg Box check if all is well, if not put original enties back in. COMMENTED OUT FOR SPEED TESTS
[color=lightgreen]' Dim Response As Integer 'In VBA Butons "yes is 6, 7 is "no"[/color]
[color=lightgreen]' Response = MsgBox(prompt:="Is all OK?", Buttons:=vbYesNo, Title:="File Check") ' Displays a message box with the yes and no options.[/color]
[color=lightgreen]' If Response = vbYes Then 'Do nothing, that is to say just carry on after End of If[/color]
[color=lightgreen]' Else[/color]
' Let RngFood.Value2 = ArrOrigEntries() 'Full repair!!':Let RngFoodAbKcal.Value2 = ArrOrigAbKcaEntries()'Repair only from Kcal
[color=lightgreen]' End If[/color]
objWin.ActiveCell.Offset(0, -7).Select [color=lightgreen]'Just to be on the safe side in case you riun again (For example to get weighting avarage up), select back the first call in selection row[/color]
[color=blue]End[/color] [color=blue]Sub[/color] [color=lightgreen]'TransferDebiNetToDumpArrays[/color]
[color=lightgreen]'..................................................[/color]
[color=lightgreen]'[/color]
'
'
'
[color=blue]Sub[/color] TransferDebiNetToDumpMostSpreadsheet()
[color=lightgreen]'Initial Sheet Names / File Names[/color]
[color=blue]Dim[/color] wksL2 [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wksL2 = ThisWorkbook.Worksheets("Leith2") [color=lightgreen]'Main Sheet for Imported Nutrients[/color]
[color=blue]Dim[/color] FileName [color=blue]As[/color] String: [color=blue]Let[/color] FileName = ThisWorkbook.Name [color=lightgreen]'Need to do a bit of mucking about to get the window name[/color]
[color=blue]Dim[/color] WindowFileName [color=blue]As[/color] [color=blue]String[/color] [color=lightgreen]'File name as typically seen displayed without last bit after dot[/color]
[color=blue]Let[/color] WindowFileName = Left(FileName, (InStrRev(FileName, ".") - 1)) [color=lightgreen]'Take off the bit after the dot[/color]
[color=blue]Dim[/color] objWin [color=blue]As[/color] Object: [color=blue]Set[/color] objWin = Windows("11 172x49ExtRowsGruppiertDumpAbDec2014ISSC") [color=lightgreen]'Mainly for Conveneince[/color]
[color=lightgreen]'Make some back up initial data. - The Selected Food complete original Range and also The Selected original Food from KcalSet ## to last column wanted in Master to Fill in. (CU Vitamine) range to include everything wanted.[/color]
objWin.Activate [color=lightgreen]'ActiveCell below will not work without this[/color]
[color=blue]Dim[/color] ARow [color=blue]As[/color] [color=blue]Long[/color], Acolumn [color=blue]As[/color] [color=blue]Long[/color], lc [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Current Active Cell[/color]
[color=blue]Let[/color] ARow = ActiveCell.Row: [color=blue]Let[/color] Acolumn = ActiveCell.Column [color=lightgreen]'Get cell coordinates for selected range. (For a range, the top left cell coordinates are returned)[/color]
ActiveSheet.Range("A" & ARow & ":CU" & ARow & "").Select [color=lightgreen]'##CU (Vitamine) Extended selection range to include everything wanted.[/color]
wksL2.Activate [color=lightgreen]'Mainly to see what is going on, and the next line still works!?[/color]
[color=lightgreen]' The Selected Food[/color]
[color=blue]Dim[/color] RngFood [color=blue]As[/color] Range: [color=blue]Set[/color] RngFood = objWin.Selection
[color=blue]Dim[/color] ArrOrigEntries() [color=blue]As[/color] Variant: [color=blue]Let[/color] ArrOrigEntries() = RngFood.Value2 [color=lightgreen]'Value2 is the underlying value, quickes and most accurate to get to http://www.excelforum.com/excel-new-users-basics/1058406-range-dimensioning-range-and-value-referencing-and-referring-to-arrays.html[/color]
[color=blue]Dim[/color] ArrFood() [color=blue]As[/color] [color=blue]Variant[/color], ArrNut() [color=blue]As[/color] [color=blue]Variant[/color] [color=lightgreen]'CVhoose variant both to allow for "" and also to allow one liner throgh Range Object to work.[/color]
[color=blue]Let[/color] ArrFood() = RngFood.Value2 [color=lightgreen]'VBA Capture possibility to bring in values in one liner[/color]
[color=blue]Let[/color] lc = [color=blue]UBound[/color](ArrFood, 2) [color=lightgreen]': Let lc = 99'=CU[/color]
[color=lightgreen]' The Selected Food from Kcal[/color]
objWin.Activate [color=lightgreen]'ActiveCell below will not work without this[/color]
ActiveSheet.Range("H" & ARow & ":CU" & ARow & "").Select
[color=blue]Dim[/color] RngFoodAbKcal [color=blue]As[/color] Range: [color=blue]Set[/color] RngFoodAbKcal = objWin.Selection
[color=blue]Dim[/color] ArrOrigAbKcaEntries() [color=blue]As[/color] Variant: [color=blue]Let[/color] ArrOrigAbKcaEntries() = RngFoodAbKcal.Value2 [color=lightgreen]'Variant is not only necerssary for seeing Range Object, but also if it was for example a number than it will have 0s in place of empty cells[/color]
[color=lightgreen]'Now main Loopin through spreadsheet, looking for match conditions, then writing in when match found[/color]
[color=lightgreen]'The DebiNet imported range. May be advisable to set this Manually sometimes[/color]
[color=blue]Dim[/color] lDbNr [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'The DebiNet imported range. May be advisable to set this Manually sometimes[/color]
[color=blue]Let[/color] lDbNr = wksL2.Cells.Find(What:="*", After:=wksL2.Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row [color=lightgreen]'Get last Row with entry anywhere for Sheet1. Method: You start at first cell then go backwards (which effectively starts at end of sheet), sercching for anything ( = * ) by rows, then get the row number. This allows for different excel versions with different available Row numbers) Just a different method that finds last row in sheet rather than row for last entry in particular cell. Better to use that here as we are not sure which columns are full[/color]
[color=blue]Dim[/color] wksDump [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wksDump = Workbooks("11 172x49ExtRowsGruppiertDumpAbDec2014ISSC.xlsx").Worksheets("Sheet1") [color=lightgreen]'Extra step over Array mehtod as objWin has no cells property, only an ActiveCell Property[/color]
[color=blue]Dim[/color] r [color=blue]As[/color] [color=blue]Long[/color], c [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Row and column in Debinet imported data[/color]
[color=blue]Dim[/color] DumpColumn [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Column of Master Dump[/color]
[color=blue]Dim[/color] Headr [color=blue]As[/color] Long [color=lightgreen]'Heading Row, (1 to 20)[/color]
[color=blue]For[/color] r = 21 [color=blue]To[/color] lDbNr [color=lightgreen]'Each debinet imported row. -20 must bbe used as starts at 1 due to the capture method[/color]
[color=blue]If[/color] wksL2.Cells(r, 1).Value <> "" [color=blue]Then[/color] [color=lightgreen]'Efficiently only goint further if there is a Nutrition from Food Product[/color]
[color=blue]For[/color] DumpColumn = 8 [color=blue]To[/color] lc [color=blue]Step[/color] 2 [color=lightgreen]'Go along each Master DumpColumn[/color]
[color=blue]For[/color] Headr = 1 [color=blue]To[/color] 20 [color=lightgreen]'Going throght the Headings...[/color]
[color=blue]If[/color] wksL2.Cells(r, 1).Value = wksDump.Cells(Headr, DumpColumn).Value [color=blue]Then[/color] [color=lightgreen]'Have heading match condition[/color]
[color=blue]If[/color] wksDump.Cells(ARow, DumpColumn).Value = "" [color=blue]Then[/color] [color=lightgreen]'Case empty cell, no entry yet[/color]
[color=blue]Let[/color] wksDump.Cells(ARow, DumpColumn).Value = wksL2.Cells(r, 4).Value [color=lightgreen]'Important match condition so put in Forth column In appropriate colum indiciee for output[/color]
[color=blue]Else[/color] [color=lightgreen]'Presumably an entry is already there, and we already know we have a debinet entry, so as a first approximation put the average in[/color]
[color=blue]Let[/color] wksDump.Cells(ARow, DumpColumn).Value = Application.WorksheetFunction.Round((wksDump.Cells(ARow, DumpColumn).Value + wksL2.Cells(r, 4).Value) / 2, 7)
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]Else[/color] [color=lightgreen]'No heading match[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]Next[/color] Headr [color=lightgreen]'Try next heading for match[/color]
[color=blue]Next[/color] DumpColumn [color=lightgreen]'back to next Master DumpColumn[/color]
[color=blue]Else[/color] [color=lightgreen]'Not doing any sorting for no Nurition from Food Product[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]Next[/color] r [color=lightgreen]'Go to next imported debinet Row[/color]
[color=lightgreen]' 'Msg Box check if all is well, if not put original enties back in. COMMENTED OUT FOR SPEED TESTS[/color]
[color=lightgreen]' Dim Response As Integer 'In VBA Butons "yes is 6, 7 is "no"[/color]
[color=lightgreen]' Response = MsgBox(prompt:="Is all OK?", Buttons:=vbYesNo, Title:="File Check") ' Displays a message box with the yes and no options.[/color]
[color=lightgreen]' If Response = vbYes Then 'Do nothing, that is to say just carry on after End of If[/color]
[color=lightgreen]' Else[/color]
' Let RngFood.Value2 = ArrOrigEntries() 'Full repair!!':Let RngFoodAbKcal.Value2 = ArrOrigAbKcaEntries()'Repair only from Kcal
[color=lightgreen]' End If[/color]
wksDump.Cells(ARow, 1).Activate [color=lightgreen]'Just to be on the safe side in case run again without checking active cell[/color]
[color=blue]End[/color] [color=blue]Sub[/color] [color=lightgreen]'TransferDebiNetToDumpMostSpreadSheet[/color]
[color=lightgreen]'[/color]
'
'
[color=blue]Sub[/color] TransferDebiNetToDumpMostSpreadsheetMatchOnErrPair()
[color=lightgreen]'Initial Sheet Names / File Names[/color]
[color=blue]Dim[/color] wksL2 [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wksL2 = ThisWorkbook.Worksheets("Leith2") [color=lightgreen]'Main Sheet for Imported Nutrients[/color]
[color=blue]Dim[/color] FileName [color=blue]As[/color] String: [color=blue]Let[/color] FileName = ThisWorkbook.Name [color=lightgreen]'Need to do a bit of mucking about to get the window name[/color]
[color=blue]Dim[/color] WindowFileName [color=blue]As[/color] [color=blue]String[/color] [color=lightgreen]'File name as typically seen displayed without last bit after dot[/color]
[color=blue]Let[/color] WindowFileName = Left(FileName, (InStrRev(FileName, ".") - 1)) [color=lightgreen]'Take off the bit after the dot[/color]
[color=blue]Dim[/color] objWin [color=blue]As[/color] Object: [color=blue]Set[/color] objWin = Windows("11 172x49ExtRowsGruppiertDumpAbDec2014ISSC") [color=lightgreen]'Mainly for Conveneince[/color]
[color=lightgreen]'Make some back up initial data. - The Selected Food complete original Range and also The Selected original Food from KcalSet ## to last column wanted in Master to Fill in. (CU Vitamine) range to include everything wanted.[/color]
objWin.Activate [color=lightgreen]'ActiveCell below will not work without this[/color]
[color=blue]Dim[/color] ARow [color=blue]As[/color] [color=blue]Long[/color], Acolumn [color=blue]As[/color] [color=blue]Long[/color], lc [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Current Active Cell[/color]
[color=blue]Let[/color] ARow = ActiveCell.Row: [color=blue]Let[/color] Acolumn = ActiveCell.Column [color=lightgreen]'Get cell coordinates for selected range. (For a range, the top left cell coordinates are returned)[/color]
ActiveSheet.Range("A" & ARow & ":CU" & ARow & "").Select [color=lightgreen]'##CU (Vitamine) Extended selection range to include everything wanted.[/color]
wksL2.Activate [color=lightgreen]'Mainly to see what is going on, and the next line still works!?[/color]
[color=lightgreen]' The Selected Food[/color]
[color=blue]Dim[/color] RngFood [color=blue]As[/color] Range: [color=blue]Set[/color] RngFood = objWin.Selection
[color=blue]Dim[/color] ArrOrigEntries() [color=blue]As[/color] Variant: [color=blue]Let[/color] ArrOrigEntries() = RngFood.Value2 [color=lightgreen]'Value2 is the underlying value, quickes and most accurate to get to http://www.excelforum.com/excel-new-users-basics/1058406-range-dimensioning-range-and-value-referencing-and-referring-to-arrays.html[/color]
[color=blue]Dim[/color] ArrFood() [color=blue]As[/color] [color=blue]Variant[/color], ArrNut() [color=blue]As[/color] [color=blue]Variant[/color] [color=lightgreen]'CVhoose variant both to allow for "" and also to allow one liner throgh Range Object to work.[/color]
[color=blue]Let[/color] ArrFood() = RngFood.Value2 [color=lightgreen]'VBA Capture possibility to bring in values in one liner[/color]
[color=blue]Let[/color] lc = [color=blue]UBound[/color](ArrFood, 2) [color=lightgreen]': Let lc = 99'=CU[/color]
[color=lightgreen]' The Selected Food from Kcal[/color]
objWin.Activate [color=lightgreen]'ActiveCell below will not work without this[/color]
ActiveSheet.Range("H" & ARow & ":CU" & ARow & "").Select
[color=blue]Dim[/color] RngFoodAbKcal [color=blue]As[/color] Range: [color=blue]Set[/color] RngFoodAbKcal = objWin.Selection
[color=blue]Dim[/color] ArrOrigAbKcaEntries() [color=blue]As[/color] Variant: [color=blue]Let[/color] ArrOrigAbKcaEntries() = RngFoodAbKcal.Value2 [color=lightgreen]'Variant is not only necerssary for seeing Range Object, but also if it was for example a number than it will have 0s in place of empty cells[/color]
[color=lightgreen]'Now main Loopin through spreadsheet, looking for match conditions, then writing in when match found[/color]
[color=lightgreen]'The DebiNet imported range. May be advisable to set this Manually sometimes[/color]
[color=blue]Dim[/color] lDbNr [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'The DebiNet imported range. May be advisable to set this Manually sometimes[/color]
[color=blue]Let[/color] lDbNr = wksL2.Cells.Find(What:="*", After:=wksL2.Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row [color=lightgreen]'Get last Row with entry anywhere for Sheet1. Method: You start at first cell then go backwards (which effectively starts at end of sheet), sercching for anything ( = * ) by rows, then get the row number. This allows for different excel versions with different available Row numbers) Just a different method that finds last row in sheet rather than row for last entry in particular cell. Better to use that here as we are not sure which columns are full[/color]
[color=blue]Dim[/color] wksDump [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wksDump = Workbooks("11 172x49ExtRowsGruppiertDumpAbDec2014ISSC.xlsx").Worksheets("Sheet1") [color=lightgreen]'Extra step over Array mehtod as objWin has no cells property, only an ActiveCell Property[/color]
[color=blue]Dim[/color] r [color=blue]As[/color] [color=blue]Long[/color], c [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Row and column in Debinet imported data[/color]
[color=blue]Dim[/color] DumpColumn [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Column of Master Dump[/color]
[color=blue]Dim[/color] Headr [color=blue]As[/color] Long [color=lightgreen]'Heading Row, (1 to 20)[/color]
[color=blue]For[/color] r = 21 [color=blue]To[/color] lDbNr [color=lightgreen]'Each debinet imported row. -20 must bbe used as starts at 1 due to the capture method[/color]
[color=blue]If[/color] wksL2.Cells(r, 1).Value <> "" [color=blue]Then[/color] [color=lightgreen]'Efficiently only goint further if there is a Nutrition from Food Product[/color]
[color=blue]For[/color] DumpColumn = 8 [color=blue]To[/color] lc [color=blue]Step[/color] 2 [color=lightgreen]'Go along each Master DumpColumn[/color]
[color=lightgreen]'For Headr = 1 To 20 'Going throght the Headings...[/color]
[color=lightgreen]' If wksL2.Cells(r, 1).Value = wksDump.Cells(Headr, DumpColumn).Value Then 'Have heading match condition[/color]
[color=blue]On[/color] [color=blue]Error[/color] [color=blue]Resume[/color] [color=blue]Next[/color] [color=lightgreen]'If the next line errors we carry on[/color]
[color=blue]If[/color] Application.WorksheetFunction.Match(wksL2.Cells(r, 1).Value, wksDump.Columns(DumpColumn), 0) = -1234 [color=blue]Then[/color] [color=lightgreen]' This will error for any match indicie other than 1 , 2 , 3..... etc..[/color]
[color=lightgreen]'here we "carry on" at the "next" -- but there is nothing here so we go to end If[/color]
[color=blue]Else[/color] [color=lightgreen]'We come here if match "worked" but did not return the number -1234[/color]
[color=blue]If[/color] wksDump.Cells(ARow, DumpColumn).Value = "" [color=blue]Then[/color] [color=lightgreen]'Case empty cell, no entry yet[/color]
[color=blue]Let[/color] wksDump.Cells(ARow, DumpColumn).Value = wksL2.Cells(r, 4).Value [color=lightgreen]'Important match condition so put in Forth column In appropriate colum indiciee for output[/color]
[color=blue]Else[/color] [color=lightgreen]'Presumably an entry is already there, and we already know we have a debinet entry, so as a first approximation put the average in[/color]
[color=blue]Let[/color] wksDump.Cells(ARow, DumpColumn).Value = Application.WorksheetFunction.Round((wksDump.Cells(ARow, DumpColumn).Value + wksL2.Cells(r, 4).Value) / 2, 7)
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=lightgreen]'Next Headr 'Try next heading for match[/color]
[color=blue]Next[/color] DumpColumn [color=lightgreen]'back to next Master DumpColumn[/color]
[color=blue]Else[/color] [color=lightgreen]'Not doing any sorting for no Nurition from Food Product[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]Next[/color] r [color=lightgreen]'Go to next imported debinet Row[/color]
[color=lightgreen]' 'Msg Box check if all is well, if not put original enties back in. COMMENTED OUT FOR SPEED TESTS[/color]
[color=lightgreen]' Dim Response As Integer 'In VBA Butons "yes is 6, 7 is "no"[/color]
[color=lightgreen]' Response = MsgBox(prompt:="Is all OK?", Buttons:=vbYesNo, Title:="File Check") ' Displays a message box with the yes and no options.[/color]
[color=lightgreen]' If Response = vbYes Then 'Do nothing, that is to say just carry on after End of If[/color]
[color=lightgreen]' Else[/color]
' Let RngFood.Value2 = ArrOrigEntries() 'Full repair!!':Let RngFoodAbKcal.Value2 = ArrOrigAbKcaEntries()'Repair only from Kcal
[color=lightgreen]' End If[/color]
wksDump.Cells(ARow, 1).Activate [color=lightgreen]'Just to be on the safe side in case run again without checking active cell[/color]
[color=blue]End[/color] [color=blue]Sub[/color] [color=lightgreen]'TransferDebiNetToDumpMostSpreadSheetMatchOnErrPair[/color]
[color=lightgreen]'[/color]
'
'
'
[color=blue]Sub[/color] TransferDebiNetToDumpMostSpreadsheetMatchOnErrPairShortenedHeaderColumn()
[color=lightgreen]'Initial Sheet Names / File Names[/color]
[color=blue]Dim[/color] wksL2 [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wksL2 = ThisWorkbook.Worksheets("Leith2") [color=lightgreen]'Main Sheet for Imported Nutrients[/color]
[color=blue]Dim[/color] FileName [color=blue]As[/color] String: [color=blue]Let[/color] FileName = ThisWorkbook.Name [color=lightgreen]'Need to do a bit of mucking about to get the window name[/color]
[color=blue]Dim[/color] WindowFileName [color=blue]As[/color] [color=blue]String[/color] [color=lightgreen]'File name as typically seen displayed without last bit after dot[/color]
[color=blue]Let[/color] WindowFileName = Left(FileName, (InStrRev(FileName, ".") - 1)) [color=lightgreen]'Take off the bit after the dot[/color]
[color=blue]Dim[/color] objWin [color=blue]As[/color] Object: [color=blue]Set[/color] objWin = Windows("11 172x49ExtRowsGruppiertDumpAbDec2014ISSC") [color=lightgreen]'Mainly for Conveneince[/color]
[color=lightgreen]'Make some back up initial data. - The Selected Food complete original Range and also The Selected original Food from KcalSet ## to last column wanted in Master to Fill in. (CU Vitamine) range to include everything wanted.[/color]
objWin.Activate [color=lightgreen]'ActiveCell below will not work without this[/color]
[color=blue]Dim[/color] ARow [color=blue]As[/color] [color=blue]Long[/color], Acolumn [color=blue]As[/color] [color=blue]Long[/color], lc [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Current Active Cell[/color]
[color=blue]Let[/color] ARow = ActiveCell.Row: [color=blue]Let[/color] Acolumn = ActiveCell.Column [color=lightgreen]'Get cell coordinates for selected range. (For a range, the top left cell coordinates are returned)[/color]
ActiveSheet.Range("A" & ARow & ":CU" & ARow & "").Select [color=lightgreen]'##CU (Vitamine) Extended selection range to include everything wanted.[/color]
wksL2.Activate [color=lightgreen]'Mainly to see what is going on, and the next line still works!?[/color]
[color=lightgreen]' The Selected Food[/color]
[color=blue]Dim[/color] RngFood [color=blue]As[/color] Range: [color=blue]Set[/color] RngFood = objWin.Selection
[color=blue]Dim[/color] ArrOrigEntries() [color=blue]As[/color] Variant: [color=blue]Let[/color] ArrOrigEntries() = RngFood.Value2 [color=lightgreen]'Value2 is the underlying value, quickes and most accurate to get to http://www.excelforum.com/excel-new-users-basics/1058406-range-dimensioning-range-and-value-referencing-and-referring-to-arrays.html[/color]
[color=blue]Dim[/color] ArrFood() [color=blue]As[/color] [color=blue]Variant[/color], ArrNut() [color=blue]As[/color] [color=blue]Variant[/color] [color=lightgreen]'CVhoose variant both to allow for "" and also to allow one liner throgh Range Object to work.[/color]
[color=blue]Let[/color] ArrFood() = RngFood.Value2 [color=lightgreen]'VBA Capture possibility to bring in values in one liner[/color]
[color=blue]Let[/color] lc = [color=blue]UBound[/color](ArrFood, 2) [color=lightgreen]': Let lc = 99'=CU[/color]
[color=lightgreen]' The Selected Food from Kcal[/color]
objWin.Activate [color=lightgreen]'ActiveCell below will not work without this[/color]
ActiveSheet.Range("H" & ARow & ":CU" & ARow & "").Select
[color=blue]Dim[/color] RngFoodAbKcal [color=blue]As[/color] Range: [color=blue]Set[/color] RngFoodAbKcal = objWin.Selection
[color=blue]Dim[/color] ArrOrigAbKcaEntries() [color=blue]As[/color] Variant: [color=blue]Let[/color] ArrOrigAbKcaEntries() = RngFoodAbKcal.Value2 [color=lightgreen]'Variant is not only necerssary for seeing Range Object, but also if it was for example a number than it will have 0s in place of empty cells[/color]
[color=lightgreen]'Now main Loopin through spreadsheet, looking for match conditions, then writing in when match found[/color]
[color=lightgreen]'The DebiNet imported range. May be advisable to set this Manually sometimes[/color]
[color=blue]Dim[/color] lDbNr [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'The DebiNet imported range. May be advisable to set this Manually sometimes[/color]
[color=blue]Let[/color] lDbNr = wksL2.Cells.Find(What:="*", After:=wksL2.Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row [color=lightgreen]'Get last Row with entry anywhere for Sheet1. Method: You start at first cell then go backwards (which effectively starts at end of sheet), sercching for anything ( = * ) by rows, then get the row number. This allows for different excel versions with different available Row numbers) Just a different method that finds last row in sheet rather than row for last entry in particular cell. Better to use that here as we are not sure which columns are full[/color]
[color=blue]Dim[/color] wksDump [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wksDump = Workbooks("11 172x49ExtRowsGruppiertDumpAbDec2014ISSC.xlsx").Worksheets("Sheet1") [color=lightgreen]'Extra step over Array mehtod as objWin has no cells property, only an ActiveCell Property[/color]
[color=blue]Dim[/color] r [color=blue]As[/color] [color=blue]Long[/color], c [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Row and column in Debinet imported data[/color]
[color=blue]Dim[/color] DumpColumn [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Column of Master Dump[/color]
[color=blue]Dim[/color] Headr [color=blue]As[/color] Long [color=lightgreen]'Heading Row, (1 to 20)[/color]
[color=blue]For[/color] r = 21 [color=blue]To[/color] lDbNr [color=lightgreen]'Each debinet imported row. -20 must bbe used as starts at 1 due to the capture method[/color]
[color=blue]If[/color] wksL2.Cells(r, 1).Value <> "" [color=blue]Then[/color] [color=lightgreen]'Efficiently only goint further if there is a Nutrition from Food Product[/color]
[color=blue]For[/color] DumpColumn = 8 [color=blue]To[/color] lc [color=blue]Step[/color] 2 [color=lightgreen]'Go along each Master DumpColumn[/color]
[color=lightgreen]'For Headr = 1 To 20 'Going throght the Headings...[/color]
[color=lightgreen]' If wksL2.Cells(r, 1).Value = wksDump.Cells(Headr, DumpColumn).Value Then 'Have heading match condition[/color]
[color=blue]On[/color] [color=blue]Error[/color] [color=blue]Resume[/color] [color=blue]Next[/color] [color=lightgreen]'If the next line errors we carry on[/color]
[color=blue]If[/color] Application.WorksheetFunction.Match(wksL2.Cells(r, 1).Value, wksDump.Range((wksDump.Cells(1, DumpColumn)), (wksDump.Cells(21, DumpColumn))), 0) = -1234 [color=blue]Then[/color] [color=lightgreen]' This will error for any match indicie other than 1 , 2 , 3..... etc..[/color]
[color=lightgreen]'here we "carry on" at the "next" -- but there is nothing here so we go to end If[/color]
[color=blue]Else[/color] [color=lightgreen]'We come here if match "worked" but did not return the number -1234[/color]
[color=blue]If[/color] wksDump.Cells(ARow, DumpColumn).Value = "" [color=blue]Then[/color] [color=lightgreen]'Case empty cell, no entry yet[/color]
[color=blue]Let[/color] wksDump.Cells(ARow, DumpColumn).Value = wksL2.Cells(r, 4).Value [color=lightgreen]'Important match condition so put in Forth column In appropriate colum indiciee for output[/color]
[color=blue]Else[/color] [color=lightgreen]'Presumably an entry is already there, and we already know we have a debinet entry, so as a first approximation put the average in[/color]
[color=blue]Let[/color] wksDump.Cells(ARow, DumpColumn).Value = Application.WorksheetFunction.Round((wksDump.Cells(ARow, DumpColumn).Value + wksL2.Cells(r, 4).Value) / 2, 7)
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=lightgreen]'Next Headr 'Try next heading for match[/color]
[color=blue]Next[/color] DumpColumn [color=lightgreen]'back to next Master DumpColumn[/color]
[color=blue]Else[/color] [color=lightgreen]'Not doing any sorting for no Nurition from Food Product[/color]
[color=blue]End[/color] [color=blue]If[/color]
[color=blue]Next[/color] r [color=lightgreen]'Go to next imported debinet Row[/color]
[color=lightgreen]' 'Msg Box check if all is well, if not put original enties back in. COMMENTED OUT FOR SPEED TESTS[/color]
[color=lightgreen]' Dim Response As Integer 'In VBA Butons "yes is 6, 7 is "no"[/color]
[color=lightgreen]' Response = MsgBox(prompt:="Is all OK?", Buttons:=vbYesNo, Title:="File Check") ' Displays a message box with the yes and no options.[/color]
[color=lightgreen]' If Response = vbYes Then 'Do nothing, that is to say just carry on after End of If[/color]
[color=lightgreen]' Else[/color]
' Let RngFood.Value2 = ArrOrigEntries() 'Full repair!!':Let RngFoodAbKcal.Value2 = ArrOrigAbKcaEntries()'Repair only from Kcal
[color=lightgreen]' End If[/color]
wksDump.Cells(ARow, 1).Activate [color=lightgreen]'Just to be on the safe side in case run again without checking active cell[/color]
[color=blue]End[/color] [color=blue]Sub[/color] [color=lightgreen]'TransferDebiNetToDumpMostSpreadSheetMatchOnErrPairShortenedHeaderColumn[/color]
[color=blue]Sub[/color] Timers() [color=lightgreen]'SubRoutine to call Timer Functions and Subroutines under test and display results.[/color]
Application.ScreenUpdating = [color=blue]False[/color] [color=lightgreen]'Not necerssary but speeds things up a bit and is usually done so do it consistantly here in all tests. (Turns screen updating off. Good to edit out for Debuging Purposes.[/color]
[color=blue]On[/color] [color=blue]Error[/color] [color=blue]GoTo[/color] TheEnd [color=lightgreen]'If anything goes wrong go to the End instead of crashing. Useful to Edit out for Debuging[/color]
[color=blue]Dim[/color] StartMTTime [color=blue]As[/color] [color=blue]Long[/color], StartVBATime [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'times in seconds at start of a run (Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647) -Most smaller numbers, Byte, Integer, Single are converted in computer to long so no advantage of Dim to smaller Type here) >>> Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647) If you need some sort of validation the value should only be within the range of a Byte/Integer otherwise there's no point using anything but Long.--upon/after 32-bit, Integers (Short) need converted internally anyways, so a Long is actually faster. http://www.mrexcel.com/forum/excel-questions/803662-byte-backward-loop-3.html[/color]
[color=blue]Dim[/color] MTTime [color=blue]As[/color] [color=blue]Long[/color], VBATime [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Run times given from Timer Functions[/color]
[color=blue]Let[/color] MTTime = 0 [color=lightgreen]'Could leave this out, but good[/color]
[color=blue]Let[/color] VBATime = 0 [color=lightgreen]'Practice to put it in[/color]
[color=blue]Dim[/color] Iteration [color=blue]As[/color] [color=blue]Long[/color], MaxIteration [color=blue]As[/color] [color=blue]Long[/color] [color=lightgreen]'Variable used in avaraging a few runs[/color]
[color=blue]Let[/color] MaxIteration = 1000 [color=lightgreen]'Set here the nimber of runs that you want.[/color]
[color=blue]Call[/color] TransferDebiNetToDumpArrays [color=lightgreen]'Often good to do it initially once before time runs. Sometimes on the first run things are done extra[/color]
[color=lightgreen]'Call TransferDebiNetToDumpMostSpreadsheet[/color]
[color=lightgreen]'Call TransferDebiNetToDumpMostSpreadsheetMatchOnErrPair[/color]
[color=lightgreen]'Call TransferDebiNetToDumpMostSpreadsheetMatchOnErrPairShortenedHeaderColumn[/color]
[color=blue]For[/color] Iteration = 1 [color=blue]To[/color] MaxIteration [color=lightgreen]'Run as many times as specified.[/color]
[color=blue]Let[/color] StartMTTime = MicroTimer [color=lightgreen]'Function Code from Charley Williams[/color]
[color=blue]Let[/color] StartVBATime = VBATimer [color=lightgreen]'Typical VBA Timer() Function code[/color]
[color=blue]Call[/color] TransferDebiNetToDumpArrays
[color=lightgreen]'Call TransferDebiNetToDumpMostSpreadsheet[/color]
[color=lightgreen]'Call TransferDebiNetToDumpMostSpreadsheetMatchOnErrPair[/color]
[color=lightgreen]'Call TransferDebiNetToDumpMostSpreadsheetMatchOnErrPairShortenedHeaderColumn[/color]
[color=blue]Let[/color] MTTime = (MTTime + (MicroTimer - StartMTTime)) [color=lightgreen]'Total times so[/color]
[color=blue]Let[/color] VBATime = (VBATime + (VBATimer - StartVBATime)) [color=lightgreen]'far.[/color]
[color=blue]Next[/color] Iteration 'Go and do another run(s)
MsgBox "Micro Timer " & (MTTime) / MaxIteration & " Seconds" & vbCr & _
"VBA Timer " & (VBATime) / MaxIteration & " Seconds" [color=lightgreen]'Display avarage results.[/color]
Application.ScreenUpdating = [color=blue]True[/color] [color=lightgreen]'Turn screen "back on" or screen is "dead"[/color]
[color=blue]Exit[/color] [color=blue]Sub[/color] [color=lightgreen]'We stop code here assuming it worked (or at least did not crash!)[/color]
TheEnd: [color=lightgreen]'We come here on erroring rather than crashing. Anything that should be done before ending the macro should be done here, to make sure it will always be dine ecen if the code crashes![/color]
Application.ScreenUpdating = [color=blue]True[/color] [color=lightgreen]'Screen need to be turned back on or your screen will be "dead"!! Important to turn screen on here, incase anything goes wrong.[/color]
MsgBox (Err.Description) [color=lightgreen]'Print out error message in Message Box[/color]
[color=blue]End[/color] [color=blue]Sub[/color] [color=lightgreen]' Timers()[/color]
[color=blue]Function[/color] VBATimer()
[color=lightgreen]'Typical VBA Timer Program[/color]
VBATimer = Timer [color=lightgreen]'Timer is a VBA Function that gives current time in seconds[/color]
[color=blue]End[/color] [color=blue]Function[/color] [color=lightgreen]' VBATimer()[/color]
[color=blue]Function[/color] MicroTimer() [color=blue]As[/color] [color=blue]Single[/color] 'Charley Williams Micro Timer Code
[color=lightgreen]' http://www.mrexcel.com/forum/excel-questions/805285-copy-based-match-criteria-code-alternative-looping-2.html[/color]
[color=lightgreen]' Jerry Sullivan Speed up VBA code with VLOOKUP. http://www.mrexcel.com/forum/excel-questions/745455-speed-up-visual-basic-applications-code-vlookup.html[/color]
[color=lightgreen]' https://msdn.microsoft.com/en-us/library/ff700515(v=office.14).aspx[/color]
[color=blue]Dim[/color] cyTicks1 [color=blue]As[/color] [color=blue]Currency[/color]
[color=blue]Static[/color] cyFrequency [color=blue]As[/color] [color=blue]Currency[/color]
[color=blue]Let[/color] MicroTimer = 0
[color=blue]If[/color] cyFrequency = 0 [color=blue]Then[/color] getFrequency cyFrequency [color=lightgreen]' get ticks/sec[/color]
getTickCount cyTicks1 [color=lightgreen]' get ticks[/color]
[color=blue]If[/color] cyFrequency [color=blue]Then[/color] MicroTimer = cyTicks1 / cyFrequency ' calc seconds
[color=blue]End[/color] [color=blue]Function[/color] [color=lightgreen]'MicroTimer()[/color]