Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default very confusing

Hi

I have a module in a spreadsheet which executes a piece of code, see below

Set wRep = Worksheets("Report to Region")
wRep.Activate
Set R = wRep.Range("A20:A46")
R.Activate
enc = R.Find(What:=sIprojNo, After:=R.Cells(1, 1), LookIn:=xlValues,
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False, SearchFormat:=False).Activate
If enc = False Then
nRows = WorksheetFunction.CountA(R) + 1
R.Cells(nRows, 1) = sIprojNo
R.Cells(nRows, 2).Select
End If

Which works perfectly, later in the same spreadsheet, in a user form, on a
button to save the data that may be captures, I need to do the same data
check and paste the relevant field, if needed to the end of the list so I
copies and pasted the code andit doesnot work, see below

Set wRep = Worksheets("Report to Region")
wRep.Activate
Set R = wRep.Range("A20:A46")
R.Activate
enc = R.Find(What:=sIprojNo, After:=R.Cells(1, 1), LookIn:=xlValues,
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False, SearchFormat:=False).Activate
If enc = False Then
nRows = WorksheetFunction.CountA(R) + 1
R.Cells(nRows, 1) = sIprojNo
R.Cells(nRows, 2).Select
End If

This same code now returns a Run Time Error '91':

Object Variable or With block variable not set

I use Option Explicit so it cant be a variable, what else could it be, any
ideas or suggestions are most welcome.

in both instances, the variables are defined as follows

Dim sIprojNo As String
Dim wRep As Worksheet
Dim R As Range
Dim enc As Boolean
Dim nRows As Integer


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default very confusing

Remove all the Activate and Select. Cano only perform those commands if it is
the active sheet.

Note that a space and underscore at the end of a line is a line break in an
otherwise single line of code.

Setting the find to a range variable allows you to get rid of the select at
the end of the Find function.

After:=R.Cells(R.Cells.Count) forces find to start looking in first cell.
After:=R.Cells(1, 1) actually is just that; After the first cell and find
starts looking from second cell and does not find the first cell until after
it has looked at the last cell.

Sub TestFind()
Dim wRep As Worksheet
Dim R As Range
Dim enc As Range
Dim sIprojNo As Long
Dim nRows As Long

sIprojNo = 54321 'Dummy value for test

Set wRep = Worksheets("Report to Region")

With wRep
Set R = .Range("A20:A46")
End With

