Can you use vlookup within a mcaro?

Mac1512

Board Regular
Joined
Jul 26, 2011
Messages
58
I am trying to create a macro to look at payroll info and if the text in column A = Overtime then I want it to lookup(in another list) the word overtime and paste this in another sheet called (JNL) i have got it to work by typing in the code in the macro but i will have varioous codes for different names (see from below macro) not sure how to finish this off
Appreciate any help from you guys, i have gone as far as i can! :mad:
The macro must then run on every line

Sub Payroll()
Dim x As Variant
Dim i As Variant
For x = 10 To 15
i = Sheets("Data").Cells(x, 1)

If i = "Base salary" Then
Sheets("JNL").Cells(x, 14).Value = Sheets("Data").Cells(x, 3)
Sheets("JNL").Cells(x, 8).Value = "6111010"
ElseIf i = "Base salary for overtime" Then
Sheets("JNL").Cells(x, 14).Value = Sheets("Data").Cells(x, 3)
Sheets("JNL").Cells(x, 8).Value = "6111020"
Else
Cells(x, 5).Value = "Sorry No Account Info"
End If
Next x
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I guess you mean something like this, untested though, and fill in some of the parts I guessed:

Code:
Sub Payroll()
    Dim x As Long
    Dim v As Variant
    
    For x = 10 To 15

        v = WorksheetFunction.VLookup(Cells(x, 1).Value, "LOOKUP TABLE", 2, 0)
        
        If IsError(v) Then
        
            Cells(x, 5).Value = "Sorry No Account Info"
        
        Else
        
            Sheets("JNL").Cells(x, 14).Value = Cells(x, 3).Value
            Sheets("JNL").Cells(x, 8).Value = v

        End If
    
    Next x
    
End Sub
 
Upvote 0
Sorry, i cannot get this to work, what does the below line do? do i need to create a function or is "LOOKUP TABLE" the name of the sheet where the lookup takes place? i.e where do i put the lookup table?

v = WorksheetFunction.VLookup(Cells(x, 1).Value, "LOOKUP TABLE", 2, 0)

Cheers
 
Upvote 0
You can put the lookup table where you want, but with "LOOKUP TABLE" I wanted to indicate that you should have a valid reference to your lookup table, just where I wrote: "LOOKUP TABLE".
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

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