Search and Move Text

mjwilyone

New Member
Joined
Jun 15, 2008
Messages
6
I am working on a macro and, as part of it, I would like to look at column H of my spreadshet. If the wording in any cell in column H begins with the word, "total" I would like the cell contents of the cell in column G and up one to be dropped down one. Here is an example:

Let's say that the words "total money" is found in cell H2. I would like the cell contents of cell G1 to be moved to G2, making boths cell side-by-side having text in them - G2 and H2.

Can anyone help me with this?

Thanks so much,
Mike
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Give this a try. (make sure you backup your workbook first)

Code:
Sub Findit()

    Dim LastRow As Long
    Dim x As Long
    
    LastRow = Worksheets("sheet1").Range("g65536").End(xlUp).Row
    
    For x = LastRow To 1 Step -1
    
        If Left(Cells(x, 7), 5) = "total" Then  ' find cell that matched criteria
            Cells(x, 7).Offset(0, 1).Value = Cells(x, 7).Offset(-1, 1).Value ' copy H cell down
            Cells(x, 7).Offset(-1, 1).Value = ""  'clear moved cell data.
        End If
    Next x
        
End Sub
 
Upvote 0
LilStevie,

The code does not appear to do anything. Does it look for cells in column H having text that begins with the word "Total", then move the cell to the left and up one down one?

Thanks,
Mike
 
Upvote 0
Sorry about that, I read your post incorrectly. Try this:


Code:
Sub Findit()

    Dim LastRow As Long
    Dim x As Long
    
    LastRow = Worksheets("sheet1").Range("h65536").End(xlUp).Row
    
    For x = LastRow To 1 Step -1
    
        If Left(Cells(x, 8), 5) = "total" Then  ' find h row cell that matches criteria
            Cells(x, 8).Offset(0,-1).Value = Cells(x, 8).Offset(-1, -1).Value ' copy g row cell down
            Cells(x, 8).Offset(-1,-1).Value = ""  'clear moved cell data.
        End If
    Next x
        
End Sub
 
Upvote 0
I still get no reponse of any kind. There is no error ... just no progress either. Any ideas?

Thanks,
Mike
 
Upvote 0
Not sure...
Code:
Sub test()
Dim r As Range
For Each r In Range("g2", Range("g" & Rows.Count).End(xlUp))
    If LCase(r.Value) Like "total*" Then r.Offset(,1).Value = r.Offset(-1,1).Value
Next
End Sub
 
Upvote 0
Mike,
the code works fine for me. Can you confirm that your "total" is in the H column and the data that you want to move down is in the G column? Your pasting this code into a standard module and then running it..

try this and let me know what the message box tells you
Code:
Sub Findit()

    Dim LastRow As Long
    Dim x As Long
    Dim i As Integer
    
    
    LastRow = Worksheets("sheet1").Range("h65536").End(xlUp).Row
    i = 0
    For x = LastRow To 2 Step -1
    
        If Left(Cells(x, 8), 5) = "total" Then  ' find h row cell that matches criteria
            Cells(x, 8).Offset(0, -1).Value = Cells(x, 8).Offset(-1, -1).Value ' copy g row cell down
            Cells(x, 8).Offset(-1, -1).Value = "" 'clear moved cell data.
            i = i + 1
        End If
    Next x
        MsgBox "Corrected " & i & " 'total' matches"
End Sub
 
Upvote 0
LilStevie,

Your code was correct ... it did work, once I noticed that my spreadsheet used the word "Total" not "total" as I had originally placed in my request for help. I am sorry to put you through all that work. Didn't think about case sensitivity.

Thank you for code that works!

Mike
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,565
Members
449,038
Latest member
Guest1337

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