VBA to do CalX

Rayne
Rayne used Ask the Experts™
on
Hello To All VBA Gurus,

I am looking for the best VBA way to do this…..
There are two tables: tbl1 and tbl2_divisors
Requirements
Tbl1 has a total column that needs to be converted to three other columns via dividing by a specific number. That specific number is specific to a prod. TBL2_Divisors has the three divisors type isted in three columns for a one each prod.
Need to a VBA to do the following:
•      If  E5  has a value, and
•      Look up the divisor for the combination of (prod + convert) in tbl2_divisor
•      Then divide E5/looked up and put in F5.
•      Likewise do the same for G and H as well.
•      Do the above for all the elements in column D


Thanks
R
divisorLookUp.xlsx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi Rayne, in case you would consider a non-VBA solution, here is one using INDEX & MATCH.

Hope that helps
...Terry
divisorLookUp-2.xlsx

Author

Commented:
Thanks Terry :)
That works

Author

Commented:
Just found out that both the rows in the tables can vary and will grow with time, so I am not sure if hardcoding them in formulas will work? What do you think for dynamic ranges?
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

... and a VBA version...
divisorLookUp-V2.xlsm
Rayne,

Oops, crossing posts. The attached "expands" TBL_2_Divisors down to (but not including) the first blank cell below K5.
Option Explicit

Sub Build_Conversion()
Dim xLast_Row As Long
Dim xDivisor_End As Long

Sheets("Sheet1").Activate

xLast_Row = Range("A1").SpecialCells(xlLastCell).Row
If xLast_Row < 5 Then
    MsgBox ("No data found - run cancelled.")
    Exit Sub
End If

Range("K5").End(xlDown).Select
xDivisor_End = ActiveCell.Row
If xDivisor_End = Cells.Rows.Count Then
    MsgBox ("No Divisors found - run cancelled.")
    Exit Sub
End If

Range("F5").Formula = "=IF($E5="""","""",IFERROR($E5/INDEX($L$5:$N$" & xDivisor_End & ",MATCH(""D_""&$D5,$K$5:$K$" & xDivisor_End & "),MATCH(""ff_""&F$4,$L$4:$N$4,)),""""))"
Range("F5").Copy Destination:=Range("F5:H" & xLast_Row)
With Range("F5:H" & xLast_Row)
    .Copy
    .PasteSpecial xlPasteValues
End With

Application.CutCopyMode = False
Range("F5").Select

End Sub

Open in new window

Regards,
Brian.
divisorLookUp-V3.xlsm

Author

Commented:
Thanks Brian :)

Thats the more dynamic way I wanted.
Apologies, Rayne, the two "Match" functions were both missing their Match Types.
divisorLookUp-V4.xlsm

Author

Commented:
No problem Brian,

I am not sure what you meant but your previous solution worked gold for me :)

Author

Commented:
ahh I see you remove the last ",0" from the old match below
MATCH(""D_""&$D5,$K$5:$K$" & xDivisor_End & ",0)

Got it :)

Author

Commented:
Thanks to both the experts for their awesome help :)
Hi Rayne, just to finish this off for both options, please refer attached using dynamic ranges which will adapt when new rows are added. The named ranges are based on an OFFSET function which makes them dynamic.
divisorLookUp-3.xlsx

Author

Commented:
Thanks Terry :)
Sure I will keep that mind.
Thanks, Rayne.

Apologies for butting in, Terry, I was happy for your nice, clean solution to get the points.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial