Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 22nd, 2009, 10:17 AM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation .Find problem - Help

Can anyone help me with why I'm getting an "Object variable or With block variable not set (Error 91)" error on the highlighted code below?? Its probably something simple that I'm missing ... just cant see it ... Thanks!!


Code:
 
Sub test1()
'test1 macro
Dim Data As Range
Dim cRange, pRange, R, Oppty As Range
Dim cLastRow, pLastRow, x, OpptyCol As Integer
'Set range for Current data
Set cRange = Worksheets("current").Range("A1").specialcells(xlCellTypeLastCell)
'Set range for previous data
Set pRange = Worksheets("previous").Range("A1").specialcells(xlCellTypeLastCell)
'Find last rows
cLastRow = cRange.Row
pLastRow = pRange.Row
    
'Find Forecasted column
Set R = Worksheets("previous").Range("A1:AZ1").Find(What:="Forecasted?", LookAt:=xlPart)
'Find Oppty # column
Set Oppty = Worksheets("previous").Range("A1:AZ1").Find(What:="Oppty #", LookAt:=xlPart)
'Set column #
OpptyCol = Oppty.Column
For x = 2 To pLastRow
'look in current worksheet in range a1:a1000
With Worksheets("current").Range("a1:a1000")
'for each cell in the Oppty # column - previous worksheet - to return the value in the 5th column
    vName = .Find(What:=Cells(x, OpptyCol), after:=.Cells(1, 1), _
            LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False).Offset(0, 5)
End With
'enter value in following cell
    R.Offset((x - 1), 1) = vName
Next x
End Sub
 
Old January 23rd, 2009, 02:30 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

It might be because of absence of required text and Find doesn't return anything

You can check that by :

If Not R Is Nothing Then
Dim vName As Range
For x = 2 To pLastRow
'look in current worksheet in range a1:a1000
With Worksheets("current").Range("a1:a1000")
'for each cell in the Oppty # column - previous worksheet - to return the value in the 5th column
Set vName = .Find(What:=Cells(x, OpptyCol), After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If Not vName Is Nothing Then
vName = vName.Offset(0, 5)
End If
End With
'enter value in following cell
If Not vName Is Nothing Then
R.Offset((x - 1), 1) = vName
End If
Next x
End If

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Range.Find and CountA crugg1 Excel VBA 3 January 15th, 2009 10:34 AM
Problem to find Column Length in vb.net sachinbashetti VB Databases Basics 0 September 20th, 2005 07:29 AM
Open Word Doc from Access - find, find next save donaldmaloney Access VBA 1 May 25th, 2005 11:09 AM
Create a find and a find and replace in VB.NET snowy0 VB How-To 0 January 26th, 2004 07:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.