Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA code explanation

Posted on 2013-10-30
3
Medium Priority
?
603 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 1000 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 35

Assisted Solution

by:Norie
Norie earned 1000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

963 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