Solved

Excel VBA code explanation

Posted on 2013-10-30
3
578 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
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 33

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel formula - data format 5 22
Copy and Paste Text into Text Box 3 29
Consolidation of Worksheet into a final worksheet 4 41
Formula to run in whole column: 15 16
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

821 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