Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default loop and vlookup

I'm doing a vlookup to find the matching identifier in another excel sheet,
and when it doesn't find the matching identifier, then look for another
identifier in the sheet, if neither are there return a zero. But for some
reason, when it doesn't find the first identifier it doesn't look for the
other, it just returns zero, any idea why?
This is what I have:

For i = 3 To lastRow Step 1
tmp = VariantType.Empty

On Error Resume Next
tmp = LCV.WorksheetFunction.VLookup(LCV.Range("A" & i).Value,
oApp.Range("A1:J" & lastRow2).Value, 10, False)
On Error GoTo 0

If tmp = VariantType.Empty Then
On Error Resume Next
tmp = LCV.WorksheetFunction.VLookup(LCV.Range("B" &
i).Value, oApp.Range("B1:J" & lastRow2).Value, 10, False)
On Error GoTo 0
If tmp = VariantType.Empty Then
LCV.Range("L" & i).Value = 0
Else : LCV.Range("L" & i).Value = tmp
End If
Else : LCV.Range("L" & i).Value = tmp
End If
Next i

Thanks in advance!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default loop and vlookup

Hi,

You have some errors in your code. I don't know what VariantType.Empty is.
Can't find anything on it in help so I used an alternative method.

The function does not refer to a specific worksheet; it is simply a
worksheet function and the name of the worksheet is not included.

The table to lookin is a range and should not have '.Value' after it.

Note the comment before your second VlookUp. You were trying to look in a
column which was outside the table range.

Set LCV = Sheets("Sheet1")
Set oAPP = Sheets("Sheet2")

For i = 3 To lastRow Step 1
tmp = Empty
On Error Resume Next
tmp = WorksheetFunction.VLookup(LCV.Range("A" & i) _
.Value, oAPP.Range("A1:J" & lastRow2), 10, False)
On Error GoTo 0

If tmp = Empty Then

On Error Resume Next

'In the next line of code columns B to J is only
'9 columns wide. Can't say look in column 10
tmp = WorksheetFunction.VLookup(LCV.Range("B" & i) _
.Value, oAPP.Range("B1:J" & lastRow2), 9, False)

On Error GoTo 0

If tmp = Empty Then
LCV.Range("L" & i).Value = 0
Else
LCV.Range("L" & i).Value = tmp
End If
Else
LCV.Range("L" & i).Value = tmp
End If

Next i

Regards,

OssieMac


"NewToVB" wrote:

I'm doing a vlookup to find the matching identifier in another excel sheet,
and when it doesn't find the matching identifier, then look for another
identifier in the sheet, if neither are there return a zero. But for some
reason, when it doesn't find the first identifier it doesn't look for the
other, it just returns zero, any idea why?
This is what I have:

For i = 3 To lastRow Step 1
tmp = VariantType.Empty

On Error Resume Next
tmp = LCV.WorksheetFunction.VLookup(LCV.Range("A" & i).Value,
oApp.Range("A1:J" & lastRow2).Value, 10, False)
On Error GoTo 0

If tmp = VariantType.Empty Then
On Error Resume Next
tmp = LCV.WorksheetFunction.VLookup(LCV.Range("B" &
i).Value, oApp.Range("B1:J" & lastRow2).Value, 10, False)
On Error GoTo 0
If tmp = VariantType.Empty Then
LCV.Range("L" & i).Value = 0
Else : LCV.Range("L" & i).Value = tmp
End If
Else : LCV.Range("L" & i).Value = tmp
End If
Next i

Thanks in advance!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default loop and vlookup

NewToVB is writing in VSTO.

Probably a good thing to include in all of his posts when he's looking for an
answer that will work in VSTO, huh?

(is that too passive aggressive?)

OssieMac wrote:

Hi,

You have some errors in your code. I don't know what VariantType.Empty is.
Can't find anything on it in help so I used an alternative method.

The function does not refer to a specific worksheet; it is simply a
worksheet function and the name of the worksheet is not included.

The table to lookin is a range and should not have '.Value' after it.

Note the comment before your second VlookUp. You were trying to look in a
column which was outside the table range.

Set LCV = Sheets("Sheet1")
Set oAPP = Sheets("Sheet2")

