Solved

Excel VBA code explanation

Posted on 2013-10-30
3
582 Views
Last Modified: 2013-10-30
I have inherited this function from someone else. Could I get some assistance as to what this is doing?  Especially the lines…
SumOrCat = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 4, False)
    s1stWorstCase = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 5, False)
    s2ndWorstCase = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 6, False)
    s3rdWorstCase = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 7, False)
    s4thWorstCase = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 8, False)
May be a parameter break down.

Here is the function…
Public Function GetWorstCase(ByVal QuestCode As String, ByVal LatestValue As String, ByRef CurrWorstCase) As String
    Dim s1stWorstCase As String
    Dim s2ndWorstCase As String
    Dim s3rdWorstCase As String
    Dim s4thWorstCase As String
    Dim SumOrCat As String
   
    On Error GoTo ErrorHandler
   
    SumOrCat = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 4, False)
    s1stWorstCase = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 5, False)
    s2ndWorstCase = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 6, False)
    s3rdWorstCase = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 7, False)
    s4thWorstCase = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 8, False)
   
    GetWorstCase = LatestValue
Exit Function

ErrorHandler:
   MsgBox "Error: " & Err.Description

End Function

Sub test()
    Debug.Print GetWorstCase("GQ-001", "Yes", "No")
End Sub

Thanks in advance.

I have attached this problem statement in a Word document so this can be seen better.
Thanks.
Get-Worse-Case-function.docx
0
Comment
Question by:tesla764
[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
3 Comments
 
LVL 15

Accepted Solution

by:
Ess Kay earned 250 total points
ID: 39611831
There are 5 variables being filled here, highlighted in BOLD
They all call a function WorksheetFunction.VLookup
Everything in the function is the same except the 3rd parameter, which is 4,5,6,7 or 8

They all select CELLS from A1 to H496

You need to look up the VLookup function to see what it does.



SumOrCat = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 4, False)

    s1stWorstCase = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 5, False)

    s2ndWorstCase = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 6, False)

    s3rdWorstCase = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 7, False)

    s4thWorstCase = WorksheetFunction.VLookup(QuestCode, Sheets("Data").Range("$A$1:$H$496"), 8, False)
0
 
LVL 34

Assisted Solution

by:Norie
Norie earned 250 total points
ID: 39611841
That section is using the worksheet function VLOOKUP to find QuestCode in column A of the range $A$1:$H$496 on the 'Data' worksheet.

If the QuestCode is found in a row corresponding values from other columns are returned to various variables.

The value from column D of the row QuestCode is found is returned to SumOrCat.

The values from columns E, F, G and H are returned to s1stWorstCase, s2ndWorstCase, s3rdWorstCase, and s4thWorstCase respectively.

This is the general syntax fo VLOOKUP.

VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)
0
 

Author Closing Comment

by:tesla764
ID: 39611903
Thanks.
0

Featured Post

Office 365 Advanced Training for Admins

Special Offer:  Buy 1 course, get 2nd free!  Buy the 'Managing Office 365 Identities & Requirements' course w/ Accelerated TestPrep, and automatically receive the 'Enabling Office 365 Services' course FREE!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
drag and drop (but keep all) lines 10 36
Create email hyperlinks from regular text 3 26
Excel Date Question 8 44
count number 10 27
As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
My experience with Windows 10 over a one year period and suggestions for smooth operation
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

739 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