Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Add a Range to an existing Range in a loop?

How can I add a range of cells (a whole row) to an existing range, and do
this in a loop?

I want to search for cells containing a specific string, then append all of
the cells in those rows to a Range object, the select the resulting Range.

Sub FindAndSelect()

Dim foundRows As Range
Dim cell As Range
Dim lnLastRow As Long
Dim lnLastCol As Long
Dim target As String
Dim lastCellAddr As String

' Get the address of the last cell containing data
Range("A1").Select
On Error Resume Next
lnLastRow = cells.Find("*", Range("A1"), xlFormulas, , xlByRows,
xlPrevious).Row
lnLastCol = cells.Find("*", Range("A1"), xlFormulas, , xlByColumns,
xlPrevious).Column
lastCellAddr = cells(lnLastRow, lnLastCol).Address

' Get the string to search for
target = InputBox("Enter search string")
If target = "" Then
Exit Sub
End If

' Find all rows containing the search string and add them to the selection
With Worksheets(1).Range("a1:" + lastCellAddr)
Set cell = .Find(target, LookIn:=xlValues, Lookat:=xlPart)
If Not cell Is Nothing Then
firstAddress = cell.Address
Do

' <<< THIS IS WHERE I NEED HELP
' Append all of the cells in cell.EntireRow to the foundRows Range
' foundRows = foundRows + cell.EntireRow
Set foundRows = Union(foundRows, cell.EntireRow)

Set cell = .FindNext(cell)
Loop While cell.Address < firstAddress
End If
End With

' Select all of the rows that contained the search string
foundRows.Select

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Add a Range to an existing Range in a loop?

If Foundrows is nothing then
set foundrows = cell
else
set foundrows = union(cell, foundrows)
end if

'you can use .entirerow later

if foundrows is nothing then
'do nothing
else
foundrows.entirerow.select '???or whatever you want here.
end if



Mick wrote:

How can I add a range of cells (a whole row) to an existing range, and do
this in a loop?

I want to search for cells containing a specific string, then append all of
the cells in those rows to a Range object, the select the resulting Range.

Sub FindAndSelect()

Dim foundRows As Range
Dim cell As Range
Dim lnLastRow As Long
Dim lnLastCol As Long
Dim target As String
Dim lastCellAddr As String

' Get the address of the last cell containing data
Range("A1").Select
On Error Resume Next
lnLastRow = cells.Find("*", Range("A1"), xlFormulas, , xlByRows,
xlPrevious).Row
lnLastCol = cells.Find("*", Range("A1"), xlFormulas, , xlByColumns,
xlPrevious).Column
lastCellAddr = cells(lnLastRow, lnLastCol).Address

' Get the string to search for
target = InputBox("Enter search string")
If target = "" Then
Exit Sub
End If

' Find all rows containing the search string and add them to the selection
With Worksheets(1).Range("a1:" + lastCellAddr)
Set cell = .Find(target, LookIn:=xlValues, Lookat:=xlPart)
If Not cell Is Nothing Then
firstAddress = cell.Address
Do

' <<< THIS IS WHERE I NEED HELP
' Append all of the cells in cell.EntireRow to the foundRows Range
' foundRows = foundRows + cell.EntireRow
Set foundRows = Union(foundRows, cell.EntireRow)

Set cell = .FindNext(cell)
Loop While cell.Address < firstAddress
End If
End With

' Select all of the rows that contained the search string
foundRows.Select

End Sub


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Add a Range to an existing Range in a loop?

You sir are a steely-eyed Excel man. :)

Thanks!

"Dave Peterson" wrote:

If Foundrows is nothing then
set foundrows = cell
else
set foundrows = union(cell, foundrows)
end if

'you can use .entirerow later

if foundrows is nothing then
'do nothing
else
foundrows.entirerow.select '???or whatever you want here.
end if



Mick wrote:

How can I add a range of cells (a whole row) to an existing range, and do
this in a loop?

