Cut Values and Paste to another column based on criteria

tranzeodude

New Member
Joined
Jul 24, 2012
Messages
5
My spreadsheet has 'Hours Worked' under column F and 'Pay Code' under column G.
What I want to do is whenever there are '601', '603', '17' under column G 'Pay Code', I want to cut/paste values from column F 'Hours Worked' to column K.

Below is my code which is partially working (it does moved based on the pay codes that I indicated but it is moving 'column G - Pay Code' instead of 'column F - hours worked' that I needed!):

Sub Macro1()
'
'
Dim rngA As Range
Dim rngB As Range
Dim cell As Range

Set rngA = Sheets("Employee Hours").Range("G2:G20")
Set rngB = Sheets("Employee Hours").Range("F2:F20")

For Each cell In rngA
If cell.Value = "601" Or cell.Value = "603" Or cell.Value = "17" Then
ActiveCell.Offset(0, 5).Range("A1").Select
Cells.Copy cell.Offset(0, 4)
Cells.Clear
End If
Next cell
'
End Sub

Please help.
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi

I think a simple numeric loop would achieve the result a bit easier

Code:
Sub Macro1()
Dim i As Integer
Dim MyCodes As String
Dim lstK As Long


MyCodes = "|601|603|17|"


With Sheets("Employee Hours")
    For i = 2 To 20
    
        If InStrRev(MyCodes, "|" & .Range("G" & i) & "|") > 0 Then
            lstK = .Range("K" & Rows.Count).End(xlUp).Row + 1
            .Range("K" & lstK) = .Range("F" & i)
        End If
    
    Next i
End With


End Sub
 
Upvote 0
Thanks but it doesn't do exactly like I intended.


What I want to achieve is to for each row under column G - Pay Code, only move the numbers from column F - hours worked to column K if they contains 601 or 603 or 17.
If no match, then nothing need to be done. Your code seems to move the first 3 rows and nothing happened after that even though there's a match.





Hi

I think a simple numeric loop would achieve the result a bit easier

Code:
Sub Macro1()
Dim i As Integer
Dim MyCodes As String
Dim lstK As Long


MyCodes = "|601|603|17|"


With Sheets("Employee Hours")
    For i = 2 To 20
    
        If InStrRev(MyCodes, "|" & .Range("G" & i) & "|") > 0 Then
            lstK = .Range("K" & Rows.Count).End(xlUp).Row + 1
            .Range("K" & lstK) = .Range("F" & i)
        End If
    
    Next i
End With


End Sub
 
Upvote 0
Looks like In range G2:G20 (as defined in your code there is only one instance of each 17, 601 and 603 so my code works just fine.
 
Upvote 0
Try this:
Code:
Sub Hours()
'Modified  7/23/2018  6:32:11 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Sheets("Employee Hours").Activate
Lastrow = Cells(Rows.Count, "G").End(xlUp).Row
For i = 1 To Lastrow
MyVal = Cells(i, "G").Value
With Cells(i, "G")
            Select Case MyVal
                Case "601", "603", "17"
                    Cells(i, "G").Offset(, -1).Copy Cells(i, "G").Offset(, 4): Cells(i, "G").Offset(, -1).ClearContents
            End Select
End With
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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