Solved

VBA to do CalX

Posted on 2012-12-28
14
386 Views
Last Modified: 2012-12-29
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
0
Comment
Question by:Rayne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
14 Comments
 
LVL 16

Assisted Solution

by:terencino
terencino earned 225 total points
ID: 38728387
Hi Rayne, in case you would consider a non-VBA solution, here is one using INDEX & MATCH.

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

Author Comment

by:Rayne
ID: 38728436
Thanks Terry :)
That works
0
 

Author Comment

by:Rayne
ID: 38728440
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?
0
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 26

Expert Comment

by:redmondb
ID: 38728445
... and a VBA version...
divisorLookUp-V2.xlsm
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38728464
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
0
 

Author Comment

by:Rayne
ID: 38728623
Thanks Brian :)

Thats the more dynamic way I wanted.
0
 
LVL 26

Accepted Solution

by:
redmondb earned 275 total points
ID: 38728626
Apologies, Rayne, the two "Match" functions were both missing their Match Types.
divisorLookUp-V4.xlsm
0
 

Author Comment

by:Rayne
ID: 38728647
No problem Brian,

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

Author Comment

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

Got it :)
0
 

Author Closing Comment

by:Rayne
ID: 38728661
Thanks to both the experts for their awesome help :)
0
 
LVL 16

Expert Comment

by:terencino
ID: 38728674
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
0
 

Author Comment

by:Rayne
ID: 38728817
Thanks Terry :)
Sure I will keep that mind.
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38728951
Thanks, Rayne.

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

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question