hmmm, vlookup with an if statement as a worksheet function in VBA....

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
2,056
Office Version
  1. 365
Platform
  1. Windows
so, i am trying to replicate a vlookup, that has a lookup value determined by an if statement, in VBA.

the formula seeks to lookup a name associated with a project. the lookup value for each project is the concatenation of the project name and project phase. the project phase appears within a string so is determined by finding either of two words that appear at the start of each phase.

the phase might start with "Description" or it might start with "Resource".

the lookup value formula that works in the sheet is:

Procurement Forward Plan (Live) (A7702697).xlsb
ABCD
36LookupValueContract OwnerContractDescription
37COM-ABC-U-0012Resource - Design Period of ProjectNOT YET ALLOCATEDCOM-ABC-U-0012Common Street ABC Upgrade - Technical words here for my industry - Description - Resource - Design Period of Project
Sheet1
Cell Formulas
RangeFormula
A37A37=IF(B37<>"NOT YET ALLOCATED",TRIM(C37),TRIM(LEFT(C37,14)&IF(COUNTIF(D37,"*Resource*"),RIGHT(D37,(LEN(D37)-(FIND("Description",D37)+13))),RIGHT(D37,(LEN(D37)-(FIND("Description",D37)-1))))))


in the macro i am writing, i want to use a loop to look for all projects "NOT YET ALLOCATED" and then look up the correct person for that project.

my macro so far:

Code:
Sub NYAprojects()

'////REPLACES NOT YET ALLOCATED

    Dim COsht As Worksheet
    Dim COrowLast As Long
    Dim COrng As Range
    Dim COarr As Variant
    Dim n As Long
    
    With Application
    .ScreenUpdating = False ' stop screen flashing as macro runs
    .DisplayAlerts = False ' stop alert messages
    .EnableEvents = False ' disable events running
    End With
   
    Set COsht = Worksheets("Forward Plan")
    With COsht
        COrowLast = .Range("A" & Rows.Count).End(xlUp).Row
        Set COrng = .Range(.Cells(45, "A"), .Cells(rowLast, "A"))
        COarr = COrng.Value2
    End With
   
    For n = 1 To UBound(COarr)
        If (COarr(n, 1)) = "NOT YET ALLOCATED" Then
                COarr(n, 1) = /////VLOOKUP TO GO HERE/////

application.worksheetfunction.vlookup(IF(COUNTIF(d5,"*Resource*"),RIGHT(d5,(LEN(d5)-(FIND("Description",d5)+13))),RIGHT(d5,(LEN(d5)-(FIND("Description",d5)-1))),other sheet range, 2,0))



        End If
    Next i
   
    rng.Value2 = COarr

    With Application
         'turn each back on
        .ScreenUpdating = True
        .DisplayAlerts = True
        .EnableEvents = True
    End With

End Sub

help or suggestions of other ways to do the same thing would be greatly appreciated.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
anyone?

i thought maybe i should assign the lookup value to a variable but am getting lost.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top