Copying Cells with a Partial String

BobNewsome

New Member
Joined
Oct 11, 2016
Messages
4
I have a macro where I'm trying to cut rows out of one sheet and place in another based on a partial data string in the cell.

Data:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]1111-1000-8[/TD]
[/TR]
[TR]
[TD]1111-1001-8[/TD]
[/TR]
[TR]
[TD]1111-2000-8[/TD]
[/TR]
[TR]
[TD]1111-2000-8[/TD]
[/TR]
[TR]
[TD]1111-2001-8[/TD]
[/TR]
</tbody>[/TABLE]


Code:
Code:
Sub Macro1()'
Dim myrange As Range
Dim myvalue As String




myvalue = Range("A1:A2000").Find(What:="1111-2", LookIn:=xlFormulas)


'
    For Each Cell In Range("A1:A2000")
        If Cell.Value = myvalue Then
            If myrange Is Nothing Then
                Set myrange = Range(Cell.Address)
            Else
                Set myrange = Union(myrange, Range(Cell.Address))
            End If
        End If
    Next
    
    myrange.Select
    Selection.Cut

End Sub


The VBA is only finding the first 2 "1111-2000" but when the second qualifier changes ie "2001" it does not capture it. How do I get the code to capture ALL of the "1111-2" regardless of the next characters?

Thanks.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hope this helps.
Please try floowing code after save your book.

Code:
Sub Macro1() '
    Dim myvalue As Range, FirstCell As Range, Target As Range
    
    Set myvalue = Range("A1:A2000").Find(What:="111-2*", LookIn:=xlFormulas)
    If myvalue Is Nothing Then
        MsgBox "NotFound"
        Exit Sub
    Else
        Set FirstCell = myvalue
        Set Target = myvalue
    End If
    Do
        Set myvalue = Range("A1:A2000").FindNext(myvalue)
        If myvalue.Address = FirstCell.Address Then
            Exit Do
        Else
            Set Target = Union(Target, myvalue)
        End If
    Loop
    Target.EntireRow.Delete
End Sub
 
Last edited:
Upvote 0
Another option
Code:
Sub chk()
   Dim Rng As Range
   With Range("A1", Range("A" & Rows.Count).End(xlUp))
      .Replace "1111-2", "=XXX1111-2", xlPart, , , , False, False
      Set Rng = .SpecialCells(xlFormulas, xlErrors)
      .Replace "=XXX1111-2", "1111-2", xlPart, , , , False, False
   End With
   Rng.Cut Sheets("[COLOR=#ff0000]New[/COLOR]").Range("A1")
End Sub
Change sheet name in red to suit
 
Upvote 0
Another option
Code:
Sub chk()
   Dim Rng As Range
   With Range("A1", Range("A" & Rows.Count).End(xlUp))
      .Replace "1111-2", "=XXX1111-2", xlPart, , , , False, False
      Set Rng = .SpecialCells(xlFormulas, xlErrors)
      .Replace "=XXX1111-2", "1111-2", xlPart, , , , False, False
   End With
   Rng.Cut Sheets("[COLOR=#ff0000]New[/COLOR]").Range("A1")
End Sub
Change sheet name in red to suit


Thank you Fluff! That worked slick......although I'm not sure why changing the data and then changing it back works, but it does :laugh:

Thanks again!
 
Upvote 0
Glad to help & thanks for the feedback.
Changing the values in a formula (which will cause a formula error) allows the use of the specialcells method to find them.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top