Copy value based on criteria of different cell

Saoirse

New Member
Joined
Jan 27, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
I am trying to create a macro that automatically copies data from sheet 1 to sheet 2 based on the data in sheet 1.
However, everyday I change the information in sheet 1. So I am trying to create a macro I can run to have the info pull daily.

I want columns C and D to copy over to the cooresponding sheet of the employee. So sheet 1 A2 has Albert with a Q and a P column. I am trying to get B2 and C2 to transfer over to the "Albert" sheet but only if they coorespond with Albert on sheet 1.

bench(153).xlsm
ABC
1Albert
2I want this cell to have yesterday's dateThis cell should copy the data from Sheet 1 column B but only when column A says "Albert"This cell should copy the data from Sheet 1 column C but only when column A says "Albert"
3Previous DateQueuedPended
41/30/2022
5
6
Albert
Cell Formulas
RangeFormula
A4A4=TODAY()-1


bench(153).xlsm
ABC
1UserIDQP
2Albert126436
3Bradley27236
4Conner81334
5Deanna87315
6Eli6581
7Felicia46298
8Greg142254
9Henry22134
10Isacc23314
11Jeremy84403
12Kyle18
13Lawrance102151
14Mary11359
15Nancy49274
16Oscar49229
17Peter17535
18Quincy164285
19Robert158149
20Sam3578
Sheet 1
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hello Saoirse,

The below code should do what you want.

VBA Code:
Sub CopyCD()
Application.ScreenUpdating = False 'Turns off screen updating - runs faster
Dim i As Integer
i = 1  ' i is the row number that changes by 1 for each loop

For i = 1 To 20 'loops from A1 to A20 (You can change that to the last row you have)
    If Cells(i, 1).Value = Sheets("Data").Cells(i, 1).Value Then 'checks the A1 values match
    Sheets("Data").Cells(i, 2).Copy 'Copies B2
    Cells(i, 2).Select 'Selects and pastes values to B2
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Data").Cells(i, 3).Copy 'Copies C2
    Cells(i, 3).Select 'Selects and pastes values to B2
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    End If
Next i 'Loops through rows 2,3,4,5,..20
Application.CutCopyMode = False
Application.ScreenUpdating = True 'Turns on screen updating again
End Sub

Jamie
 
Upvote 0
Unfortunately it didn't work :( I'm not sure where I went wrong, but no luck.
 
Upvote 0
Hello,

Did you change where it says - Sheets("Data"). to Sheets("Albert"). for every instance?

Jamie
 
Upvote 0
Sub CopyCD()
Application.ScreenUpdating = False
Dim i As Integer
i = 1

For i = 1 To 20
If Cells(i, 1).Value = Sheets("Albert").Cells(i, 1).Value Then
Sheets("Albert").Cells(i, 2).Copy
Cells(i, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Albert").Cells(i, 3).Copy
Cells(i, 3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next i
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

bench(153).xlsm
ABC
1Albert
2I want this cell to have yesterday's dateThis cell should copy the data from Sheet 1 column B but only when column A says "Albert"This cell should copy the data from Sheet 1 column C but only when column A says "Albert"
3Previous DateQueuedPended
42/2/2022
5
6
Albert
Cell Formulas
RangeFormula
A4A4=TODAY()-1


bench(153).xlsm
ABC
1UserIDQP
2Albert126436
3Bradley27236
4Conner81334
5Deanna87315
6Eli6581
7Felicia46298
8Greg142254
9Henry22134
10Isacc23314
11Jeremy84403
12Kyle18
13Lawrance102151
14Mary11359
15Nancy49274
16Oscar49229
17Peter17535
18Quincy164285
19Robert158149
20Sam3578
21Timothy112168
22Ursula553
Sheet 1
 
Upvote 0
Hello,

I think it is backwards. :)

The macro 'button
will be on the Albert tab.

And then replace Each "ALBERT" in the code with the name of the sheet with the user id column.

The first part of the loop reads like this

VBA Code:
If Cells(i, 1).Value = Sheets("Albert").Cells(i, 1).Value Then     'if the cell on THIS sheet = the cell on the other sheet then do this
Sheets("Albert").Cells(i, 2).Copy                                              'Copy the cell on the other sheet
Cells(i, 2).Select                                                                       'Paste the values of the cell on the THIS sheet
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

NB:- The loop should be 2-20 that will start on cell A2 - 1 to 20 starts on A1. My mistake.

Jamie
 
Upvote 0
no luck again. will send screen shots once on other computer. This computer doesn't do the xl2bb thing. will send in a few hours.
 
Upvote 0
still unable to get columns C and D to copy over to associated sheet.

Sub CopyCD()
Application.ScreenUpdating = False
Dim i As Integer
i = 1

For i = 1 To 20
If Cells(i, 1).Value = Sheets("Albert").Cells(i, 1).Value Then
Sheets("Albert").Cells(i, 2).Copy
Cells(i, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Albert").Cells(i, 3).Copy
Cells(i, 3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next i
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

bench.xlsm
ABC
1UserIDQP
2Albert126436
3Bradley27236
4Conner81334
5Deanna87315
6Eli6581
7Felicia46298
8Greg142254
9Henry22134
10Isacc23314
11Jeremy84403
12Kyle18
13Lawrance102151
14Mary11359
15Nancy49274
16Oscar49229
17Peter17535
18Quincy164285
19Robert158149
20Sam3578
21Timothy112168
22Ursula553
Sheet 1


bench.xlsm
ABC
1Albert
2I want this cell to have yesterday's dateThis cell should copy the data from Sheet 1 column B but only when column A says "Albert"This cell should copy the data from Sheet 1 column C but only when column A says "Albert"
3Previous DateQueuedPended
42/6/2022
5
Albert
Cell Formulas
RangeFormula
A4A4=TODAY()-1
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,962
Latest member
Fenes

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