Set enc = R.Find(What:=sIprojNo, _
After:=R.Cells(R.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not enc Is Nothing Then
'sIprojNo found
Else
'sIprojNo NOT found
'Assume this code is for NOT found
nRows = WorksheetFunction.CountA(R) + 1
R.Cells(nRows, 1) = sIprojNo
End If

End Sub

--
Regards,

OssieMac


"Ihar" wrote:

Hi

I have a module in a spreadsheet which executes a piece of code, see below

Set wRep = Worksheets("Report to Region")
wRep.Activate
Set R = wRep.Range("A20:A46")
R.Activate
enc = R.Find(What:=sIprojNo, After:=R.Cells(1, 1), LookIn:=xlValues,
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False, SearchFormat:=False).Activate
If enc = False Then
nRows = WorksheetFunction.CountA(R) + 1
R.Cells(nRows, 1) = sIprojNo
R.Cells(nRows, 2).Select
End If

Which works perfectly, later in the same spreadsheet, in a user form, on a
button to save the data that may be captures, I need to do the same data
check and paste the relevant field, if needed to the end of the list so I
copies and pasted the code andit doesnot work, see below

Set wRep = Worksheets("Report to Region")
wRep.Activate
Set R = wRep.Range("A20:A46")
R.Activate
enc = R.Find(What:=sIprojNo, After:=R.Cells(1, 1), LookIn:=xlValues,
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False, SearchFormat:=False).Activate
If enc = False Then
nRows = WorksheetFunction.CountA(R) + 1
R.Cells(nRows, 1) = sIprojNo
R.Cells(nRows, 2).Select
End If

This same code now returns a Run Time Error '91':

Object Variable or With block variable not set

I use Option Explicit so it cant be a variable, what else could it be, any
ideas or suggestions are most welcome.

in both instances, the variables are defined as follows

Dim sIprojNo As String
Dim wRep As Worksheet
Dim R As Range
Dim enc As Boolean
Dim nRows As Integer


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default very confusing

see if this helps:

Set wRep = Worksheets("Report to Region")

Set R = wRep.Range("A20:A46")

Set enc = R.Find(What:=sIprojNo, After:=R.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

If enc Is Nothing Then
nRows = WorksheetFunction.CountA(R) + 1
R.Cells(nRows, 1) = sIprojNo
R.Cells(nRows, 2).Select
End If
--
jb


"Ihar" wrote:

Hi

I have a module in a spreadsheet which executes a piece of code, see below

Set wRep = Worksheets("Report to Region")
wRep.Activate
Set R = wRep.Range("A20:A46")
R.Activate
enc = R.Find(What:=sIprojNo, After:=R.Cells(1, 1), LookIn:=xlValues,
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False, SearchFormat:=False).Activate
If enc = False Then
nRows = WorksheetFunction.CountA(R) + 1
R.Cells(nRows, 1) = sIprojNo
R.Cells(nRows, 2).Select
End If

Which works perfectly, later in the same spreadsheet, in a user form, on a
button to save the data that may be captures, I need to do the same data
check and paste the relevant field, if needed to the end of the list so I
copies and pasted the code andit doesnot work, see below

Set wRep = Worksheets("Report to Region")
wRep.Activate
Set R = wRep.Range("A20:A46")
R.Activate
enc = R.Find(What:=sIprojNo, After:=R.Cells(1, 1), LookIn:=xlValues,
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False, SearchFormat:=False).Activate
If enc = False Then
nRows = WorksheetFunction.CountA(R) + 1
R.Cells(nRows, 1) = sIprojNo
R.Cells(nRows, 2).Select
End If

This same code now returns a Run Time Error '91':

Object Variable or With block variable not set

I use Option Explicit so it cant be a variable, what else could it be, any
ideas or suggestions are most welcome.

in both instances, the variables are defined as follows

Dim sIprojNo As String
Dim wRep As Worksheet
Dim R As Range
Dim enc As Boolean
Dim nRows As Integer


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default very confusing

Thanks

This worked like a dream


"OssieMac" wrote in message
...
Remove all the Activate and Select. Cano only perform those commands if it
is
the active sheet.

Note that a space and underscore at the end of a line is a line break in
an
otherwise single line of code.

Setting the find to a range variable allows you to get rid of the select
at
the end of the Find function.

After:=R.Cells(R.Cells.Count) forces find to start looking in first cell.
After:=R.Cells(1, 1) actually is just that; After the first cell and find
starts looking from second cell and does not find the first cell until
after
it has looked at the last cell.

Sub TestFind()
Dim wRep As Worksheet
Dim R As Range
Dim enc As Range
Dim sIprojNo As Long
Dim nRows As Long

sIprojNo = 54321 'Dummy value for test

Set wRep = Worksheets("Report to Region")

With wRep
Set R = .Range("A20:A46")
End With

Set enc = R.Find(What:=sIprojNo, _
After:=R.Cells(R.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not enc Is Nothing Then
'sIprojNo found
Else
'sIprojNo NOT found
'Assume this code is for NOT found
nRows = WorksheetFunction.CountA(R) + 1
R.Cells(nRows, 1) = sIprojNo
End If

End Sub

--
Regards,

OssieMac


"Ihar" wrote:

Hi

I have a module in a spreadsheet which executes a piece of code, see
below

Set wRep = Worksheets("Report to Region")
wRep.Activate
Set R = wRep.Range("A20:A46")
R.Activate
enc = R.Find(What:=sIprojNo, After:=R.Cells(1, 1), LookIn:=xlValues,
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False, SearchFormat:=False).Activate
If enc = False Then
nRows = WorksheetFunction.CountA(R) + 1
R.Cells(nRows, 1) = sIprojNo
R.Cells(nRows, 2).Select
End If

Which works perfectly, later in the same spreadsheet, in a user form, on
a
button to save the data that may be captures, I need to do the same data
check and paste the relevant field, if needed to the end of the list so I
copies and pasted the code andit doesnot work, see below

Set wRep = Worksheets("Report to Region")
wRep.Activate
Set R = wRep.Range("A20:A46")
R.Activate
enc = R.Find(What:=sIprojNo, After:=R.Cells(1, 1), LookIn:=xlValues,
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False, SearchFormat:=False).Activate
If enc = False Then
nRows = WorksheetFunction.CountA(R) + 1
R.Cells(nRows, 1) = sIprojNo
R.Cells(nRows, 2).Select
End If

This same code now returns a Run Time Error '91':

Object Variable or With block variable not set

I use Option Explicit so it cant be a variable, what else could it be,
any
ideas or suggestions are most welcome.

in both instances, the variables are defined as follows

Dim sIprojNo As String
Dim wRep As Worksheet
Dim R As Range
Dim enc As Boolean
Dim nRows As Integer



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
Confusing spreadsheet Dan Wood Excel Discussion (Misc queries) 0 May 20th 10 05:09 PM
Confusing results Papa Jonah Excel Worksheet Functions 5 March 5th 10 10:38 PM
Menu confusing marriedhsdad New Users to Excel 3 January 18th 10 04:20 PM
V-/H- LOOKUP still confusing Andreas B Excel Worksheet Functions 3 June 5th 08 02:54 PM
Help please! I'm confusing myself..... Ray Excel Programming 2 June 22nd 07 03:30 PM


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