VBA Coding Questions on Copy and Paste

rickloudin

New Member
Joined
Apr 23, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I am new to VBA coding, Want to know if you can copy and paste cells from cells in the row above the cells that meet a certain criteria.
For example A3 meets my criteria and I want to paste the Range B2:E2 into B3:E3. The next time the criteria appears maybe in A8 so I would paste B7:E7 to B8:E8 and so on.
Any help would be greatly appreciated. Thank you!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I am new to VBA coding, Want to know if you can copy and paste cells from cells in the row above the cells that meet a certain criteria.
For example A3 meets my criteria and I want to paste the Range B2:E2 into B3:E3. The next time the criteria appears maybe in A8 so I would paste B7:E7 to B8:E8 and so on.
Any help would be greatly appreciated. Thank you!
Yes this can be done.
Provide more exact details.
Like do you want a script to look down column A and look for some criteria and perform this past action for all cells in column A that meet a certain criteria?
 
Upvote 0
Welcome @rickloudin

Something like this?
Book2
ABCDE
1
2NBCDE
3Y
4N
5N
6N
7N2345
8Y
9N
Sheet1


The VBA code, "copying" the values from the B-E columns from the row above, where the criterion met is the cell in column A = "Y":
VBA Code:
Sub subCopyYitems()

Dim lngLoop As Long

For lngLoop = 2 To 9
    If Cells(lngLoop, 1) = "Y" Then
        Range("B" & Trim(Str(lngLoop - 1)), "E" & Trim(Str(lngLoop - 1))).Copy _
            Destination:=Range("B" & Trim(Str(lngLoop)), "E" & Trim(Str(lngLoop)))
    End If
Next lngLoop

End Sub
 
Upvote 0
Here is a script that would do what you want.
If in column A your search for the value "Alpha"
VBA Code:
Sub Look_For_Criteria()
'Modified 4/24/2021  1:03:45 AM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row

    For i = 1 To Lastrow
        If Cells(i, 1).Value = "Alpha" Then Cells(i, 1).Offset(-1, 1).Resize(, 4).Copy Cells(i, 1).Offset(, 1)
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Welcome @rickloudin

Something like this?
Book2
ABCDE
1
2NBCDE
3Y
4N
5N
6N
7N2345
8Y
9N
Sheet1


The VBA code, "copying" the values from the B-E columns from the row above, where the criterion met is the cell in column A = "Y":
VBA Code:
Sub subCopyYitems()

Dim lngLoop As Long

For lngLoop = 2 To 9
    If Cells(lngLoop, 1) = "Y" Then
        Range("B" & Trim(Str(lngLoop - 1)), "E" & Trim(Str(lngLoop - 1))).Copy _
            Destination:=Range("B" & Trim(Str(lngLoop)), "E" & Trim(Str(lngLoop)))
    End If
Next lngLoop

End Sub
Thank you!
 
Upvote 0
Here is a script that would do what you want.
If in column A your search for the value "Alpha"
VBA Code:
Sub Look_For_Criteria()
'Modified 4/24/2021  1:03:45 AM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row

    For i = 1 To Lastrow
        If Cells(i, 1).Value = "Alpha" Then Cells(i, 1).Offset(-1, 1).Resize(, 4).Copy Cells(i, 1).Offset(, 1)
    Next
Application.ScreenUpdating = True
End Sub
Thank you! What do I need to do it get it to work for my criteria "Bud2020". Works very well if I change by criteria to "alpha" but when i change to my Bud2020 it will not work.
 
Upvote 0
So you have this exact value in column A:
"Bud2020"
And this value is entered manually and not the result of a formula.
show me the code you used that did not work.
And what do you mean by did not work.
Did you get a error message?
And the script provide in post 3 did not work?
 
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

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