Input data into adjacent cells based on data in cell Copy loop or Visible Cells Paste

%3CLINGO-SUB%20id%3D%22lingo-sub-2080382%22%20slang%3D%22en-US%22%3EInput%20data%20into%20adjacent%20cells%20based%20on%20data%20in%20cell%20Copy%20loop%20or%20Visible%20Cells%20Paste%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2080382%22%20slang%3D%22en-US%22%3E%3CP%3ENeed%20help%20in%20a%20major%20way%20I%20have%20tried%20several%20different%20ways%20to%20accomplish%20my%20goal%20and%20everyone%20of%20them%20are%20too%20slow.%20I%20have%20a%20table%20where%20I%20use%20For%20Each%20Cell%20to%20look%20through%20each%20cell%20in%20a%20column%20and%20if%20that%20cell%20has%201%20in%20it%20then%20I%20need%20to%20input%20a%20different%20formula%20into%2012%20cells%20within%20the%20same%20row.%3C%2FP%3E%3CP%3EAt%20first%20I%20thought%20this%20would%20be%20very%20simple.%20I%20use%20VBA%20and%20a%20For%20Each%20Cell%20Loop%20through%20the%20data%20and%20where%20it%20fond%20a%20match%20it%20would%20input%20the%20formulas%20into%20the%20cells%20needed.%20For%20example%3A%20If%20Cell%20A2%3D1%20Then%20Input%20Formula1%20in%20B2%2C%20Formula2%20in%20D2%2C%20Formula3%20in%20H2%2C%20Formula4%20in%20J2.%20I%20think%20you%20get%20the%20idea.%3C%2FP%3E%3CP%3EProblem%3A%20I%20have%207%2C126%20Rows%20of%20data%20and%20currently%20this%20process%20takes%205%20seconds%20per%20row.%20Which%20if%20my%20math%20is%20correct%20that%20would%20be%203%2C563%20Seconds%20or%2059.38%20Minutes%20or%201%20Hour%3F%20That%20just%20will%20not%20work.%3C%2FP%3E%3CP%3ENext%20I%20tried%20using%20VBA%20ListObjects%20to%20filter%20out%20the%20data%20not%20equal%20to%201%20in%20Column%20A.%20Then%20do%20SpecialCells(xlCellTypeVisible)%20to%20select%20and%20paste%20the%20formulas%20in%20each%20of%20the%20columns.%20That%20took%20forever%2C%20and%20I%20even%20turned%20off%20calculations.%20Even%20with%20calculations%20turned%20off%20the%20Table%20still%20wants%20Calculate%20each%20row%20when%20I%20use%20this%20method.%20I%20also%20already%20optimized%20where%20I%20could%20turning%20off%20events%2C%20Calculations%2C%20ScreenUpdating%2C%20The%20code%20is%20set%20to%20use%20the%20least%20amount%20of%20selects%20as%20possible%20for%20instance%20one%20of%20the%20formula%20inputs%20I%20used%20ActiveCell.Offset(0%2C%2045).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table_Query%5BCPUM32%5D%2CMATCH(%5B%40UIDPS%5D%2CTable_Query%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%20where%20I%20tried%20the%20For%20Each%20Loop.%20I%20also%20just%20used%20Next%20without%20the%20reference%20next%20to%20it%20because%20I%20know%20that%20can%20slow%20it%20down%20as%20well.%20I%20need%20complete%20this%20task%20in%20less%20than%2030%20seconds%20or%20at%20least%20under%20a%20minute.%3CBR%20%2F%3EWhat%20is%20the%20fastest%20way%20to%20achieve%20the%20end%20goal%3F%3C%2FP%3E%3CP%3EWhy%20does%20my%20Table%20calculate%20each%20cell%20when%20I%20use%20SpecialCells(xlCellTypeVisible)%20with%20calculations%20turned%20off%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2080382%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2080457%22%20slang%3D%22en-US%22%3ERe%3A%20Input%20data%20into%20adjacent%20cells%20based%20on%20data%20in%20cell%20Copy%20loop%20or%20Visible%20Cells%20Paste%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2080457%22%20slang%3D%22en-US%22%3EHere%20is%20some%20of%20the%20code%20used%20for%20this%20in%20the%20For%20Each%20Loop%20method.%3CBR%20%2F%3ESub%20FormulaUpdate()%3CBR%20%2F%3EDim%20Answer%20As%20String%3CBR%20%2F%3EDim%20rng%20As%20Range%2C%20cel%20As%20Range%3CBR%20%2F%3ECall%20Setup%3CBR%20%2F%3EApplication.Calculation%20%3D%20xlCalculationManual%3CBR%20%2F%3ESheets(%22PV%22).Select%3CBR%20%2F%3E%3CBR%20%2F%3ERange(%22PV%5B%5B%23Headers%5D%2C%5BOn%20Open%20Contracts%20Report%5D%5D%22).Select%3CBR%20%2F%3EActiveCell.Offset(-1%2C%200).Copy%3CBR%20%2F%3ERange(%22PV%5BOn%20Open%20Contracts%20Report%5D%22).PasteSpecial%20xlPasteAll%3CBR%20%2F%3ESelection.Calculate%3CBR%20%2F%3ESelection.Copy%3CBR%20%2F%3ESelection.PasteSpecial%20Paste%3A%3DxlPasteValues%2C%20Operation%3A%3DxlNone%2C%20SkipBlanks%20_%3CBR%20%2F%3E%3A%3DFalse%2C%20Transpose%3A%3DFalse%3CBR%20%2F%3ESet%20rng%20%3D%20Range(%22PV%5BOn%20Open%20Contracts%20Report%5D%22)%20%E2%80%98Table%3CBR%20%2F%3EApplication.CutCopyMode%20%3D%20False%3CBR%20%2F%3E%3CBR%20%2F%3E'Add%20copy%20loop%20to%20loop%20through%20each%20cell%20in%20%22On%20Open%20Contracts%20Report%22%20and%20if%20cell%20%3D%201%20then%20paste%20formula%20into%20adjacent%20cells.%20If%20Not%2C%20then%20Next%20Cell%3CBR%20%2F%3EFor%20Each%20cel%20In%20rng.Cells%3CBR%20%2F%3Ecel.Select%3CBR%20%2F%3EIf%20cel.Value2%20%3D%201%20Then%3CBR%20%2F%3EActiveCell.Offset(0%2C%2026).Formula2%20%3D%20%22%3DXLOOKUP(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2CTable1%5BGBUNT%5D%2C%22%22%22%22)%22%3CBR%20%2F%3EActiveCell.Offset(0%2C%2027).FormulaR1C1%20%3D%20%22%3DXLOOKUP(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2CTable1%5BGBDIV%5D%2C%22%22%22%22)%22%3CBR%20%2F%3E%3CBR%20%2F%3E'Update%20Buyer%20From%20Open%20Contracts%20Report%20if%20Open%20Contracts%20%3D%201%3CBR%20%2F%3EActiveCell.Offset(0%2C%20-1).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BPLNN06%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3E%3CBR%20%2F%3E'Update%20Price%20%26amp%3B%20Description%20if%20Open%20Contracts%20%3D%201%3CBR%20%2F%3EActiveCell.Offset(0%2C%206).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BBUDC35%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3EActiveCell.Offset(0%2C%207).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BBUDC35DSC%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3E%3CBR%20%2F%3E'Update%20StockRoom%20if%20Open%20Contracts%20%3D%201%3CBR%20%2F%3EActiveCell.Offset(0%2C%209).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BSTRM32%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3E%3CBR%20%2F%3E'Update%20Major%20Code%2C%20Product%20Description%2C%20Minor%20Code%20%26amp%3B%20Descriptions%20if%20Open%20Contracts%20%3D%201%3CBR%20%2F%3EActiveCell.Offset(0%2C%2021).FormulaR1C1%20%3D%20%22%3DIFERROR(TEXT(INDEX(Table1%5BPGMJ35%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22000%22%22)%2C%22%22%22%22)%22%3CBR%20%2F%3EActiveCell.Offset(0%2C%2023).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BPGMN35%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3EActiveCell.Offset(0%2C%2024).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BPGMN35DSC%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3E%3CBR%20%2F%3E'Update%20Commodity%20Code%20%26amp%3B%20Description%20if%20Open%20Contracts%20%3D%201%3CBR%20%2F%3EActiveCell.Offset(0%2C%2029).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BPCLS35%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3EActiveCell.Offset(0%2C%2030).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BPCLS35DSC%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3E%3CBR%20%2F%3E'Update%20UOM%20and%20Conversion%20Factor%20if%20Open%20Contracts%20%3D%201%3CBR%20%2F%3EActiveCell.Offset(0%2C%2045).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BCPUM32%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3EActiveCell.Offset(0%2C%2046).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BSUNT35%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3EActiveCell.Offset(0%2C%2047).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BSPCF35%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3E%3CBR%20%2F%3E'Update%20Contract%20Base%20Price%2C%20Contract%20Currency%2C%20Contract%23%2C%20If%20Open%20Contracts%20%3D%201%3CBR%20%2F%3EActiveCell.Offset(0%2C%2053).FormulaR1C1%20%3D%20%22%3DINDEX(Table1%5BPRICE%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%22%3CBR%20%2F%3EActiveCell.Offset(0%2C%2054).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BCURRENCY%5D%2CMATCH(%5B%40UID%5D%2CTable1%5BUID%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3EActiveCell.Offset(0%2C%2055).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BCONTRACT%5D%2CMATCH(%5B%40UIDPS%5D%2CTable1%5BUIDS%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3E%3CBR%20%2F%3E'Update%20Part%20Description%20if%20Open%20Contracts%20%3D%201%3CBR%20%2F%3EDoEvents%20%E2%80%98Prevents%20Excel%20from%20going%20non-responsive%3CBR%20%2F%3EActiveCell.Offset(0%2C%20-11).FormulaR1C1%20%3D%20%22%3DIFERROR(INDEX(Table1%5BITEM_DESC%5D%2CMATCH(%5B%40UID%5D%2CTable1%5BUID%5D%2C0))%2C%22%22%22%22)%22%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EDebug.Print%20cel.Address%20%E2%80%98Just%20so%20I%20can%20time%20how%20fast%20it%20is%20going.%3CBR%20%2F%3ENext%3CBR%20%2F%3EApplication.Calculate%20%E2%80%98When%20completed%20calculates%20the%20sheet.%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E
Contributor

Need help in a major way I have tried several different ways to accomplish my goal and everyone of them are too slow. I have a table where I use For Each Cell to look through each cell in a column and if that cell has 1 in it then I need to input a different formula into 12 cells within the same row.

At first I thought this would be very simple. I use VBA and a For Each Cell Loop through the data and where it fond a match it would input the formulas into the cells needed. For example: If Cell A2=1 Then Input Formula1 in B2, Formula2 in D2, Formula3 in H2, Formula4 in J2. I think you get the idea.

Problem: I have 7,126 Rows of data and currently this process takes 5 seconds per row. Which if my math is correct that would be 3,563 Seconds or 59.38 Minutes or 1 Hour? That just will not work.

Next I tried using VBA ListObjects to filter out the data not equal to 1 in Column A. Then do SpecialCells(xlCellTypeVisible) to select and paste the formulas in each of the columns. That took forever, and I even turned off calculations. Even with calculations turned off the Table still wants Calculate each row when I use this method. I also already optimized where I could turning off events, Calculations, ScreenUpdating, The code is set to use the least amount of selects as possible for instance one of the formula inputs I used ActiveCell.Offset(0, 45).FormulaR1C1 = "=IFERROR(INDEX(Table_Query[CPUM32],MATCH([@UIDPS],Table_Query[UIDS],0)),"""")" where I tried the For Each Loop. I also just used Next without the reference next to it because I know that can slow it down as well. I need complete this task in less than 30 seconds or at least under a minute.
What is the fastest way to achieve the end goal?

Why does my Table calculate each cell when I use SpecialCells(xlCellTypeVisible) with calculations turned off?

 

2 Replies
Here is some of the code used for this in the For Each Loop method.
Sub FormulaUpdate()
Dim Answer As String
Dim rng As Range, cel As Range
Call Setup
Application.Calculation = xlCalculationManual
Sheets("PV").Select

Range("PV[[#Headers],[On Open Contracts Report]]").Select
ActiveCell.Offset(-1, 0).Copy
Range("PV[On Open Contracts Report]").PasteSpecial xlPasteAll
Selection.Calculate
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set rng = Range("PV[On Open Contracts Report]") ‘Table
Application.CutCopyMode = False

'Add copy loop to loop through each cell in "On Open Contracts Report" and if cell = 1 then paste formula into adjacent cells. If Not, then Next Cell
For Each cel In rng.Cells
cel.Select
If cel.Value2 = 1 Then
ActiveCell.Offset(0, 26).Formula2 = "=XLOOKUP([@UIDPS],Table1[UIDS],Table1[GBUNT],"""")"
ActiveCell.Offset(0, 27).FormulaR1C1 = "=XLOOKUP([@UIDPS],Table1[UIDS],Table1[GBDIV],"""")"

'Update Buyer From Open Contracts Report if Open Contracts = 1
ActiveCell.Offset(0, -1).FormulaR1C1 = "=IFERROR(INDEX(Table1[PLNN06],MATCH([@UIDPS],Table1[UIDS],0)),"""")"

'Update Price & Description if Open Contracts = 1
ActiveCell.Offset(0, 6).FormulaR1C1 = "=IFERROR(INDEX(Table1[BUDC35],MATCH([@UIDPS],Table1[UIDS],0)),"""")"
ActiveCell.Offset(0, 7).FormulaR1C1 = "=IFERROR(INDEX(Table1[BUDC35DSC],MATCH([@UIDPS],Table1[UIDS],0)),"""")"

'Update StockRoom if Open Contracts = 1
ActiveCell.Offset(0, 9).FormulaR1C1 = "=IFERROR(INDEX(Table1[STRM32],MATCH([@UIDPS],Table1[UIDS],0)),"""")"

'Update Major Code, Product Description, Minor Code & Descriptions if Open Contracts = 1
ActiveCell.Offset(0, 21).FormulaR1C1 = "=IFERROR(TEXT(INDEX(Table1[PGMJ35],MATCH([@UIDPS],Table1[UIDS],0)),""000""),"""")"
ActiveCell.Offset(0, 23).FormulaR1C1 = "=IFERROR(INDEX(Table1[PGMN35],MATCH([@UIDPS],Table1[UIDS],0)),"""")"
ActiveCell.Offset(0, 24).FormulaR1C1 = "=IFERROR(INDEX(Table1[PGMN35DSC],MATCH([@UIDPS],Table1[UIDS],0)),"""")"

'Update Commodity Code & Description if Open Contracts = 1
ActiveCell.Offset(0, 29).FormulaR1C1 = "=IFERROR(INDEX(Table1[PCLS35],MATCH([@UIDPS],Table1[UIDS],0)),"""")"
ActiveCell.Offset(0, 30).FormulaR1C1 = "=IFERROR(INDEX(Table1[PCLS35DSC],MATCH([@UIDPS],Table1[UIDS],0)),"""")"

'Update UOM and Conversion Factor if Open Contracts = 1
ActiveCell.Offset(0, 45).FormulaR1C1 = "=IFERROR(INDEX(Table1[CPUM32],MATCH([@UIDPS],Table1[UIDS],0)),"""")"
ActiveCell.Offset(0, 46).FormulaR1C1 = "=IFERROR(INDEX(Table1[SUNT35],MATCH([@UIDPS],Table1[UIDS],0)),"""")"
ActiveCell.Offset(0, 47).FormulaR1C1 = "=IFERROR(INDEX(Table1[SPCF35],MATCH([@UIDPS],Table1[UIDS],0)),"""")"

'Update Contract Base Price, Contract Currency, Contract#, If Open Contracts = 1
ActiveCell.Offset(0, 53).FormulaR1C1 = "=INDEX(Table1[PRICE],MATCH([@UIDPS],Table1[UIDS],0))"
ActiveCell.Offset(0, 54).FormulaR1C1 = "=IFERROR(INDEX(Table1[CURRENCY],MATCH([@UID],Table1[UID],0)),"""")"
ActiveCell.Offset(0, 55).FormulaR1C1 = "=IFERROR(INDEX(Table1[CONTRACT],MATCH([@UIDPS],Table1[UIDS],0)),"""")"

'Update Part Description if Open Contracts = 1
DoEvents ‘Prevents Excel from going non-responsive
ActiveCell.Offset(0, -11).FormulaR1C1 = "=IFERROR(INDEX(Table1[ITEM_DESC],MATCH([@UID],Table1[UID],0)),"""")"

End If
Debug.Print cel.Address ‘Just so I can time how fast it is going.
Next
Application.Calculate ‘When completed calculates the sheet.
Looks like I have solved my issue. For those of you who might have this same issue here is how to resolve it.
I created a new sheet and called it Calc (Calculations). I then copied the data columns I needed to calculate from the Table1 Sheet into the Calc sheet. Since the Table1 Sheet had over 250 columns in a table (and Excel only allows use of half your processor when calculating tables) it made calculating anything in this table slow, very slow. So by taking the 12 columns I needed into another sheet, not using a table format allowed me to filter out the data I did not want to place formula lookups in. Then I I entered my formulas accross row 2 and pasted them down (xlEnd) the sheet as SpecialPaste(xlVisibleCellsOnly). Which only took 5 seconds. I am using VBA of course to automate this entire process. Then, I clear the filters, I copy and paste the updated data from Calc back into Table1 Sheet. Whole process took less than 30 seconds versus what would have taken an hour. Would have been nice not to have to come up with a workaround to make Excel work like it should using the first 2 processes I tried. I had to outsmart the programming to make it work efficiently.