For i = 3 To lastRow Step 1
tmp = Empty
On Error Resume Next
tmp = WorksheetFunction.VLookup(LCV.Range("A" & i) _
.Value, oAPP.Range("A1:J" & lastRow2), 10, False)
On Error GoTo 0

If tmp = Empty Then

On Error Resume Next

'In the next line of code columns B to J is only
'9 columns wide. Can't say look in column 10
tmp = WorksheetFunction.VLookup(LCV.Range("B" & i) _
.Value, oAPP.Range("B1:J" & lastRow2), 9, False)

On Error GoTo 0

If tmp = Empty Then
LCV.Range("L" & i).Value = 0
Else
LCV.Range("L" & i).Value = tmp
End If
Else
LCV.Range("L" & i).Value = tmp
End If

Next i

Regards,

OssieMac

"NewToVB" wrote:

I'm doing a vlookup to find the matching identifier in another excel sheet,
and when it doesn't find the matching identifier, then look for another
identifier in the sheet, if neither are there return a zero. But for some
reason, when it doesn't find the first identifier it doesn't look for the
other, it just returns zero, any idea why?
This is what I have:

For i = 3 To lastRow Step 1
tmp = VariantType.Empty

On Error Resume Next
tmp = LCV.WorksheetFunction.VLookup(LCV.Range("A" & i).Value,
oApp.Range("A1:J" & lastRow2).Value, 10, False)
On Error GoTo 0

If tmp = VariantType.Empty Then
On Error Resume Next
tmp = LCV.WorksheetFunction.VLookup(LCV.Range("B" &
i).Value, oApp.Range("B1:J" & lastRow2).Value, 10, False)
On Error GoTo 0
If tmp = VariantType.Empty Then
LCV.Range("L" & i).Value = 0
Else : LCV.Range("L" & i).Value = tmp
End If
Else : LCV.Range("L" & i).Value = tmp
End If
Next i

Thanks in advance!


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default loop and vlookup

Thank you for your help! The reason I'm using variant type is because I'm
using visual studio rather than VBA. tmp is declared as VariantType, and I
tried at first to use tmp = Empty but it was an error, so I had to use tmp =
VariantType.Empty. Also for the vlookup, LCV is the excel application that I
have to refer to rather than the worksheet. I can't run it with just
("worksheetfunction.VLookUp..."). Good call on the table range.. but for
some reason, I'm still getting a zero for the second Vlookup and the
identifier is there. Could it be because the cell I'm looking for is all
caps? I have both columns (the identifier its looking for, and the range its
looking in) in the second Vlookup formatted as text so I don't think that
would matter...but I don't know what else it could be. Thanks in advance!!

"NewToVB" wrote:

I'm doing a vlookup to find the matching identifier in another excel sheet,
and when it doesn't find the matching identifier, then look for another
identifier in the sheet, if neither are there return a zero. But for some
reason, when it doesn't find the first identifier it doesn't look for the
other, it just returns zero, any idea why?
This is what I have:

For i = 3 To lastRow Step 1
tmp = VariantType.Empty

On Error Resume Next
tmp = LCV.WorksheetFunction.VLookup(LCV.Range("A" & i).Value,
oApp.Range("A1:J" & lastRow2).Value, 10, False)
On Error GoTo 0

If tmp = VariantType.Empty Then
On Error Resume Next
tmp = LCV.WorksheetFunction.VLookup(LCV.Range("B" &
i).Value, oApp.Range("B1:J" & lastRow2).Value, 10, False)
On Error GoTo 0
If tmp = VariantType.Empty Then
LCV.Range("L" & i).Value = 0
Else : LCV.Range("L" & i).Value = tmp
End If
Else : LCV.Range("L" & i).Value = tmp
End If
Next i

Thanks in advance!

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default loop and vlookup

Case doesn't matter to =vlookup().

You may want to look at your data once more. Maybe there's a difference (extra
space???).

In fact, open the workbooks and build the formula manually and see what
happens. Maybe that'll give you a hint.



NewToVB wrote:

Thank you for your help! The reason I'm using variant type is because I'm
using visual studio rather than VBA. tmp is declared as VariantType, and I
tried at first to use tmp = Empty but it was an error, so I had to use tmp =
VariantType.Empty. Also for the vlookup, LCV is the excel application that I
have to refer to rather than the worksheet. I can't run it with just
("worksheetfunction.VLookUp..."). Good call on the table range.. but for
some reason, I'm still getting a zero for the second Vlookup and the
identifier is there. Could it be because the cell I'm looking for is all
caps? I have both columns (the identifier its looking for, and the range its
looking in) in the second Vlookup formatted as text so I don't think that
would matter...but I don't know what else it could be. Thanks in advance!!

