Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default VLookup in VBA Help Needed

I am getting an "Unable to get VLookup property of WorksheetFunction class"
error on line "c.Offset(0, -1) =", even though VendorTable
Referto:=AAV_Table!$B$2:$C$1260

Private Sub cmdUpdate_Click()
Dim c As Range
Dim lRow As Long
Worksheets("MarketList").Activate
lRow = ActiveSheet.UsedRange.Rows.Count

For Each c In Worksheets("MarketList").Range("C2:C" & lRow).Cells
c.Offset(0, -1) = Application.WorksheetFunction.VLookup(c,
VendorTable,2,False)
Next c
End Sub

VendorTable defined he
Worksheets("AAV_Table").Activate
BOReport_lastRow = ActiveSheet.UsedRange.Rows.Count
ThisWorkbook.Names.Add Name:="VendorTable", _
RefersTo:=Worksheets("AAV_Table").Range("B2:C" & BOReport_lastRow)
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default VLookup in VBA Help Needed

Hi,

VendorTable is a named range and not a VBA variable so you use it like the
following

c.Offset(0, -1) = Application.WorksheetFunction.VLookup(c,
Range("VendorTable"), 2, False)

Alternatively you can declare VendorTable as a range variable and use it
like your example but it needs to be done just prior to the VLooup.

Dim c As Range
Dim lRow As Long
Dim BOReport_lastRow
Dim VendorTable As Range

Worksheets("MarketList").Activate
lRow = ActiveSheet.UsedRange.Rows.Count

With Worksheets("AAV_Table")
BOReport_lastRow = .UsedRange.Rows.Count
Set VendorTable = .Range("B2:C" & BOReport_lastRow)
End With

For Each c In Worksheets("MarketList").Range("C2:C" & lRow).Cells

c.Offset(0, -1) = Application.WorksheetFunction.VLookup(c, VendorTable, 2,
False)

Next c



--
Regards,

OssieMac


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default VLookup in VBA Help Needed

Also there is no need to activate/select worksheets or ranges. The following
code

Worksheets("MarketList").Activate
lRow = ActiveSheet.UsedRange.Rows.Count

can be replaced with this

With Worksheets("MarketList")
lRow = .UsedRange.Rows.Count
End With

or with this
lRow = Worksheets("MarketList").UsedRange.Rows.Count

--
Regards,

OssieMac


  #4   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default VLookup in VBA Help Needed

Thanks Mac. Now how do I account for the occassion when
Application.WorksheetFunction.VLookup(c, VendorTable, 2,False) IsNA?.
How do I write that in code.


"OssieMac" wrote:

Hi,

VendorTable is a named range and not a VBA variable so you use it like the
following

c.Offset(0, -1) = Application.WorksheetFunction.VLookup(c,
Range("VendorTable"), 2, False)

Alternatively you can declare VendorTable as a range variable and use it
like your example but it needs to be done just prior to the VLooup.

Dim c As Range
Dim lRow As Long
Dim BOReport_lastRow
Dim VendorTable As Range

Worksheets("MarketList").Activate
lRow = ActiveSheet.UsedRange.Rows.Count

With Worksheets("AAV_Table")
BOReport_lastRow = .UsedRange.Rows.Count
Set VendorTable = .Range("B2:C" & BOReport_lastRow)
End With

For Each c In Worksheets("MarketList").Range("C2:C" & lRow).Cells

c.Offset(0, -1) = Application.WorksheetFunction.VLookup(c, VendorTable, 2,
False)

Next c



--
Regards,

OssieMac


  #5   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default VLookup in VBA Help Needed

Thanks Mac.
Now how do I account for the occassion when
Application.WorksheetFunction.VLookup(c, VendorTable, 2,False) IsNA?.
How do I write that in code.



"OssieMac" wrote:

Also there is no need to activate/select worksheets or ranges. The following
code

Worksheets("MarketList").Activate
lRow = ActiveSheet.UsedRange.Rows.Count

can be replaced with this

With Worksheets("MarketList")
lRow = .UsedRange.Rows.Count
End With

or with this
lRow = Worksheets("MarketList").UsedRange.Rows.Count

--
Regards,

OssieMac




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default VLookup in VBA Help Needed


Try this. It will leave a blank for any not found. However, if running the
code on the same range as has been previously used then need to clear the
range first otherwise it will leave the old value.

Note that a space and an underscore at the end of a line is a line break in
an otherwise single line of code.

Worksheets("MarketList") _
.Range("C2:C" & lRow).Offset(0, -1) _
.ClearContents

For Each c In Worksheets("MarketList") _
.Range("C2:C" & lRow).Cells
On Error Resume Next
c.Offset(0, -1) = Application.WorksheetFunction _
.VLookup(c, VendorTable, 2, False)
On Error GoTo 0 'Reset error trapping ASAP
Next c

--
Regards,

OssieMac


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default VLookup in VBA Help Needed

There are two ways to handle an error from a worksheet function used
in VBA. If you include the WorksheetFunction reference in the line of
code, you need to use On Error Resume Next, call the function, and
test the Err.Number value:

Dim V As Variant
On Error Resume Next
Err.Clear
V = Application.WorksheetFunction.VLookup(123, Range("A1:C10"), 3,
False)
If Err.Number < 0 Then
Debug.Print "error with VLOOKUP"
End If


