VBA Macro to copy and paste certian cells to another sheet based on another cell value

Steves73

Board Regular
Joined
Oct 19, 2016
Messages
173
Office Version
  1. 365
Platform
  1. Windows
hello

I am very new to VBA, I have been searching for weeks now for a easy macro to copy values from certain cells and paste them to another sheet based on another cell value

Example - Macro to search column "O" if it finds the word "Reorder" it copies cells A1, A2, A4, and paste the values to Sheet 3, starting in column A. The paste finds the next blank cell.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Welcome to the Board!

What is the sheet name containing the column O you are searching?
So, are you saying that no matter what row "Reorder" is found in, you are going to copy cells A1, A2 and A4?
Are they being pasted in column A going down, or in columns A, B, and C?
 
Upvote 0
Hi Joe,

Thanks for the reply
Please see below in "red"

Welcome to the Board!

What is the sheet name containing the column O you are searching? - Sheet 1
So, are you saying that no matter what row "Reorder" is found in, you are going to copy cells A1, A2 and A4? - Yes correct, also, once I see the code I have the option to change the reference column if possible.
Are they being pasted in column A going down, or in columns A, B, and C?
- Would like them copied and pasted to say Sheet 2 - Row 2

 
Upvote 0
Try this:
Code:
Sub MyCopy()

    Dim lastRow As Long
    Dim myRow As Long
    Dim myCopyRow As Long
    
'   Set initial row to copy to as 2
    myCopyRow = 2
    
'   Find last row with data in column D on sheet 1
    lastRow = Sheets("Sheet1").Cells(Rows.Count, "D").End(xlUp).Row
    
    Application.ScreenUpdating = False
    
'   Loop through all rows in sheet 1
    For myRow = 1 To lastRow
        If Sheets("Sheet1").Cells(myRow, "D") = "Reorder" Then
            Sheets("Sheet2").Cells(myCopyRow, "A") = Sheets("Sheet1").Range("A1")
            Sheets("Sheet2").Cells(myCopyRow, "B") = Sheets("Sheet1").Range("A2")
            Sheets("Sheet2").Cells(myCopyRow, "C") = Sheets("Sheet1").Range("A4")
'           Increment row counter
            myCopyRow = myCopyRow + 1
        End If
    Next myRow
                  
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Hi Joe

Apologies, Probably didn't explain this very well

Below highlighted in red are the times I would need to be copied to Sheet 2 based on the word 'Reorder"


Part NameSupplierDepartment 48535Reorder
Part NameSupplierDepartment 48626Reorder
Part NameSupplierDepartment 36
Part NameSupplierDepartment 48535Reorder
Part NameSupplierDepartment 48
Part NameSupplierDepartment 48
Part NameSupplierDepartment 48626Reorder
Part NameSupplierDepartment 01

<tbody>
</tbody><colgroup><col span="2"><col><col span="9"></colgroup>

Part NameSupplierDepartment 48535Reorder
Part NameSupplierDepartment 48626Reorder
Part NameSupplierDepartment 36
Part NameSupplierDepartment 48535Reorder
Part NameSupplierDepartment 48
Part NameSupplierDepartment 48
Part NameSupplierDepartment 48626Reorder
Part NameSupplierDepartment 01

<tbody>
</tbody><colgroup><col span="2"><col><col span="9"></colgroup>
 
Upvote 0
I don't think that picture helps that much without a detailed explanation.
Can you build off the explanation have you given so far?
Was anything you mentioned earlier erroneous or incomplete?
 
Upvote 0
Ok, Basically the spread sheet is used to control warehouse stock, the spread sheet has all the items details in rows across the spreadsheet (Columns A - O) Each item has a quantity allocated to it, when I remove a items from stock the quantity reduces, once it reaches a certain level the sheet will place the word "Reorder" next to the line item.

What I need was a macro which search's the column that has the word 'Reorder" then copies and pastes the cells within the row instead of the entire row. Dependent on which row has the word "Reorder" the macro will move to that row, then copies cells, say A1, B1 and G1, then it looks at the next row in column "O" and if "Reorder" is found it copies A2, B2, and G2 and so on. if the row does not have the word "Reorder" it moves the next until it has checked all rows
 
Upvote 0
OK, so it is NOT always copying cells A1, A2, and A4.

If you always want to copy columns A, B, and G from the Reorder rows, try:
Code:
Sub MyCopy()

    Dim lastRow As Long
    Dim myRow As Long
    Dim myCopyRow As Long
    
'   Set initial row to copy to as 2
    myCopyRow = 2
    
'   Find last row with data in column D on sheet 1
    lastRow = Sheets("Sheet1").Cells(Rows.Count, "D").End(xlUp).Row
    
    Application.ScreenUpdating = False
    
'   Loop through all rows in sheet 1
    For myRow = 1 To lastRow
        If Sheets("Sheet1").Cells(myRow, "D") = "Reorder" Then
            Sheets("Sheet2").Cells(myCopyRow, "A") = Sheets("Sheet1").Cells(myRow, "A")
            Sheets("Sheet2").Cells(myCopyRow, "B") = Sheets("Sheet1").Cells(myRow, "B")
            Sheets("Sheet2").Cells(myCopyRow, "G") = Sheets("Sheet1").Cells(myRow, "G")
'           Increment row counter
            myCopyRow = myCopyRow + 1
        End If
    Next myRow
                  
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Thanks mate

Works perfectly

Thanks you very much for your help
 
Upvote 0

Forum statistics

Threads
1,215,129
Messages
6,123,214
Members
449,091
Latest member
jeremy_bp001

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