Matching values from column C to D and cut pasting to column G

Mgmarm

New Member
Joined
Feb 25, 2015
Messages
32
Hi Guys I am new to the forum :eek:

I am new to vba and I am trying to code a bit of automation..
What I am trying to accomplish is matching the time to the specific code "A207"
and cutting the value and pasting it to another column..

here is what I got

Dim rng As Range
Dim rngFound As Range
Dim cellNumber As Integer


Set rng = Range("C:C")
Set rngFound = rng.Find("A207")
cellNumber = 1

Do While cellNumber < 500
cellNumber = cellNumber + 1
If Range("D" & cellNumber) = rngFound Then
Selection.Cut
Range("G" & cellNumber).Paste


Else
End If

Loop






Thanks,

M
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Among other things, your method of Set rngfound As Range will only ever find the first instance as it's outside of the loop.

How about

Code:
Option Explicit
Sub test()
Dim c As Range
For Each c In Intersect("C:C", Activesheet.Usedrange))
    If c.Value = "A207" Then 
        c.Offset(0, 4) = c.Offset(0, 1)
        c.Offset(0, 1).ClearContents
    End If
Next
End Sub

This will move the value from column D to column G when column C = A207. Is this what you're trying to do? The ranges in your code don't clearly explain what you need.
 
Upvote 0
Hi Guys I am new to the forum :eek:

I am new to vba and I am trying to code a bit of automation..
What I am trying to accomplish is matching the time to the specific code "A207"
and cutting the value and pasting it to another column..

here is what I got

Dim rng As Range
Dim rngFound As Range
Dim cellNumber As Integer


Set rng = Range("C:C")
Set rngFound = rng.Find("A207")
cellNumber = 1

Do While cellNumber < 500
cellNumber = cellNumber + 1
If Range("D" & cellNumber) = rngFound Then
Selection.Cut
Range("G" & cellNumber).Paste


Else
End If

Loop






Thanks,

M


Issue Resolved Thanks Guys


Dim rng As Range
Dim rngFound As Range
Dim cellNumber As Integer


Set rng = Range("C:C")
Set rngFound = rng.Find("A207")
cellNumber = 1

Do While cellNumber < 500
cellNumber = cellNumber + 1
If Range("C" & cellNumber) = rngFound Then
Range("D" & cellNumber).Select
Selection.Cut
Range("G" & cellNumber).Select
ActiveSheet.Paste


End If



Loop
 
Upvote 0
Thanks for the reply Jason your method looks a lot more efficient but I am having problems with this line

For Each c In Intersect("C:C", Activesheet.usedRange))

I think its the usedrange part sorry I know I look like a total newbie :S

Thanks Again!

M


Among other things, your method of Set rngfound As Range will only ever find the first instance as it's outside of the loop.

How about

Code:
Option Explicit
Sub test()
Dim c As Range
For Each c In Intersect("C:C", Activesheet.Usedrange))
    If c.Value = "A207" Then 
        c.Offset(0, 4) = c.Offset(0, 1)
        c.Offset(0, 1).ClearContents
    End If
Next
End Sub

This will move the value from column D to column G when column C = A207. Is this what you're trying to do? The ranges in your code don't clearly explain what you need.
 
Upvote 0
There's nothing wrong with being a newbie, everyone has to start somewhere.

There was a typo in the code, fixed now :)

Code:
Option Explicit
Sub test()
Dim c As Range
For Each c In Intersect(Range("C:C"), ActiveSheet.UsedRange)
    If c.Value = "A207" Then
        c.Offset(0, 4) = c.Offset(0, 1)
        c.Offset(0, 1).ClearContents
    End If
Next
End Sub
 
Upvote 0
Thanks Again!

Code looks great
If I wanted to make a list of values could i just make an Array and feed it to Value

Dim c As Range
Dim Meet As Variant


Meet = Array("A207", "A240", "A224", "A225")


For Each c In Intersect(Range("C:C"), ActiveSheet.UsedRange)
If c.Value = Meet Then
c.Offset(0, 4) = c.Offset(0, 1)
c.Offset(0, 1).ClearContents
End If
Next
 
Upvote 0
You could, but you would need to loop through the array

Code:
Option Explicit
Sub test()
Dim c As Range, meet As Variant, mcount As Long
    
    meet = Array("A207", "A240", "A224", "A225")

For Each c In Intersect(Range("C:C"), ActiveSheet.UsedRange)
    For mcount = LBound(meet) To UBound(meet)
        If c.Value = meet(mcount) Then
            c.Offset(0, 4) = c.Offset(0, 1)
            c.Offset(0, 1).ClearContents
            Exit For
        End If
    Next
Next
End Sub
 
Upvote 0
You're welcome,

I'm going to add a bit more to it for you to break down and learn, this is one of the simplest, yet most effective ways of making code run faster.

Code:
Option Explicit
Sub test()
Dim c As Range, meet As Variant, mcount As Long
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    
    meet = Array("A207", "A240", "A224", "A225")

For Each c In Intersect(Range("C:C"), ActiveSheet.UsedRange)
    For mcount = LBound(meet) To UBound(meet)
        If c.Value = meet(mcount) Then
            c.Offset(0, 4) = c.Offset(0, 1)
            c.Offset(0, 1).ClearContents
            Exit For
        End If
    Next
Next
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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