#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 213
Default Range help

If the Dim "sUserPart" has no match in column A, how do I capture that? I
would like to prompt the user with a MsgBox.
Everything I have come up with itterates thru all the cells.

'======
With Sheets("Part Number")
Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh1Range = .Range("A1:A" & Sh1LastRow)
End With
For Each Sh1Cell In Sh1Range
If Sh1Cell.Value = sUserPart Then
MsgBox "do some stuff here"
Else
MsgBox "Not found!" 'loops thru all cells in range?
End If
'======
--
Regards

VBA.Noob.Confused
XP Pro
Office 2007

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Range help

with your code just add another variable found. Also you should add an exit
for to speed up the code.

Found = false
With Sheets("Part Number")
Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh1Range = .Range("A1:A" & Sh1LastRow)
End With
For Each Sh1Cell In Sh1Range
If Sh1Cell.Value = sUserPart Then
MsgBox "do some stuff here"
found = true
exit for
Else
MsgBox "Not found!" 'loops thru all cells in range?
End If


Another way of doing the same thing
set c = columns("A:A").find(what:=sUserPart,lookin:=xlvalu es,lookat:xlwhole)
if not c is nothing then
MsgBox "do some stuff here"
Else
MsgBox "Not found!" 'loops thru all cells in range?
End If


"Rick S." wrote:

If the Dim "sUserPart" has no match in column A, how do I capture that? I
would like to prompt the user with a MsgBox.
Everything I have come up with itterates thru all the cells.

'======
With Sheets("Part Number")
Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh1Range = .Range("A1:A" & Sh1LastRow)
End With
For Each Sh1Cell In Sh1Range
If Sh1Cell.Value = sUserPart Then
MsgBox "do some stuff here"
Else
MsgBox "Not found!" 'loops thru all cells in range?
End If
'======
--
Regards

VBA.Noob.Confused
XP Pro
Office 2007

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 213
Default Range help

"found = true"
GENIOUS! ;)

It is uncanny how these things can be resolved with such simplicity,
unfotunately for me my complex brain can't do simple. LOL
--
Regards

VBA.Noob.Confused
XP Pro
Office 2007



"Joel" wrote:

with your code just add another variable found. Also you should add an exit
for to speed up the code.

Found = false
With Sheets("Part Number")
Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh1Range = .Range("A1:A" & Sh1LastRow)
End With
For Each Sh1Cell In Sh1Range
If Sh1Cell.Value = sUserPart Then
MsgBox "do some stuff here"
found = true
exit for
Else
MsgBox "Not found!" 'loops thru all cells in range?
End If


Another way of doing the same thing
set c = columns("A:A").find(what:=sUserPart,lookin:=xlvalu es,lookat:xlwhole)
if not c is nothing then
MsgBox "do some stuff here"
Else
MsgBox "Not found!" 'loops thru all cells in range?
End If


"Rick S." wrote:

If the Dim "sUserPart" has no match in column A, how do I capture that? I
would like to prompt the user with a MsgBox.
Everything I have come up with itterates thru all the cells.

'======
With Sheets("Part Number")
Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh1Range = .Range("A1:A" & Sh1LastRow)
End With
For Each Sh1Cell In Sh1Range
If Sh1Cell.Value = sUserPart Then
MsgBox "do some stuff here"
Else
MsgBox "Not found!" 'loops thru all cells in range?
End If
'======
--
Regards

VBA.Noob.Confused
XP Pro
Office 2007

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Range help

You could use =match() in a worksheet cell to look for a match.

In code:

Dim Res as Variant
dim RngToCheck as range
dim myVal as String ' or variant or long or ...

with worksheets("sheet9999")
set rngtocheck = .range("A:a")
end with

myval = "somepartnumber"

res = application.match(myval, rngtocheck, 0)

if iserror(res) then
'no match
else
'found a match
end if



Rick S. wrote:

If the Dim "sUserPart" has no match in column A, how do I capture that? I
would like to prompt the user with a MsgBox.
Everything I have come up with itterates thru all the cells.

'======
With Sheets("Part Number")
Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh1Range = .Range("A1:A" & Sh1LastRow)
End With
For Each Sh1Cell In Sh1Range
If Sh1Cell.Value = sUserPart Then
MsgBox "do some stuff here"
Else
MsgBox "Not found!" 'loops thru all cells in range?
End If
'======
--
Regards

VBA.Noob.Confused
XP Pro
Office 2007


--

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
How do I enter formula sum(range+range)*0.15 sumif(range=3) tkw Excel Discussion (Misc queries) 2 October 1st 09 09:17 PM
Excel Addin:Setting the range to the Excel.Range object range prop Rp007 Excel Worksheet Functions 5 November 24th 06 04:30 PM
Range Question / error 1004: method Range of object Worksheet has failed Paul Excel Programming 3 April 7th 05 02:56 PM
Range.Find returns cell outside of range when range set to single cell Frank Jones Excel Programming 12 June 10th 04 04:22 AM
how to? set my range= my UDF argument (range vs. value in range) [advanced?] Keith R[_3_] Excel Programming 2 August 11th 03 05:55 PM


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