Cut and Move relative to starting position

Belair58

Board Regular
Joined
Mar 31, 2005
Messages
95
Hello,

I'm attempting to find a word, highlight that cell and all the cells to the right of it. Cut that range and then paste it over 3 cells to the right.

Once I get the cut and paste working, I'll need to work on the loop to look at the entire column.

I think I'm close but I'm getting a run-time error 91 "Object Variable or With block variable not set" with the code below.

Any help would be appreciated.

Code:
Sub MoveTotals()
'

    Dim rngFound As Range
    Dim rngCapture As Range
    
    With Worksheets(1).Range("D:D")
    
        Set rngFound = .Find(What:="Totals:", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If Not (rngFound Is Nothing) Then
    
        rngCapture = rngFound.End(xlToRight).Select
        rngCapture.Cut
            rngFound.Offset(0, 3).Value = rngCapture
    
        Else
            MsgBox "The word 'Totals:' was not found in column D"
        End If
    End With
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try:
Code:
Sub MoveTotals()
    Application.ScreenUpdating = False
    Dim rngFound As Range, sAddr As String, lCol As Long
    lCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    With Worksheets(1).Range("D:D")
        Set rngFound = .Find(What:="Totals:", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If Not rngFound Is Nothing Then
            sAddr = rngFound.Address
            Do
                lCol = Cells(rngFound.Row, Columns.Count).End(xlToLeft).Column
                Range(Cells(rngFound.Row, 5), Cells(rngFound.Row, lCol)).Cut rngFound.Offset(0, 3)
                Set rngFound = .FindNext(rngFound)
            Loop While rngFound.Address <> sAddr
            sAddr = ""
        End If
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Mumps,

You rock!

I'm going to work through your changes to see where I went wrong.

Works great, and thanks!!
 
Upvote 0
You are very welcome. :) Nice car! I had a 1966 Belair in my younger days.
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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