Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Vlookup Error handling

I am using the following to get information from Sheet1 to Sheet2.

Sub Getdata()
msg = "Do you want to filter the data now?"
Style = vbYesNo + vbDefaultButton1 + vbQuestion
Title = "Filter"
response = MsgBox(msg, Style, Title)
If response = vbYes Then
Application.ScreenUpdating = False
With Worksheets("Sheet2")
Set rng = .Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp))
End With
With Worksheets("Sheet1")
Set rng1 = .Range(.Cells(2, 3), .Cells(Rows.Count, 3).End(xlUp))
End With
For Each cell In rng
cell.Offset(0, 2).Value = Application.VLookup(cell.Value, rng1.Resize(, 10),
7, 0)
cell.Offset(0, 3).Value = Application.VLookup(cell.Value, rng1.Resize(, 10),
8, 0)
cell.Offset(0, 4).Value = Application.VLookup(cell.Value, rng1.Resize(, 10),
9, 0)
Next cell
Application.ScreenUpdating = True
End If
End Sub

On some occasions values in rng are not found in rng1 so #N/A is returned.
On these occasions I would, if possible, like to draw the users attention to
these values. I would like to have a message box listing the values, or if
this is too much, a simple message saying that certain values were not found
(perhaps these values could then be highlighted). I would prefer to do this
with conditional formatting.

Thanks in advance.

Gareth



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default Vlookup Error handling

On Error Resume Next
For Each cell In rng
cell.Offset(0, 2).Value = Application.VLookup(cell.Value, rng1.Resize(,

10),
7, 0)

If err.number <0 then
cell.Offset(0, 2).Value ="# " & Cell.Value
End If
cell.Offset(0, 3).Value = Application.VLookup(cell.Value, rng1.Resize(,

10),
8, 0)


ditto the If / End if

cell.Offset(0, 4).Value = Application.VLookup(cell.Value, rng1.Resize(,

10),
9, 0)


ditto

Next cell



In the above the offset cell value is the look up info preceded by a "#"
ypu can set te sheet's conditional formatting to test if a cell's value
starts with "#" to say color the cell red.

HTH


--
Patrick Molloy
Microsoft Excel MVP
----------------------------------
"Gareth" wrote in message
...
I am using the following to get information from Sheet1 to Sheet2.

Sub Getdata()
msg = "Do you want to filter the data now?"
Style = vbYesNo + vbDefaultButton1 + vbQuestion
Title = "Filter"
response = MsgBox(msg, Style, Title)
If response = vbYes Then
Application.ScreenUpdating = False
With Worksheets("Sheet2")
Set rng = .Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp))
End With
With Worksheets("Sheet1")
Set rng1 = .Range(.Cells(2, 3), .Cells(Rows.Count, 3).End(xlUp))
End With
For Each cell In rng
cell.Offset(0, 2).Value = Application.VLookup(cell.Value, rng1.Resize(,

10),
7, 0)
cell.Offset(0, 3).Value = Application.VLookup(cell.Value, rng1.Resize(,

10),
8, 0)
cell.Offset(0, 4).Value = Application.VLookup(cell.Value, rng1.Resize(,

10),
9, 0)
Next cell
Application.ScreenUpdating = True
End If
End Sub

On some occasions values in rng are not found in rng1 so #N/A is returned.
On these occasions I would, if possible, like to draw the users attention

to
these values. I would like to have a message box listing the values, or

if
this is too much, a simple message saying that certain values were not

found
(perhaps these values could then be highlighted). I would prefer to do

this
with conditional formatting.

Thanks in advance.

Gareth





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
Error Handling Kevin Excel Discussion (Misc queries) 4 June 19th 08 12:31 AM
Error handling VLOOKUP. #N/A - can it be 0 to allow sumation. Richhall Excel Worksheet Functions 3 October 8th 07 12:31 PM
Handling #NUM! error Michel Khennafi Excel Worksheet Functions 1 February 26th 07 08:49 PM
Error Handling Rob Bovey Excel Programming 0 August 7th 03 12:11 AM
Error Handling Bill Lunney Excel Programming 0 August 6th 03 11:56 PM


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