Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 230
Default Find - search in upward direction

Is there a way to modify the Find section of my code (below) so that it will
look for the value starting from the bottom of the range.
There may be more than one occurrence of the InvNum and I want it to find
the last occurrence.
(Maybe there's an alternative if modifying the Find code doesn't do it?)

Dim InvName
Dim InvDate
Dim InvNum
Dim InvAmount
'The values in these ranges are determined by the Invoice that's opened.
Set InvName = Range("D13")
Set InvDate = Range("I4")
Set InvNum = Range("I2")
Set InvAmount = Range("J48")

Windows("Records (CURRENT YEAR) Modifying.xls").Activate
With Sheets("Tax Invoice Records").Range("B21:B1000")
.Find(InvNum, LookIn:=xlValues).Select
ActiveCell.Offset(0, 1) = InvDate
ActiveCell.Offset(0, 2) = InvName
ActiveCell.Offset(0, 3) = InvAmount
End With

Rob


  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,501
Default Find - search in upward direction

Rob,

To search in reverse you could use a for next loop:

For x = 1000 To 21 Step -1
Cells(x, 2).Select '< Starts at B1000
ActiveCell.Offset(0, 1) = InvDate
ActiveCell.Offset(0, 2) = InvName
ActiveCell.Offset(0, 3) = InvAmount
Next

This starts in B1000 and each pass rounf the loop x reduces by 1.

Mike

"RobN" wrote:

Is there a way to modify the Find section of my code (below) so that it will
look for the value starting from the bottom of the range.
There may be more than one occurrence of the InvNum and I want it to find
the last occurrence.
(Maybe there's an alternative if modifying the Find code doesn't do it?)

Dim InvName
Dim InvDate
Dim InvNum
Dim InvAmount
'The values in these ranges are determined by the Invoice that's opened.
Set InvName = Range("D13")
Set InvDate = Range("I4")
Set InvNum = Range("I2")
Set InvAmount = Range("J48")

Windows("Records (CURRENT YEAR) Modifying.xls").Activate
With Sheets("Tax Invoice Records").Range("B21:B1000")
.Find(InvNum, LookIn:=xlValues).Select
ActiveCell.Offset(0, 1) = InvDate
ActiveCell.Offset(0, 2) = InvName
ActiveCell.Offset(0, 3) = InvAmount
End With

Rob



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 230
Default Find - search in upward direction

Thanks Mike, but I'm a bit of a dunce with loops.

I can't quite figure out how this loop is going to activate the cell that
matches the InvNum variable, so that it can use the ActiveCell.Offsets to
paste the appropriate values.

In summary, I need the code to find a Cell in column B that matches the
InvNum variable.
Then, when found, it needs to put the values from the other variables into
the adjacent cells in that row as determined by the Offset rules. (And it
needs to find relevant cell going upwards - or at least the last incidence
of that InvNum variable value.)

Rob

"Mike H" wrote in message
...
Rob,

To search in reverse you could use a for next loop:

For x = 1000 To 21 Step -1
Cells(x, 2).Select '< Starts at B1000
ActiveCell.Offset(0, 1) = InvDate
ActiveCell.Offset(0, 2) = InvName
ActiveCell.Offset(0, 3) = InvAmount
Next

This starts in B1000 and each pass rounf the loop x reduces by 1.

Mike

"RobN" wrote:

Is there a way to modify the Find section of my code (below) so that it
will
look for the value starting from the bottom of the range.
There may be more than one occurrence of the InvNum and I want it to find
the last occurrence.
(Maybe there's an alternative if modifying the Find code doesn't do it?)

Dim InvName
Dim InvDate
Dim InvNum
Dim InvAmount
'The values in these ranges are determined by the Invoice that's opened.
Set InvName = Range("D13")
Set InvDate = Range("I4")
Set InvNum = Range("I2")
Set InvAmount = Range("J48")

Windows("Records (CURRENT YEAR) Modifying.xls").Activate
With Sheets("Tax Invoice Records").Range("B21:B1000")
.Find(InvNum, LookIn:=xlValues).Select
ActiveCell.Offset(0, 1) = InvDate
ActiveCell.Offset(0, 2) = InvName
ActiveCell.Offset(0, 3) = InvAmount
End With

Rob





  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Find - search in upward direction

First, some comments.

Instead of activating the window and depending on the correct worksheet being
the active worksheet, you can work on that worksheet directly. And I wouldn't
go through the windows collection. If the user window|new window, you may not
find a window with that name. I'd go through the workbooks collection. It's
safer.

And it's always better to specify all the parameters in the .find statement. If
you don't, then excel will use the settings used by the last .find. And that
can by by the user or by any code that the user ran.

And you can pass a parm to the .find statement to look upward (xlprevious). And
if you start at the top and look up, you're looking for the last value in the
range.

This may help you:

Option Explicit
Sub testme()

Dim wks As Worksheet
Dim FoundCell As Range
Dim InvNum As String 'long????
Dim InvDate As Date
Dim InvName As String
Dim InvAmount As Double

'change this for your worksheet name.
Set wks = Workbooks("Records (CURRENT YEAR) Modifying.xls") _
.Worksheets("sheet9999")

'testdata
InvNum = "asdf0"
InvDate = Date
InvName = "hi there"
InvAmount = 9999.99

