VBA - Copy / paste range based on matching a cell value to sheet name..

BenGee

Board Regular
Joined
Mar 5, 2016
Messages
196
Hi

I'm struggling with the copy / paste part of my code. An example of what I'm trying to do is, if cell B1 in workheets("Rota") matches the name of a worksheet, then copy range B3:B33 and paste this into the matching worksheet in the range V3:V33. I want to do this for each match cell in the range B1:Z1 in worksheets("Rota") and the range to copy is always the adjacent cells in rows 3 to 33.

My Code so far is;
Code:
Dim ws As Worksheet
Dim Target As Range
Dim CopyData As Range
Dim PasteTarget As Range

Set Target = Worksheets("Rota").Range("B1:Z1")
Set CopyData = ActiveCell.Offset(2, 0).Resize(31, 0)
Set PasteTarget = Range("V3").Resize(31, 0)

For Each ws In Sheets
For Each Cell In Target
If Cell.Value = ws.Name Then
CopyData.Copy Destination:=PasteTarget
End If
Next Cel
Next ws
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
How about
Code:
Sub CopyToShts()
   Dim Cl As Range
   
   For Each Cl In Sheets("Rota").Range("B1:Z1")
      If Evaluate("isref('" & Cl.Value & "'!A1)") Then
         Cl.Offset(, 2).Resize(31).copy Sheets(Cl.Value).Range("V3")
      End If
   Next Cl
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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