Copy heading and name to applicable sheet

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have names in Col O in Sheet2


I need the header in (O1) as well as the name in Col O to be copied to Z1 on the applicable sheet for eg BR1 on Col O as well as the heading to be copied to Sheet BR1 Z1.


it would be appreciated if someone could kindly provide me with code to do this




Book1
O
1Branch Name
2BR1
3BR2
4BR3
5BR4
6BR5
7BR6
8BR7
9BR8
10BR9
11BR10
12BR11
13BR12
Sheet2
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try:
Code:
Sub CopyCells()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range
    For Each rng In Range("O2:O" & LastRow)
        Sheets(rng.Value).Range("Z1") = Cells(1, "O") & " " & rng
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Mumps


I have a similar problem on Sheet2 where I have the names of the sheets in Col W and email address in the first 3 rows below the relevant sheet name. These are to be copied into AA! in the relevant sheet name

See sample data below


for eg Admin Staff and email addreses 3 rows below to be copied to AA! on sheet admin staff, Sales Staff and email address 3 rows below to be copied to sheet admin staff in AA1


Book1
W
1Sales Staff
2dav@mac.com
3lau@mac.com
4nig@mac.com
5
6Admin Staff
7Ste@mac.com
8pet@mac.com
Sheet2


i have tried to amend your code, but get a syntax error

Code:
  Sheets(rng.Value).Range("AA1") = Cells.Resize(3,(23, "V") & " " & rng


Code:
 Sub CopyEmail_AddressestoRelevantSheets()
  Sheets(2).Select
   Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range
    For Each rng In Range("W1:W" & LastRow)
        Sheets(rng.Value).Range("AA1") = Cells.Resize(3,(23, "V") & " " & rng
    Next rng
    Application.ScreenUpdating = True
End Sub

Kindly check & amend the code
 
Upvote 0
Try:
Code:
Sub CopyCells2()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim x As Long, rng As Range
    For x = 2 To LastRow Step 5
        Range("W" & x).Resize(3).Copy Sheets(Range("W" & x - 1).Value).Range("AA1")
        For Each rng In Sheets(Range("W" & x - 1).Value).Range("AA1").Resize(3)
            rng = Range("W" & x - 1) & " " & rng
        Next rng
    Next x
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hi Mumps


I get subscript out of range when running the macro


Would you like me to email you a smple file ? if so send me a PM


Code:
 Range("W" & x).Resize(3).Copy Sheets(Range("W" & x - 1).Value).Range("AA1")
 
Upvote 0
Instead of using email, perhaps you could upload a copy of your file to a free site such as www.box.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
The macro assumes that there will always be 3 blank rows between each set of email addresses. If this is not the case, then please upload a workbook with enough sample data to show how your data is organized.
Code:
Sub CopyCells2()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim x As Long, rng As Range
    For x = 2 To LastRow Step 7
        Range("W" & x).Resize(3).Copy Sheets(Range("W" & x - 1).Value).Range("AA1")
        For Each rng In Sheets(Range("W" & x - 1).Value).Range("AA1").Resize(3)
            rng = Range("W" & x - 1) & " " & rng
        Next rng
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,792
Members
449,048
Latest member
greyangel23

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