#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,069
Default vlookup text in vba

Hi to all...
I am currently using Excel 2003. I am trying to use vlookup in VBA. First I
have a workbook with 8 spreadsheets. I have a Master Parts List spreadsheet
that updates 7 other worksheets within the workbook with the Vlookup in each
cell looking back at the Master Parts List spreadsheet. I would like to
accomplish this in VBA when I go to any one of the other spread sheets in
column A active cell value I type it looks up part number, description, and
unit of measure. I need the value in column A to remain text because I have
both numbers and alpha characters as part numbers. Here is what I have and I
keep getting #N/A.

Sub Lookup()
Dim lookuprng As Range
Dim myVal As String 'or whatever
Set lookuprng = Worksheets("Master Parts List").Range("$A$8:$D$5000")
myVal = ActiveCell.Value
ActiveCell.Offset(0, 1) = Application.VLookup(myVal, lookuprng, 2, False)
If myVal = "" Then
ActiveCell.Offset(0, 1) = ""
End If
End Sub

Thanks for any help I can get.


  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,520
Default vlookup text in vba

Try

Dim myVal As Variant

If this post helps click Yes
---------------
Jacob Skaria


"John" wrote:

Hi to all...
I am currently using Excel 2003. I am trying to use vlookup in VBA. First I
have a workbook with 8 spreadsheets. I have a Master Parts List spreadsheet
that updates 7 other worksheets within the workbook with the Vlookup in each
cell looking back at the Master Parts List spreadsheet. I would like to
accomplish this in VBA when I go to any one of the other spread sheets in
column A active cell value I type it looks up part number, description, and
unit of measure. I need the value in column A to remain text because I have
both numbers and alpha characters as part numbers. Here is what I have and I
keep getting #N/A.

Sub Lookup()
Dim lookuprng As Range
Dim myVal As String 'or whatever
Set lookuprng = Worksheets("Master Parts List").Range("$A$8:$D$5000")
myVal = ActiveCell.Value
ActiveCell.Offset(0, 1) = Application.VLookup(myVal, lookuprng, 2, False)
If myVal = "" Then
ActiveCell.Offset(0, 1) = ""
End If
End Sub

Thanks for any help I can get.


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10,124
Default vlookup text in vba

I tested yours and it worked fine. You may have leading or trailing spaces
that need trimming.

Sub Lookups()
Dim lookuprng As Range
Dim myVal As String 'or whatever
Set lookuprng = Worksheets("Master Parts List").Range("A8:D5000")
With ActiveCell
If .Value < "" Then
.Offset(, 1) = Application.VLookup(Trim(.Value), lookuprng, 3, 0)
End If
End With
End Sub
--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"John" wrote in message
...
Hi to all...
I am currently using Excel 2003. I am trying to use vlookup in VBA. First
I
have a workbook with 8 spreadsheets. I have a Master Parts List
spreadsheet
that updates 7 other worksheets within the workbook with the Vlookup in
each
cell looking back at the Master Parts List spreadsheet. I would like to
accomplish this in VBA when I go to any one of the other spread sheets in
column A active cell value I type it looks up part number, description,
and
unit of measure. I need the value in column A to remain text because I
have
both numbers and alpha characters as part numbers. Here is what I have
and I
keep getting #N/A.

Sub Lookup()
Dim lookuprng As Range
Dim myVal As String 'or whatever
Set lookuprng = Worksheets("Master Parts List").Range("$A$8:$D$5000")
myVal = ActiveCell.Value
ActiveCell.Offset(0, 1) = Application.VLookup(myVal, lookuprng, 2,
False)
If myVal = "" Then
ActiveCell.Offset(0, 1) = ""
End If
End Sub

Thanks for any help I can get.



  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default vlookup text in vba

Is the first column of the table also text? (Not just formatted as text, but
really text???)

Just changing the format from number to text (or text to number) isn't enough to
change the underlying values.

You can either fix the data so that it's consistent (all text or all numbers) or
you can use =vlookup() twice.

I used clng() to change text numbers to a long integer. If your part numbers
are decimals, you'll want to use cdbl() instead of clng().

Personally, this kind of lookup scares me. I'd invest the time and fix the data
so that it's consistent.

Option Explicit
Sub myLookup()
Dim lookuprng As Range
Dim myVal As Variant
Dim res As Variant

Set lookuprng = Worksheets("Master Parts List").Range("$A$8:$D$5000")

myVal = ActiveCell.Value

If myVal = "" Then
res = ""
Else
'look for a match as a string
res = Application.VLookup(myVal & "", lookuprng, 2, False)
If IsError(res) Then
'look for a match as a number
If IsNumeric(myVal) Then
res = Application.VLookup(CLng(myVal), lookuprng, 2, False)
If IsError(res) Then
'it wasn't found twice!
res = "Not found" ' "" when you're done testing
End If
End If
End If
End If

ActiveCell.Offset(0, 1) = res

End Sub

I wouldn't use a name that is also a function name in excel.






John wrote:

Hi to all...
I am currently using Excel 2003. I am trying to use vlookup in VBA. First I
have a workbook with 8 spreadsheets. I have a Master Parts List spreadsheet
that updates 7 other worksheets within the workbook with the Vlookup in each
cell looking back at the Master Parts List spreadsheet. I would like to
accomplish this in VBA when I go to any one of the other spread sheets in
column A active cell value I type it looks up part number, description, and
unit of measure. I need the value in column A to remain text because I have
both numbers and alpha characters as part numbers. Here is what I have and I
keep getting #N/A.

Sub Lookup()
Dim lookuprng As Range
Dim myVal As String 'or whatever
Set lookuprng = Worksheets("Master Parts List").Range("$A$8:$D$5000")
myVal = ActiveCell.Value
ActiveCell.Offset(0, 1) = Application.VLookup(myVal, lookuprng, 2, False)
If myVal = "" Then
ActiveCell.Offset(0, 1) = ""
End If
End Sub

Thanks for any help I can get.


--

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
look for text not using vlookup and sum if Fanny Excel Discussion (Misc queries) 6 March 13th 09 10:18 AM
VLOOKUP (with text) Woody13 Excel Discussion (Misc queries) 3 January 24th 06 07:20 PM
Using VLOOKUP and text Tara C. Excel Discussion (Misc queries) 1 December 17th 05 04:40 PM
Vlookup using text gersumabat Excel Worksheet Functions 2 November 1st 05 09:18 PM
Vlookup a value and text raboone Excel Discussion (Misc queries) 1 June 8th 05 08:04 PM


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

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

About Us

"It's about Microsoft Excel"