I want to search for cells containing a specific string, then append all of
the cells in those rows to a Range object, the select the resulting Range.

Sub FindAndSelect()

Dim foundRows As Range
Dim cell As Range
Dim lnLastRow As Long
Dim lnLastCol As Long
Dim target As String
Dim lastCellAddr As String

' Get the address of the last cell containing data
Range("A1").Select
On Error Resume Next
lnLastRow = cells.Find("*", Range("A1"), xlFormulas, , xlByRows,
xlPrevious).Row
lnLastCol = cells.Find("*", Range("A1"), xlFormulas, , xlByColumns,
xlPrevious).Column
lastCellAddr = cells(lnLastRow, lnLastCol).Address

' Get the string to search for
target = InputBox("Enter search string")
If target = "" Then
Exit Sub
End If

' Find all rows containing the search string and add them to the selection
With Worksheets(1).Range("a1:" + lastCellAddr)
Set cell = .Find(target, LookIn:=xlValues, Lookat:=xlPart)
If Not cell Is Nothing Then
firstAddress = cell.Address
Do

' <<< THIS IS WHERE I NEED HELP
' Append all of the cells in cell.EntireRow to the foundRows Range
' foundRows = foundRows + cell.EntireRow
Set foundRows = Union(foundRows, cell.EntireRow)

Set cell = .FindNext(cell)
Loop While cell.Address < firstAddress
End If
End With

' Select all of the rows that contained the search string
foundRows.Select

End Sub


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Add a Range to an existing Range in a loop?

Excellent music. Oh, wait. That was Steely Dan.



Mick wrote:

You sir are a steely-eyed Excel man. :)

Thanks!

"Dave Peterson" wrote:

If Foundrows is nothing then
set foundrows = cell
else
set foundrows = union(cell, foundrows)
end if

'you can use .entirerow later

if foundrows is nothing then
'do nothing
else
foundrows.entirerow.select '???or whatever you want here.
end if



Mick wrote:

How can I add a range of cells (a whole row) to an existing range, and do
this in a loop?

I want to search for cells containing a specific string, then append all of
the cells in those rows to a Range object, the select the resulting Range.

Sub FindAndSelect()

Dim foundRows As Range
Dim cell As Range
Dim lnLastRow As Long
Dim lnLastCol As Long
Dim target As String
Dim lastCellAddr As String

' Get the address of the last cell containing data
Range("A1").Select
On Error Resume Next
lnLastRow = cells.Find("*", Range("A1"), xlFormulas, , xlByRows,
xlPrevious).Row
lnLastCol = cells.Find("*", Range("A1"), xlFormulas, , xlByColumns,
xlPrevious).Column
lastCellAddr = cells(lnLastRow, lnLastCol).Address

' Get the string to search for
target = InputBox("Enter search string")
If target = "" Then
Exit Sub
End If

' Find all rows containing the search string and add them to the selection
With Worksheets(1).Range("a1:" + lastCellAddr)
Set cell = .Find(target, LookIn:=xlValues, Lookat:=xlPart)
If Not cell Is Nothing Then
firstAddress = cell.Address
Do

' <<< THIS IS WHERE I NEED HELP
' Append all of the cells in cell.EntireRow to the foundRows Range
' foundRows = foundRows + cell.EntireRow
Set foundRows = Union(foundRows, cell.EntireRow)

Set cell = .FindNext(cell)
Loop While cell.Address < firstAddress
End If
End With

' Select all of the rows that contained the search string
foundRows.Select

End Sub


--

Dave Peterson


--

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
Loop through a range James Excel Programming 1 April 8th 05 07:59 PM
For Each Loop w/ a range xlcharlie Excel Programming 3 April 8th 05 07:43 PM
loop in range Aksel Børve Excel Programming 3 March 9th 05 02:30 PM
Loop in a Range aksel børve Excel Programming 2 March 8th 05 06:40 PM
Loop through a range Fred[_21_] Excel Programming 6 October 22nd 04 10:45 PM


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