If Then Statement to Copy and Paste from 1 Table to Another

auslegung

New Member
Joined
Apr 5, 2013
Messages
12
I need the below if statement to be in a loop that goes through all of Table1. However, I am new to programming in general, and brand new to VBA syntax. Can someone please write the code, or point me to a resource that will quickly teach me how to write this specific code?

If K is equal to AM {
Copy AP:AR and paste to AC:AE
}
 
I can't seem to upload a file, so here's the link: https://www.dropbox.com/s/58kypopmgp49kcz/Test copy loop.xlsm?m. I've erased all sensitive information of course, and hidden all unnecessary columns to make it easier to use.

Here's the exact code I'm using. I told you some incorrect columns so I had to change them from your code. If you run your code with no changes then it definitely won't work properly.

Code:
Sub CopyLoop_Drei()
'Assumes That Table1 Contains Headers In Row 1
'Declare Variables
Dim lngRow As Long, lngLoop As Long
'Set Variable To Count Number Of Rows In Table1
lngRow = Range("Table13").Rows.Count + 1
'Loop Through Table, Comparing Value In K to Value In AN
'And Copy AQ:AS To AC:AE In Same Row If Matched
For lngLoop = 2 To lngRow
    If Range("K" & lngLoop).Value = Range("AN" & lngLoop).Value Then _
        Range("AQ" & lngLoop & ":AS" & lngLoop).Copy Range("AC" & lngLoop)
Next lngLoop
'Loops Through Rows Again, Filling In AC:AE With Values In Cells Above If Blank
For lngLoop = 2 To lngRow
    If IsEmpty(Range("AC" & lngLoop)) Then
        Range("AC" & lngLoop & ":AE" & lngLoop).Value = _
        Range("AC" & lngLoop - 1 & ":AE" & lngLoop - 1).Value
    End If
Next lngLoop
End Sub
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I've figured out where the disconnect is. You interpreted my original post

If K is equal to AM {
Copy AP:AR and paste to AC:AE
}

to mean that K2 is only going to match AM2, but that is false. K2 might match any string in AM.

Here's the code I've come up with that contains a nested loop. I'm about to run it and hope it doesn't crash my computer:

Code:
Sub CopyLoop()
'Assumes That Table1 Contains Headers In Row 1
'Declare Variables
Dim lngRow As Long, lngLoop As Long, frsLoop As Long
'Set Variable To Count Number Of Rows In Table1
lngRow = Range("Table13").Rows.Count + 1
'Loop Through Table, Comparing Value In K to Value In AM
'And Copy AP:AR To AC:AE In Same Row If Matched
For frsLoop = 2 to lngRow
    For lngLoop = 2 To lngRow
        If Range("AN" & frsLoop).Value = Range("K" & lngLoop).Value Then _
            Range("AQ" & frsLoop & ":AS" & frsLoop).Copy Range("AC" & lngLoop & ":AE" & lngLoop)
    Next lngLoop
Next frsLoop
End Sub
 
Last edited:
Upvote 0
I've figured it out! Thanks for all your help. The code above has several errors in it, but here's what works, and it didn't crash anything, just took the length of a pee break:

Code:
Sub CopyLoop()
'Assumes That Table1 Contains Headers In Row 1
'Declare Variables
Dim lngRowContact As Long, lngRowMaster As Long, lngLoopContact As Long, lngLoopMaster As Long
'Set Variable To Count Number Of Rows In Table1
lngRowContact = Range("Table45").Rows.Count + 1
lngRowMaster = Range("Table134").Rows.Count + 1

'Loop Through Table, Comparing Value In K to Value In AM
'And Copy AP:AR To AC:AE In Same Row If Matched
For lngLoopContact = 2 To lngRowContact
    For lngLoopMaster = 2 To lngRowMaster
        If Range("AN" & lngLoopContact).Value = Range("K" & lngLoopMaster).Value Then _
            Range("AQ" & lngLoopContact & ":AS" & lngLoopContact).Copy Range("AC" & lngLoopMaster)
    Next lngLoopMaster
Next lngLoopContact
End Sub
 
Upvote 0
Excellent! (Or should I say "Ausgezeichnet, Auslegung!" ?) :biggrin:

I'm sorry I misunderstood your request for so long. But I'm glad you were able to take the code I started out with and modify it to meet your needs.

Cheers!

:beerchug:
 
Upvote 0
You helped quite a lot. I'm working through Codeacademy.com so I have a small amount of knowledge, but don't know any VBA syntax. Your code helped me, and having to make my own edits helped me own it. Thanks again!
 
Upvote 0
You're most welcome, Auslegung. It always feels good when I'm able to help point someone in the right direction.

And thank you for mentioning Codecademy. I've never heard of it before, but since I've recently started studying Python, I think I'll check that out.
 
Upvote 0
Codeacademy is a great place to start. I'm finding that I end up with lots of questions about best practice, etc, that are best answered elsewhere, and it doesn't take too long before you've reached the end of what CA has to offer, but they are an excellent place to start.
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,454
Members
452,514
Latest member
cjkelly15

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