?
Solved

Excel VBA code explanation

Posted on 2013-10-30
3
Medium Priority
?
591 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 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 34

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

764 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