Is there any faster version of my Code??

PritishS

Board Regular
Joined
Dec 29, 2015
Messages
119
Office Version
  1. 2007
Platform
  1. Windows
Dear Sirs/Madams,
I have a code which search for a specific text 'PEN COLOR' in column 2, select that cell and 1 cell to right, then cut the selection and paste 1 cell right to the same row. My code works fine for small amount of data ( or rows (around 1000 rows), but when it goes for large number of rows then it's taking several minutes. Can anyone please suggest any betterment in this below code?


VBA Code:
Sub Move_Pen_Name()
    Dim ws As Worksheet
    Dim aCell As Range
    Dim i As Long
Application.ScreenUpdating = False
    Set ws = ActiveSheet
For i = 1 To Rows.Count
    With ws
        Set aCell = .Columns(2).Find(What:="PEN COLOR", LookIn:=xlValues, _
                    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=True, SearchFormat:=False)
        If Not aCell Is Nothing Then
            aCell.Select
            ActiveCell.Offset(, 1).Resize(1, 1).Cut
            Range(Cells(Selection.Row, 2).Address).Select
            ActiveSheet.Paste
        Else
            Exit Sub
        End If
    End With
Next i
Application.ScreenUpdating = True

End Sub

Thanks in advance.

Regards,
PritishS
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
How about
VBA Code:
Sub PritishS()
    Dim Rng As Range
    With Range("B1", Range("B" & Rows.Count).End(xlUp))
        .Replace "PEN COLOR", True, xlPart, , True, , False, False
       For Each Rng In .SpecialCells(xlConstants, xlLogical).Areas
            Rng.Value = Rng.Offset(, 1).Value
        Next Rng
    End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub PritishS()
    Dim Rng As Range
    With Range("B1", Range("B" & Rows.Count).End(xlUp))
        .Replace "PEN COLOR", True, xlPart, , True, , False, False
       For Each Rng In .SpecialCells(xlConstants, xlLogical).Areas
            Rng.Value = Rng.Offset(, 1).Value
        Next Rng
    End With
End Sub
Hi Fluff

Thanks for your code. Let me try the same and get back to you soon.
 
Upvote 0
Just realised I forgot to clear the cell in col C. To do that use
Rich (BB code):
Sub PritishS()
    Dim Rng As Range
    With Range("B1", Range("B" & Rows.Count).End(xlUp))
        .Replace "PEN COLOR", True, xlPart, , True, , False, False
       For Each Rng In .SpecialCells(xlConstants, xlLogical).Areas
            Rng.Value = Rng.Offset(, 1).Value
            Rng.Offset(, 1).Value = ""
        Next Rng
    End With
End Sub
 
Upvote 0
Just realised I forgot to clear the cell in col C. To do that use
Rich (BB code):
Sub PritishS()
    Dim Rng As Range
    With Range("B1", Range("B" & Rows.Count).End(xlUp))
        .Replace "PEN COLOR", True, xlPart, , True, , False, False
       For Each Rng In .SpecialCells(xlConstants, xlLogical).Areas
            Rng.Value = Rng.Offset(, 1).Value
            Rng.Offset(, 1).Value = ""
        Next Rng
    End With
End Sub
Hi,
I figured that out. :)
I used
VBA Code:
Rng.Offset(, 1).ClearContents
Your code is superb. I'm trying to modify my other code with reference to your code. I'll post the update accordingly. Thanks for getting back.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
You're welcome & thanks for the feedback
Hi,
Actually I posted the other code as example ( Though your solution worked great). Im looking for "which search for a specific text 'PEN COLOR' in column 2, select that cell and 1 cell to right, then cut the selection and paste 1 cell right to the same row", where some cut paste is involved. For that my code is

VBA Code:
Sub Move_Pen_Name()
    Dim ws As Worksheet
    Dim aCell As Range
    Dim i As Long
Application.ScreenUpdating = False
    Set ws = ActiveSheet
For i = 1 To Rows.Count
    With ws
        Set aCell = .Columns(4).Find(What:="PEN COLOR", LookIn:=xlValues, _
                    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=True, SearchFormat:=False)
        If Not aCell Is Nothing Then
            'aCell.Value = "Test"
            aCell.Select
            ActiveCell.Resize(1, 2).Cut
            Range(Cells(Selection.Row, 5).Address).Select
            ActiveSheet.Paste
        Else
            Exit Sub
        End If
    End With
Next i
Application.ScreenUpdating = True

End Sub

Any betterment (faster code) for this possible? :)
 
Upvote 0
Will "PEN COLOR" be on it's own in the cell, or part of a larger phrase?
 
Upvote 0
Will "PEN COLOR" be on it's own in the cell, or part of a larger phrase?
"PEN COLOR" will be the exact text. Not a part of any larger phrase.
.
PENCOLOR.png

Like this. My goal is to find 'PEN COLOR' in D column. Cut that cell and one cell next to it. then paste it to F column of same row. like that way code will search all 'PEN COLOR" in D column and do the same operation till last.
 
Last edited:
Upvote 0
Your code is pasting into col E, not F. Which is correct?
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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