Fixing a Vlookup emulation macro

sheetspread

Well-known Member
Joined
Sep 19, 2005
Messages
5,161
MrKowz graciously wrote this for me last year and I've tried to solve the minor glitch that produces zeroes instead of the multiplication formula.

Excel Workbook
DEF
1ColorFridaySaturday
2Red0.30.7
3Orange0.50.5
4Yellow0.10.9
5Green0.20.8
6Blue0.40.6
Allocation


Is the allocation tab

Excel Workbook
CDEF
11NumberColorFridaySaturday
1234Orange
1312Blue
1476Yellow
158Green
Destination


Is the destination tab

Now the destination tab should look like this if the "vlookupless" macro works:

Excel Workbook
CDEF
11NumberColorFridaySaturday
1234Orange1717
1312Blue4.87.2
1476Yellow7.668.4
158Green1.66.4
Destination


But, after using this macro:

Option Explicit
Public Sub LookupEmulate()
' ** Declare variables
Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long
Dim lookupvalue As String
Dim fridayresult As Double, saturdayresult As Double
Dim ws1 As Worksheet, ws2 As Worksheet
' ** Adjust sheet names accordingly
Set ws1 = Sheets("Allocation")
Set ws2 = Sheets("Destination")
lastrow1 = ws1.Range("D" & Rows.Count).End(xlUp).Row
lastrow2 = ws2.Range("D" & Rows.Count).End(xlUp).Row
' ** Loop to find the lookup value and return the multipliers
' ** This also populates columns E and I on the destination sheet
' ** Adjust beginning of For loops to fit your data
' ** Also adjust column references to fit your data
For i = 12 To lastrow2
lookupvalue = ws2.Range("D" & i).Value
For j = 2 To lastrow1
If ws1.Range("D" & j).Value = lookupvalue Then
fridayresult = Range("E" & j).Value
saturdayresult = Range("F" & j).Value
Exit For
End If
Next j
ws2.Range("E" & i).Value = Range("C" & i).Value * fridayresult
ws2.Range("F" & i).Value = Range("C" & i).Value * saturdayresult
Next i
End Sub


I get this:

Excel Workbook
CDEF
11NumberColorFridaySaturday
1234Orange00
1312Blue00
1476Yellow00
158Green00
Destination




So what part of the code is printing zeroes instead of the multiplication of the allocation and destination cells?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Is the problem:

ws2.Range("E" & i).Value = Range("C" & i).Value * fridayresult
ws2.Range("F" & i).Value = Range("C" & i).Value * saturdayresult

???

I don't know VBA well enough to see why it would or wouldn't work. Does .Value refer to a number or reference? I'd like the cells to be filled with one reference times another reference.
 
Upvote 0
Hi, sheetspread.

Rich (BB code):
ws2.Range("E" & i).Value = Range("C" & i).Value * fridayresult
 ws2.Range("F" & i).Value = Range("C" & i).Value * saturdayresult
is indeed the code that produces 0 or any other values.

Try fixing it to
Rich (BB code):
ws2.Range("E" & i).Value = ws2.Range("C" & i).Value * fridayresult
 ws2.Range("F" & i).Value = ws2.Range("C" & i).Value * saturdayresult

and
Rich (BB code):
fridayresult = Range("E" & j).Value
 saturdayresult = Range("F" & j).Value

to
Rich (BB code):
fridayresult = ws1.Range("E" & j).Value
 saturdayresult = ws1.Range("F" & j).Value
 
Upvote 0
Didn't work, I tried changing all .Value to .Formula and then figured that the lookup part should stay as .Value but neither gave me the product of the cell references. Is there a .Reference?
 
Upvote 0
Finally............After using what little I know to modify the code, it works. Here's how it goes:

Excel Workbook
DEF
1ColorFridaySaturday
2Red0.30.7
3Orange0.50.5
4Yellow0.10.9
5Green0.20.8
6Blue0.40.6
Allocation


Excel Workbook
CDEF
11NumberColorFridaySaturday
1234Orange
1312Blue
1476Yellow
158Green
Destination


Run this code:

Code:
Option Explicit
Public Sub LookupEmulate()
' ** Declare variables
Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long
Dim lookupvalue As String
Dim fridayresult As String, saturdayresult As String
Dim ws1 As Worksheet, ws2 As Worksheet
' ** Adjust sheet names accordingly
Set ws1 = Sheets("Allocation")
Set ws2 = Sheets("Destination")
lastrow1 = ws1.Range("D" & Rows.Count).End(xlUp).Row
lastrow2 = ws2.Range("D" & Rows.Count).End(xlUp).Row
' ** Loop to find the lookup value and return the multipliers
' ** This also populates columns E and I on the destination sheet
' ** Adjust beginning of For loops to fit your data
' ** Also adjust column references to fit your data
For i = 12 To lastrow2
lookupvalue = ws2.Range("D" & i).Value
For j = 2 To lastrow1
If ws1.Range("D" & j).Value = lookupvalue Then
fridayresult = ws1.Range("E" & j).Address
saturdayresult = ws1.Range("F" & j).Address
Exit For
End If
Next j
ws2.Range("E" & i).Formula = "=" & ws2.Range("C" & i).Address & "*" & "Allocation!" & fridayresult
ws2.Range("F" & i).Formula = "=" & ws2.Range("C" & i).Address & "*" & "Allocation!" & saturdayresult
Next i
End Sub


And magically appearing:

Excel Workbook
CDEF
11NumberColorFridaySaturday
1234Orange1717
1312Blue4.87.2
1476Yellow7.668.4
158Green1.66.4
Destination
 
Upvote 0

Forum statistics

Threads
1,224,617
Messages
6,179,914
Members
452,949
Latest member
beartooth91

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