With wks
'.Select 'you don't need this here
With .Range("b21:b1000")
Set FoundCell = .Cells.Find(what:=InvNum, _
after:=.Cells(1), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlPrevious, _
MatchCase:=False)
If FoundCell Is Nothing Then
MsgBox "not found"
Else
FoundCell.Offset(0, 1).Value = InvDate
FoundCell.Offset(0, 2).Value = InvName
FoundCell.Offset(0, 3).Value = InvAmount
End If
End With
End With

End Sub

And if I wanted to find the first in that range, I'd start at the bottom and
look for the next (xlnext).

Set FoundCell = .Cells.Find(what:=InvNum, _
after:=.Cells(.cells.count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)


RobN wrote:

Is there a way to modify the Find section of my code (below) so that it will
look for the value starting from the bottom of the range.
There may be more than one occurrence of the InvNum and I want it to find
the last occurrence.
(Maybe there's an alternative if modifying the Find code doesn't do it?)

Dim InvName
Dim InvDate
Dim InvNum
Dim InvAmount
'The values in these ranges are determined by the Invoice that's opened.
Set InvName = Range("D13")
Set InvDate = Range("I4")
Set InvNum = Range("I2")
Set InvAmount = Range("J48")

Windows("Records (CURRENT YEAR) Modifying.xls").Activate
With Sheets("Tax Invoice Records").Range("B21:B1000")
.Find(InvNum, LookIn:=xlValues).Select
ActiveCell.Offset(0, 1) = InvDate
ActiveCell.Offset(0, 2) = InvName
ActiveCell.Offset(0, 3) = InvAmount
End With

Rob


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 230
Default Find - search in upward direction

Thanks Dave,

I appreciate your comments. Very helpful, thanks. (I never thought about the
possible problems if all parameters weren't specified. I thought I was
being smart by deleting what I thought wasn't necessary.)

Your code works great and I should be able to fit it in with the rest of my
code.

Rob

"Dave Peterson" wrote in message
...
First, some comments.

Instead of activating the window and depending on the correct worksheet
being
the active worksheet, you can work on that worksheet directly. And I
wouldn't
go through the windows collection. If the user window|new window, you may
not
find a window with that name. I'd go through the workbooks collection.
It's
safer.

And it's always better to specify all the parameters in the .find
statement. If
you don't, then excel will use the settings used by the last .find. And
that
can by by the user or by any code that the user ran.

And you can pass a parm to the .find statement to look upward
(xlprevious). And
if you start at the top and look up, you're looking for the last value in
the
range.

This may help you:

Option Explicit
Sub testme()

Dim wks As Worksheet
Dim FoundCell As Range
Dim InvNum As String 'long????
Dim InvDate As Date
Dim InvName As String
Dim InvAmount As Double

'change this for your worksheet name.
Set wks = Workbooks("Records (CURRENT YEAR) Modifying.xls") _
.Worksheets("sheet9999")

'testdata
InvNum = "asdf0"
InvDate = Date
InvName = "hi there"
InvAmount = 9999.99

With wks
'.Select 'you don't need this here
With .Range("b21:b1000")
Set FoundCell = .Cells.Find(what:=InvNum, _
after:=.Cells(1), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlPrevious, _
MatchCase:=False)
If FoundCell Is Nothing Then
MsgBox "not found"
Else
FoundCell.Offset(0, 1).Value = InvDate
FoundCell.Offset(0, 2).Value = InvName
FoundCell.Offset(0, 3).Value = InvAmount
End If
End With
End With

End Sub

And if I wanted to find the first in that range, I'd start at the bottom
and
look for the next (xlnext).

Set FoundCell = .Cells.Find(what:=InvNum, _
after:=.Cells(.cells.count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)


RobN wrote:

Is there a way to modify the Find section of my code (below) so that it
will
look for the value starting from the bottom of the range.
There may be more than one occurrence of the InvNum and I want it to find
the last occurrence.
(Maybe there's an alternative if modifying the Find code doesn't do it?)

Dim InvName
Dim InvDate
Dim InvNum
Dim InvAmount
'The values in these ranges are determined by the Invoice that's opened.
Set InvName = Range("D13")
Set InvDate = Range("I4")
Set InvNum = Range("I2")
Set InvAmount = Range("J48")

Windows("Records (CURRENT YEAR) Modifying.xls").Activate
With Sheets("Tax Invoice Records").Range("B21:B1000")
.Find(InvNum, LookIn:=xlValues).Select
ActiveCell.Offset(0, 1) = InvDate
ActiveCell.Offset(0, 2) = InvName
ActiveCell.Offset(0, 3) = InvAmount
End With

Rob


--

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
Couldnt find in search brownti via OfficeKB.com Excel Discussion (Misc queries) 2 February 9th 07 02:56 PM
search a row to find the column Stephen Excel Worksheet Functions 2 March 23rd 05 01:51 AM
Inserting upward pointing green triangle David Mok Excel Worksheet Functions 2 March 7th 05 05:37 PM
Can Search find 2 or more "/"? Wind54Surfer Excel Discussion (Misc queries) 2 February 22nd 05 04:31 PM
How do I use "find" to search whole workbook? Sholtzy Excel Discussion (Misc queries) 2 January 14th 05 12:51 AM


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