Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 410
Default On error from Find, GoTo E


I am having trouble debugging this code. I am gettign an error on the
line rngFound =. It seems that when it gets to an i that is not in
Sheet1 in bugs out. Is there a way to skip down to E at this point?

Thanks,
Jay

Sub findLast()
Dim i
Dim lstRow As Long
Dim strResult As String
Dim strResult2 As String
Dim Concat As String
Dim concat2 As String
Dim TargetCell As Range
Dim TargetCell2 As Range
Dim rngFound As String

lstRow = Sheet1.Range("g65536").End(xlUp).Row

For Each i In Sheet1.Range("G4:G" & lstRow)

'Bugs out on this line
rngFound = Sheets("Sheet1").Range("A:A").Find(i.Value, _
LookAt:=xlWhole, LookIn:=xlFormulas, MatchCase:=True).Address
'MsgBox (rngFound)

Dim myC As String
myC = Sheets("Sheet1").Range("A2:A65536").Find(i.Value, , , , , _
xlPrevious).Address
'MsgBox (myC)


Set TargetCell = Sheets("Sheet1").Range(rngFound)
concat2 = ""
Concat = ""
Do
If TargetCell.Row <= Sheets("Sheet1").Range(myC).Row Then
strResult = TargetCell.Offset(0, 1).Value
Set TargetCell = TargetCell.Offset(1, 0)
Else
If TargetCell.Row = Range(myC).Row Then
Concat = TargetCell.Offset(0, 1).Value
Set TargetCell = TargetCell.Offset(1, 0)
End If
End If
Concat = strResult & ", " & Concat
Loop Until TargetCell.Row = Sheets("Sheet1").Range(myC).Row +
1

Set TargetCell2 = Sheets("Sheet1").Range(rngFound)
Do
If TargetCell2.Row <= Sheets("Sheet1").Range(myC).Row Then
strResult2 = TargetCell2.Offset(0, 4).Value
Set TargetCell2 = TargetCell2.Offset(1, 0)
Else
If TargetCell2.Row = Range(myC).Row Then
Concat = TargetCell2.Offset(0, 4).Value
Set TargetCell2 = TargetCell2.Offset(1, 0)
End If
End If
concat2 = strResult2 & ", " & concat2
Loop Until TargetCell2.Row = Sheets("Sheet1").Range(myC).Row +
1
E:
i.Offset(0, 20) = Concat
i.Offset(0, 21) = concat2

Next i
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default On error from Find, GoTo E

Instead of doing both the .find and .address in one step, try to do it in two:

Dim FoundCell as range
set foundcell = Sheets("Sheet1").Range("A:A").Find(i.Value, _
LookAt:=xlWhole, LookIn:=xlFormulas, MatchCase:=True)

'now check to see if you found something
if foundcell is nothing then
msgbox "Not found"
'what should happen?
else
rngfound = foundcell.address
'rest of code
end if

jlclyde wrote:

I am having trouble debugging this code. I am gettign an error on the
line rngFound =. It seems that when it gets to an i that is not in
Sheet1 in bugs out. Is there a way to skip down to E at this point?

Thanks,
Jay

Sub findLast()
Dim i
Dim lstRow As Long
Dim strResult As String
Dim strResult2 As String
Dim Concat As String
Dim concat2 As String
Dim TargetCell As Range
Dim TargetCell2 As Range
Dim rngFound As String

lstRow = Sheet1.Range("g65536").End(xlUp).Row

For Each i In Sheet1.Range("G4:G" & lstRow)

'Bugs out on this line
rngFound = Sheets("Sheet1").Range("A:A").Find(i.Value, _
LookAt:=xlWhole, LookIn:=xlFormulas, MatchCase:=True).Address
'MsgBox (rngFound)

Dim myC As String
myC = Sheets("Sheet1").Range("A2:A65536").Find(i.Value, , , , , _
xlPrevious).Address
'MsgBox (myC)

Set TargetCell = Sheets("Sheet1").Range(rngFound)
concat2 = ""
Concat = ""
Do
If TargetCell.Row <= Sheets("Sheet1").Range(myC).Row Then
strResult = TargetCell.Offset(0, 1).Value
Set TargetCell = TargetCell.Offset(1, 0)
Else
If TargetCell.Row = Range(myC).Row Then
Concat = TargetCell.Offset(0, 1).Value
Set TargetCell = TargetCell.Offset(1, 0)
End If
End If
Concat = strResult & ", " & Concat
Loop Until TargetCell.Row = Sheets("Sheet1").Range(myC).Row +
1

Set TargetCell2 = Sheets("Sheet1").Range(rngFound)
Do
If TargetCell2.Row <= Sheets("Sheet1").Range(myC).Row Then
strResult2 = TargetCell2.Offset(0, 4).Value
Set TargetCell2 = TargetCell2.Offset(1, 0)
Else
If TargetCell2.Row = Range(myC).Row Then
Concat = TargetCell2.Offset(0, 4).Value
Set TargetCell2 = TargetCell2.Offset(1, 0)
End If
End If
concat2 = strResult2 & ", " & concat2
Loop Until TargetCell2.Row = Sheets("Sheet1").Range(myC).Row +
1
E:
i.Offset(0, 20) = Concat
i.Offset(0, 21) = concat2

Next i
End Sub


--

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
On error goto problem Coppercrutch Excel Discussion (Misc queries) 3 December 28th 07 03:04 PM
On Error GoTo Doesn't Work [email protected] Excel Discussion (Misc queries) 2 November 29th 07 09:43 PM
find the last Fred and goto cell Steved Excel Worksheet Functions 6 October 11th 07 06:53 PM
On Error GoTo skip needs help dan Excel Discussion (Misc queries) 2 April 24th 06 06:29 PM
Goto Data validation Same ERROR Kate Excel Discussion (Misc queries) 10 March 17th 05 03:31 AM


All times are GMT +1. The time now is 10:41 AM.

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"