Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Problem with Find filling a listbox vba

Hi,

I've got a simple problem (I think)

I would like to search within a selection using the find method. Then
transfer the found cells to a listbox.

The problem is that the code I'm using loops a "few times to many".
Does anyone know what's wrong with it?

Here's the code

Private Sub CommandButton2_Click()

Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("sheet1")

sh.UsedRange.Activate
cntend = LastRow(sh)


Selection.Find(What:="cash", After:=ActiveCell, LookIn:=xlValues,
Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Select

counter = 1
Do While counter < cntend

Cells.FindNext(After:=ActiveCell).Activate
ListBox1.AddItem ActiveCell.Value
counter = counter + 1

Loop

End Sub


Help Appreciated,

Farmer

  #2   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default Problem with Find filling a listbox vba

hi,
instead of using "cntend = LastRow(sh)"
try this instead...
lastrow = cells(rows.count,1).End(xlup).row

-----Original Message-----
Hi,

I've got a simple problem (I think)

I would like to search within a selection using the find

method. Then
transfer the found cells to a listbox.

The problem is that the code I'm using loops a "few times

to many".
Does anyone know what's wrong with it?

Here's the code

Private Sub CommandButton2_Click()

Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("sheet1")

sh.UsedRange.Activate
cntend = LastRow(sh)


Selection.Find(What:="cash", After:=ActiveCell,

LookIn:=xlValues,
Lookat:=xlPart, SearchOrder:=xlByRows,

SearchDirection:=xlNext,
MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Select

counter = 1
Do While counter < cntend

Cells.FindNext(After:=ActiveCell).Activate
ListBox1.AddItem ActiveCell.Value
counter = counter + 1

Loop

End Sub


Help Appreciated,

Farmer

.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Problem with Find filling a listbox vba

On Mon, 11 Oct 2004 08:52:18 -0700,
wrote:

Hi,

Thanks for your reply but it's still not working



Farmer





hi,
instead of using "cntend = LastRow(sh)"
try this instead...
lastrow = cells(rows.count,1).End(xlup).row

-----Original Message-----
Hi,

I've got a simple problem (I think)

I would like to search within a selection using the find

method. Then
transfer the found cells to a listbox.

The problem is that the code I'm using loops a "few times

to many".
Does anyone know what's wrong with it?

Here's the code

Private Sub CommandButton2_Click()

Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("sheet1")

sh.UsedRange.Activate
cntend = LastRow(sh)


Selection.Find(What:="cash", After:=ActiveCell,

LookIn:=xlValues,
Lookat:=xlPart, SearchOrder:=xlByRows,

SearchDirection:=xlNext,
MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Select

counter = 1
Do While counter < cntend

Cells.FindNext(After:=ActiveCell).Activate
ListBox1.AddItem ActiveCell.Value
counter = counter + 1

Loop

End Sub


Help Appreciated,

Farmer

.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Problem with Find filling a listbox vba

There's a nice example in VBA's help for .find that shows how to search until
you loop around to the first found cell.

Option Explicit

Private Sub CommandButton2_Click()

Dim sh As Worksheet
Dim FoundCell As Range
Dim FirstAddress As String
Dim Counter As Long

Set sh = ThisWorkbook.Worksheets("sheet1")

Counter = 0
Me.ListBox1.Clear

With sh.Range("a:a") 'what was selected??
Set FoundCell = .Find(What:="cash", After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
Lookat:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)

If FoundCell Is Nothing Then
'not found on the sheet
Else
FirstAddress = FoundCell.Address
Do
Me.ListBox1.AddItem FoundCell.Value
Counter = Counter + 1
Set FoundCell = .FindNext(FoundCell)
Loop While Not FoundCell Is Nothing _
And FoundCell.Address < FirstAddress
End If
End With

End Sub

I'm not sure what Counter was used for and I figured the listbox was on the same
worksheet as the commandbutton2.

And watchout. SearchFormat was added in xl2002. If you share your workbook
with people who use xl2k or before, remove this portion.




farmer wrote:

Hi,

I've got a simple problem (I think)

I would like to search within a selection using the find method. Then
transfer the found cells to a listbox.

The problem is that the code I'm using loops a "few times to many".
Does anyone know what's wrong with it?

Here's the code

Private Sub CommandButton2_Click()

Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("sheet1")

sh.UsedRange.Activate
cntend = LastRow(sh)

Selection.Find(What:="cash", After:=ActiveCell, LookIn:=xlValues,
Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Select

counter = 1
Do While counter < cntend

Cells.FindNext(After:=ActiveCell).Activate
ListBox1.AddItem ActiveCell.Value
counter = counter + 1

Loop

End Sub

Help Appreciated,

Farmer


--

Dave Peterson

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Problem with Find filling a listbox vba

Hi,


Thanks a lot, Its just what I wanted



Greetings,

Farmer



On Mon, 11 Oct 2004 19:38:30 -0500, Dave Peterson
wrote:

There's a nice example in VBA's help for .find that shows how to search until
you loop around to the first found cell.

Option Explicit

Private Sub CommandButton2_Click()

Dim sh As Worksheet
Dim FoundCell As Range
Dim FirstAddress As String
Dim Counter As Long

Set sh = ThisWorkbook.Worksheets("sheet1")

Counter = 0
Me.ListBox1.Clear

With sh.Range("a:a") 'what was selected??
Set FoundCell = .Find(What:="cash", After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
Lookat:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)

If FoundCell Is Nothing Then
'not found on the sheet
Else
FirstAddress = FoundCell.Address
Do
Me.ListBox1.AddItem FoundCell.Value
Counter = Counter + 1
Set FoundCell = .FindNext(FoundCell)
Loop While Not FoundCell Is Nothing _
And FoundCell.Address < FirstAddress
End If
End With

End Sub

I'm not sure what Counter was used for and I figured the listbox was on the same
worksheet as the commandbutton2.

And watchout. SearchFormat was added in xl2002. If you share your workbook
with people who use xl2k or before, remove this portion.




farmer wrote:

Hi,

I've got a simple problem (I think)

I would like to search within a selection using the find method. Then
transfer the found cells to a listbox.

The problem is that the code I'm using loops a "few times to many".
Does anyone know what's wrong with it?

Here's the code

Private Sub CommandButton2_Click()

Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("sheet1")

sh.UsedRange.Activate
cntend = LastRow(sh)

Selection.Find(What:="cash", After:=ActiveCell, LookIn:=xlValues,
Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=True, SearchFormat:=False).Activate
ActiveCell.Select

counter = 1
Do While counter < cntend

Cells.FindNext(After:=ActiveCell).Activate
ListBox1.AddItem ActiveCell.Value
counter = counter + 1

Loop

End Sub

Help Appreciated,

Farmer


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
problem filling in blank cells with data above - stumped-in-excel[_2_] Excel Discussion (Misc queries) 1 July 6th 09 10:10 PM
Where do I find a template for filling out 1099-MISC forms? Anne Troy Excel Discussion (Misc queries) 0 February 13th 06 06:59 AM
Filling down problem in VB kate Excel Programming 2 June 9th 04 03:53 PM
filling a two column listbox from a two column recordset Dennis Excel Programming 5 May 23rd 04 10:13 PM
Filling a listbox depending on font index Trevor[_3_] Excel Programming 0 October 1st 03 08:25 PM


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