You can omit the WorksheetFunction from the call. In this case, the
Variant V will contain an error subtype value indicating the error. No
On Error Resume Next is required.

Dim V As Variant
V = Application.VLookup(123, Range("A1:C10"), 3, False)
If IsError(V) Then
Debug.Print "error with VLOOKUP"
End If


Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com






On Thu, 8 Apr 2010 13:16:01 -0700, Ayo
wrote:

Thanks Mac.
Now how do I account for the occassion when
Application.WorksheetFunction.VLookup(c, VendorTable, 2,False) IsNA?.
How do I write that in code.



"OssieMac" wrote:

Also there is no need to activate/select worksheets or ranges. The following
code

Worksheets("MarketList").Activate
lRow = ActiveSheet.UsedRange.Rows.Count

can be replaced with this

With Worksheets("MarketList")
lRow = .UsedRange.Rows.Count
End With

or with this
lRow = Worksheets("MarketList").UsedRange.Rows.Count

--
Regards,

OssieMac


  #8   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default VLookup in VBA Help Needed

The same error "Unable to get VLookup property of WorksheetFunction class" is
back. Run-time error '1004':

"OssieMac" wrote:


Try this. It will leave a blank for any not found. However, if running the
code on the same range as has been previously used then need to clear the
range first otherwise it will leave the old value.

Note that a space and an underscore at the end of a line is a line break in
an otherwise single line of code.

Worksheets("MarketList") _
.Range("C2:C" & lRow).Offset(0, -1) _
.ClearContents

For Each c In Worksheets("MarketList") _
.Range("C2:C" & lRow).Cells
On Error Resume Next
c.Offset(0, -1) = Application.WorksheetFunction _
.VLookup(c, VendorTable, 2, False)
On Error GoTo 0 'Reset error trapping ASAP
Next c

--
Regards,

OssieMac


  #9   Report Post  
Posted to microsoft.public.excel.programming
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default VLookup in VBA Help Needed

Thanks Chip.
I know what the error is, I just want to ignore it. The error is because the
lookup_value is not in the range. When this occurs, I what the macro to
ignore the error and move on to the next cell. That is what I am trying to
get to.

"Chip Pearson" wrote:

There are two ways to handle an error from a worksheet function used
in VBA. If you include the WorksheetFunction reference in the line of
code, you need to use On Error Resume Next, call the function, and
test the Err.Number value:

Dim V As Variant
On Error Resume Next
Err.Clear
V = Application.WorksheetFunction.VLookup(123, Range("A1:C10"), 3,
False)
If Err.Number < 0 Then
Debug.Print "error with VLOOKUP"
End If


You can omit the WorksheetFunction from the call. In this case, the
Variant V will contain an error subtype value indicating the error. No
On Error Resume Next is required.

Dim V As Variant
V = Application.VLookup(123, Range("A1:C10"), 3, False)
If IsError(V) Then
Debug.Print "error with VLOOKUP"
End If


Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com






On Thu, 8 Apr 2010 13:16:01 -0700, Ayo
wrote:

Thanks Mac.
Now how do I account for the occassion when
Application.WorksheetFunction.VLookup(c, VendorTable, 2,False) IsNA?.
How do I write that in code.



"OssieMac" wrote:

Also there is no need to activate/select worksheets or ranges. The following
code

Worksheets("MarketList").Activate
lRow = ActiveSheet.UsedRange.Rows.Count

can be replaced with this

With Worksheets("MarketList")
lRow = .UsedRange.Rows.Count
End With

or with this
lRow = Worksheets("MarketList").UsedRange.Rows.Count

--
Regards,

OssieMac


.

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default VLookup in VBA Help Needed

Did you change anything in the code? The following is the entire sub that I
used to test and it works fine.

Sub test()
Dim c As Range
Dim lRow As Long
Dim BOReport_lastRow
Dim VendorTable As Range

lRow = Worksheets("MarketList").UsedRange.Rows.Count

With Worksheets("AAV_Table")
BOReport_lastRow = .UsedRange.Rows.Count
Set VendorTable = .Range("B2:C" & BOReport_lastRow)
End With

Worksheets("MarketList") _
.Range("C2:C" & lRow).Offset(0, -1) _
.ClearContents

For Each c In Worksheets("MarketList") _
.Range("C2:C" & lRow).Cells
On Error Resume Next
c.Offset(0, -1) = Application.WorksheetFunction _
.VLookup(c, VendorTable, 2, False)
On Error GoTo 0 'Reset error trapping ASAP
Next c
End Sub

--
Regards,

OssieMac

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
VLOOKUP Help Needed blucajun Excel Discussion (Misc queries) 3 December 11th 08 03:48 AM
VLOOKUP help needed PLEASE... Levi Excel Worksheet Functions 2 May 8th 06 08:41 PM
Help needed on VLOOKUP Dr Phibes Excel Discussion (Misc queries) 6 April 13th 06 10:00 AM
Vlookup Help Needed nander Excel Discussion (Misc queries) 1 April 4th 06 07:40 PM
Vlookup Help Needed Eaglered Excel Discussion (Misc queries) 1 May 19th 05 10:36 PM


All times are GMT +1. The time now is 07:32 PM.

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"