Dynamic Vlookup

mike92500

New Member
Joined
May 21, 2012
Messages
1
Hello all, I am new to VBA and really trying to improve. I tried looking a solution up, but couldn't really find what I need. Can anyone help me? or have an idea would be great! I really appreciate it!

I can't attach the xls, but here is how its presented:
Column A has the daily dates, column D has the month and column E has the price corresponding to the month.

Column A ColumnD (doesn't really matter) ColumnE
5/21/2012 Bal May -1.3
5/22/2012 June -1.0
5/23/2012 Jul -0.65
etc.... Aug etc....
3q 12
4q 12

So I need to match the prices (column E) with the days of that month (column A) and put the prices in column B, so Bal May price -1.3 would be attached to all the days of May, and q3 12 would be attached to all the days in Sep, and 4q 12 would be attached to all the days of October, November, December...
What I coded was basically, return the number of the month of date, so 5/21/2012 would return 5, and then look in the column D for month 5 and return with a Vlookup the price in column E (-1.3).
My problem comes with 1q, 2q, 3q and 4q....because basically I need some code to search in this example "Sep" not find it, and return 3q 12 price, but also need to search Oct, Nov, and Dec and retun the 4q 12 price. The problem is that at one point in time the Oct price won't be 4q 12 anymore, etc....So my Vlookup is stuck there.....Here is my code below....If u have just an idea as to what I should be trying to do, that would be GREAT!! THANKS A LOT!

Public Sub vlookup()

'this looks up for a month, and returns the corresponding number of the month

For count = 7 To 415
Set monthCell = Worksheets("Price curve").Cells(count, 17)
Set dateCell = Worksheets("Price curve").Cells(count, 1)

If monthCell.Value = "Jan" Then monthCell.Offset(0, -1).Value = 1
If monthCell.Value = "Bal Jan" Then monthCell.Offset(0, -1).Value = 1
If monthCell.Value = "Feb" Then monthCell.Offset(0, -1).Value = 2
If monthCell.Value = "Bal Feb" Then monthCell.Offset(0, -1).Value = 2
If monthCell.Value = "Mar" Then monthCell.Offset(0, -1).Value = 3
If monthCell.Value = "Bal Mar" Then monthCell.Offset(0, -1).Value = 3
If monthCell.Value = "Apr" Then monthCell.Offset(0, -1).Value = 4
If monthCell.Value = "Bal Apr" Then monthCell.Offset(0, -1).Value = 4
If monthCell.Value = "May" Then monthCell.Offset(0, -1).Value = 5
If monthCell.Value = "Bal May" Then monthCell.Offset(0, -1).Value = 5
If monthCell.Value = "June" Then monthCell.Offset(0, -1).Value = 6
If monthCell.Value = "Bal June" Then monthCell.Offset(0, -1).Value = 6
If monthCell.Value = "July" Then monthCell.Offset(0, -1).Value = 7
If monthCell.Value = "Bal July" Then monthCell.Offset(0, -1).Value = 7
If monthCell.Value = "Aug" Then monthCell.Offset(0, -1).Value = 8
If monthCell.Value = "Bal Aug" Then monthCell.Offset(0, -1).Value = 8
If monthCell.Value = "Sep" Then monthCell.Offset(0, -1).Value = 9
If monthCell.Value = "Bal Sep" Then monthCell.Offset(0, -1).Value = 9
If monthCell.Value = "Oct" Then monthCell.Offset(0, -1).Value = 10
If monthCell.Value = "Bal Oct" Then monthCell.Offset(0, -1).Value = 10
If monthCell.Value = "Nov" Then monthCell.Offset(0, -1).Value = 11
If monthCell.Value = "Bal Nov" Then monthCell.Offset(0, -1).Value = 11
If monthCell.Value = "Dec" Then monthCell.Offset(0, -1).Value = 12
If monthCell.Value = "Bal Dec" Then monthCell.Offset(0, -1).Value = 12

dateCell.Offset(0, 1) = Application.vlookup(Month(dateCell), Range("P7:S24"), 4, False)

'I tried the below to solve my pb, but it doesn't since Aug value 10, takes over Sep value...'

If monthCell.Value <> "Sep" And monthCell.Value <> "Bal Sep" And monthCell.Value = "3q 12" Then monthCell.Offset(0, -1).Value = 9
If monthCell.Value <> "Aug" And monthCell.Value <> "Bal Aug" And monthCell.Value = "3q 12" Then monthCell.Offset(0, -1).Value = 10


Next count

End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,215,007
Messages
6,122,670
Members
449,091
Latest member
peppernaut

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