Locate and Move specific cells

D

Dean

I have several cells randomly located on a worksheet , each containing a specific word. I need to copy these cells to a new worksheet starting in column A, vertically.

I have used the "Find/Replace" command to locate the cells, but I don't know how to copy them to new sheet.

I'd really appreciate any ideas.

Thanks
 
D

Dave Peterson

What do you mean by move?
Clear the contents of that original cell
delete the cell and move the rows up?
delete the cell and move the columns to the left?
delete the whole column or row?

I'm guessing that you meant to clear contents.

This might work for you.

Option Explicit
Sub testme()

Dim myWords As Variant
Dim curWks As Worksheet
Dim newWks As Worksheet
Dim FoundCell As Range
Dim iCtr As Long
Dim oRow As Long

myWords = Array("asdf8", "asdf24", "asdf33")

Set curWks = Worksheets("sheet1")
Set newWks = Worksheets.Add

oRow = 0
With curWks
For iCtr = LBound(myWords) To UBound(myWords)
Set FoundCell = Nothing
Do
With .UsedRange
Set FoundCell = .Cells.Find(what:=myWords(iCtr), _
after:=.Cells(.Cells.Count), LookIn:=xlValues, _
lookat:=xlWhole, searchorder:=xlByRows, _
searchdirection:=xlNext, MatchCase:=False)
If FoundCell Is Nothing Then
Exit Do
Else
oRow = oRow + 1
With newWks.Cells(oRow, "A")
.Value = myWords(iCtr)
.Offset(0, 1).Value = FoundCell.Address
End With
FoundCell.ClearContents
End If
End With
Loop
Next iCtr
End With

End Sub


If you didn't mean move, but meant copy (and leave the original cell alone),
there's sample code in VBA's help that will show you how to keep track of the
address of the first found cell. Then you keep finding the value until you hit
that saved address.
 
C

coperniq

Hi ;

Dave, your macro is really great. But on the other hand, the proble
is, I still can't find a way to copy the cells and leave th
originals. Could anyone please advise me how to? I have really trie
hard but always get another error message :(

Thanx for your interest.

cop.

<quote>
If you didn't mean move, but meant copy (and leave the original cel
alone), there's sample code in VBA's help that will show you how t
keep track of the
address of the first found cell. Then you keep finding the value unti
you hit that saved address
 
D

Dave Peterson

This is referring to a post from June of 2004--so it's pretty old!

This was the original suggestion:

Option Explicit
Sub testme()

Dim myWords As Variant
Dim curWks As Worksheet
Dim newWks As Worksheet
Dim FoundCell As Range
Dim iCtr As Long
Dim oRow As Long

myWords = Array("asdf8", "asdf24", "asdf33")

Set curWks = Worksheets("sheet1")
Set newWks = Worksheets.Add

oRow = 0
With curWks
For iCtr = LBound(myWords) To UBound(myWords)
Set FoundCell = Nothing
Do
With .UsedRange
Set FoundCell = .Cells.Find(what:=myWords(iCtr), _
after:=.Cells(.Cells.Count), LookIn:=xlValues, _
lookat:=xlWhole, searchorder:=xlByRows, _
searchdirection:=xlNext, MatchCase:=False)
If FoundCell Is Nothing Then
Exit Do
Else
oRow = oRow + 1
With newWks.Cells(oRow, "A")
.Value = myWords(iCtr)
.Offset(0, 1).Value = FoundCell.Address
End With
FoundCell.ClearContents
End If
End With
Loop
Next iCtr
End With

End Sub

See that FoundCell.clearcontents line. That's the line that empties the cell.
If you want it untouched, just remove that line.
 
D

Dave Peterson

Ignore this post.

You have a good answer at your other post in .programming.

(I should have looked at the old code closer.)
 
D

Dave Peterson

You always paste into the same cell.

After you paste each of them, you'll want to determine the next "rngtopaste".

spoiler alert--code follows....

..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
Option Explicit
Sub Copyer()

Dim myWords As Variant
Dim curWks As Worksheet
Dim newWks As Worksheet
Dim rngFirst As Range
Dim FoundCell As Range
Dim rngToSearch As Range
Dim rngFoundCells As Range
Dim iCtr As Long
Dim oRow As Long
Dim rngToPaste As Range


myWords = Array("AAA", "CCC")

Set curWks = Worksheets("sheet1")
Set newWks = Worksheets("sheet10")
Set rngToSearch = curWks.Cells
Set rngToPaste = newWks.Range("A65536").End(xlUp).Offset(1, 0)

oRow = 0

With curWks
Set FoundCell = Nothing
For iCtr = LBound(myWords) To UBound(myWords)
With .UsedRange
Set FoundCell = .Cells.Find(what:=myWords(iCtr), _
after:=.Cells(.Cells.Count), LookIn:=xlValues, _
lookat:=xlWhole, searchorder:=xlByRows, _
searchdirection:=xlNext, MatchCase:=False)

If FoundCell Is Nothing Then
MsgBox "No words found."
Else
Set rngFirst = FoundCell
Set rngFoundCells = FoundCell.Offset(0, 0)
Do
Set rngFoundCells _
= Union(FoundCell.Offset(0, 0), rngFoundCells)
Set FoundCell = rngToSearch.FindNext(FoundCell)
Loop Until rngFirst.Address = FoundCell.Address
rngFoundCells.Copy _
Destination:=rngToPaste
Set rngToPaste = newWks.Range("A65536").End(xlUp).Offset(1, 0)
End If
End With
Next iCtr
End With
End Sub
 
Top