"NewToVB" wrote:

I'm doing a vlookup to find the matching identifier in another excel sheet,
and when it doesn't find the matching identifier, then look for another
identifier in the sheet, if neither are there return a zero. But for some
reason, when it doesn't find the first identifier it doesn't look for the
other, it just returns zero, any idea why?
This is what I have:

For i = 3 To lastRow Step 1
tmp = VariantType.Empty

On Error Resume Next
tmp = LCV.WorksheetFunction.VLookup(LCV.Range("A" & i).Value,
oApp.Range("A1:J" & lastRow2).Value, 10, False)
On Error GoTo 0

If tmp = VariantType.Empty Then
On Error Resume Next
tmp = LCV.WorksheetFunction.VLookup(LCV.Range("B" &
i).Value, oApp.Range("B1:J" & lastRow2).Value, 10, False)
On Error GoTo 0
If tmp = VariantType.Empty Then
LCV.Range("L" & i).Value = 0
Else : LCV.Range("L" & i).Value = tmp
End If
Else : LCV.Range("L" & i).Value = tmp
End If
Next i

Thanks in advance!


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default loop and vlookup

I got it! On OssiMac's post earlier, about the table to looking in range,
rather than changing 10 to 9, i changed "B1:J" to "A1:J" and then I realized
that the column its looking in has to be the left most column and the value
I'm looking for is in Column B. Thanks for the help!

"Dave Peterson" wrote:

Case doesn't matter to =vlookup().

You may want to look at your data once more. Maybe there's a difference (extra
space???).

In fact, open the workbooks and build the formula manually and see what
happens. Maybe that'll give you a hint.



NewToVB wrote:

Thank you for your help! The reason I'm using variant type is because I'm
using visual studio rather than VBA. tmp is declared as VariantType, and I
tried at first to use tmp = Empty but it was an error, so I had to use tmp =
VariantType.Empty. Also for the vlookup, LCV is the excel application that I
have to refer to rather than the worksheet. I can't run it with just
("worksheetfunction.VLookUp..."). Good call on the table range.. but for
some reason, I'm still getting a zero for the second Vlookup and the
identifier is there. Could it be because the cell I'm looking for is all
caps? I have both columns (the identifier its looking for, and the range its
looking in) in the second Vlookup formatted as text so I don't think that
would matter...but I don't know what else it could be. Thanks in advance!!

"NewToVB" wrote:

I'm doing a vlookup to find the matching identifier in another excel sheet,
and when it doesn't find the matching identifier, then look for another
identifier in the sheet, if neither are there return a zero. But for some
reason, when it doesn't find the first identifier it doesn't look for the
other, it just returns zero, any idea why?
This is what I have:

For i = 3 To lastRow Step 1
tmp = VariantType.Empty

On Error Resume Next
tmp = LCV.WorksheetFunction.VLookup(LCV.Range("A" & i).Value,
oApp.Range("A1:J" & lastRow2).Value, 10, False)
On Error GoTo 0

If tmp = VariantType.Empty Then
On Error Resume Next
tmp = LCV.WorksheetFunction.VLookup(LCV.Range("B" &
i).Value, oApp.Range("B1:J" & lastRow2).Value, 10, False)
On Error GoTo 0
If tmp = VariantType.Empty Then
LCV.Range("L" & i).Value = 0
Else : LCV.Range("L" & i).Value = tmp
End If
Else : LCV.Range("L" & i).Value = tmp
End If
Next i

Thanks in advance!


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add loop to Vlookup Les Excel Programming 1 April 19th 07 04:05 AM
VLOOKUP loop multiple times Lenny Excel Worksheet Functions 3 September 28th 05 10:31 AM
Need a vb loop for vlookup [email protected] Excel Programming 3 August 25th 05 12:32 PM
Need a vb loop for vlookup [email protected] Excel Programming 2 August 24th 05 04:07 PM
Find & Replace / Loop & Vlookup thom hoyle Excel Programming 5 June 25th 05 12:56 AM


All times are GMT +1. The time now is 12:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"