Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Please Help - Vlookup in a macro.


I cannot figure out what I'm doing wrong. I get a "type mismatch" error
on the vlookup line.

Sub AutoComment()

Dim abvar As String
Dim mycell As Range

For Each mycell In Range("B4:H27")

abvar = Application.VLookup(mycell.Value, "SkillLegend!A:B", 2,
False)

mycell.AddComment Text:=abvar

Next mycell

End Sub

Thank you...


--
AllenWatts
------------------------------------------------------------------------
AllenWatts's Profile: http://www.excelforum.com/member.php...o&userid=34301
View this thread: http://www.excelforum.com/showthread...hreadid=541045

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Please Help - Vlookup in a macro.

If the =vlookup() would return the #n/a, your code will have trouble.

Option Explicit
Sub AutoComment()

Dim abvar As Variant 'in case it's an error
Dim mycell As Range
For Each mycell In Range("B4:H27")
abvar = Application.VLookup(mycell.Value, _
worksheets("SkillLegend").range("A:B"), 2, False)

if iserror(abvar) then
'skip it?
else
if mycell.comment is nothing then
'no existing comment
else
mycell.comment.delete
end if
mycell.AddComment Text:=abvar
end if

Next mycell

End Sub

AllenWatts wrote:

I cannot figure out what I'm doing wrong. I get a "type mismatch" error
on the vlookup line.

Sub AutoComment()

Dim abvar As String
Dim mycell As Range

For Each mycell In Range("B4:H27")

abvar = Application.VLookup(mycell.Value, "SkillLegend!A:B", 2,
False)

mycell.AddComment Text:=abvar

Next mycell

End Sub

Thank you...

--
AllenWatts
------------------------------------------------------------------------
AllenWatts's Profile: http://www.excelforum.com/member.php...o&userid=34301
View this thread: http://www.excelforum.com/showthread...hreadid=541045


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 789
Default Please Help - Vlookup in a macro.

Hi
Try this

Sub AutoComment()


Dim abvar As String
Dim mycell As Range

For Each mycell In Range("B4:H27")
If mycell.Value < "" Then
abvar = Application.VLookup(mycell.Value,
Range("SkillLegend!A1:B10"), 2, False)
mycell.AddComment Text:=abvar
End If
Next mycell

End Sub

VLookUp doen't like to search for an empty string. Just to be safe you
might use
abvar = Cstr(Application.VLookup(mycell.Value,
Range("SkillLegend!A1:B10"), 2, False))

to force abvar to be a character string.

regards
Paul

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Please Help - Vlookup in a macro.


Thank you both!


--
AllenWatts
------------------------------------------------------------------------
AllenWatts's Profile: http://www.excelforum.com/member.php...o&userid=34301
View this thread: http://www.excelforum.com/showthread...hreadid=541045

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 Macro? blucajun Excel Worksheet Functions 0 June 27th 08 10:23 PM
vlookup macro MikeD1224 New Users to Excel 1 June 16th 07 04:37 AM
VLOOKUP in a macro?? chip_pyp Excel Programming 4 March 27th 06 09:46 PM
VLOOKUP in a Macro Peter Hesselager Excel Programming 5 May 7th 05 01:28 AM
Need a macro to do what this VLOOKUP does Tim Excel Programming 5 February 12th 04 04:41 PM


All times are GMT +1. The time now is 05:11 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"