+ Reply to Thread
Results 1 to 6 of 6

Copy a cell and give paste for each name in dataset

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Copy a cell and give paste for each name in dataset

    I'm not too sure how to explain this but my example document shows how I need a macro to work.

    Search for "Name", offset 0,-1 and copy, then offset 6,1 from searched word and paste and drag location down for however many names are in the dataset

    Thanks in advance.
    Attached Files Attached Files
    Last edited by Hyflex; 10-10-2011 at 06:20 PM.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Copy a cell and give paste for each name in dataset

    Hyflex,

    Your data is in column A, so when you say offset(0,-1), that's trying to get 1 cell to the left of column A, which doesn't exist. Do you mean go one cell down? (which would be offset(1,0)
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Copy a cell and give paste for each name in dataset

    Oooh, i think i see what you want now. In column A, search for text "Name". Then go to the cell above it (one row up), which would be .Offset(-1,0). Ok, I get it now, will have a macro in a sec

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Copy a cell and give paste for each name in dataset

    Give this a try:
    Sub tgr()
        
        Dim rngName As Range: Set rngName = Columns("A").Find("Name")
        If rngName Is Nothing Then
            MsgBox "No cells in column A that contain ""Name"""
            Exit Sub
        End If
        Dim rngPrior As Range: Set rngPrior = rngName
        
        While Not rngName Is Nothing
            If rngName.Row < rngPrior.Row Then Exit Sub
            rngName.Offset(-1, 0).Copy rngName.Offset(1, 6).Resize(rngName.CurrentRegion.Rows.Count - 2)
            Set rngPrior = rngName
            Set rngName = Columns("A").Find("Name", rngName)
        Wend
        
    End Sub

  5. #5
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Re: Copy a cell and give paste for each name in dataset

    Quote Originally Posted by tigeravatar View Post
    Give this a try:
    Sub tgr()
        
        Dim rngName As Range: Set rngName = Columns("A").Find("Name")
        If rngName Is Nothing Then
            MsgBox "No cells in column A that contain ""Name"""
            Exit Sub
        End If
        Dim rngPrior As Range: Set rngPrior = rngName
        
        While Not rngName Is Nothing
            If rngName.Row < rngPrior.Row Then Exit Sub
            rngName.Offset(-1, 0).Copy rngName.Offset(1, 6).Resize(rngName.CurrentRegion.Rows.Count - 2)
            Set rngPrior = rngName
            Set rngName = Columns("A").Find("Name", rngName)
        Wend
        
    End Sub
    I tried to make it do exact matchs for the word "Name" but it doesnt work, I really struggle to put the LookAt:=xlWhole part in...

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Copy a cell and give paste for each name in dataset

    Hyflex,

    Here's the code with the LookAt:=xlWhole property
    Sub tgr()
        
        Dim rngName As Range: Set rngName = Columns("A").Find(What:="Name", LookAt:=xlWhole)
        If rngName Is Nothing Then
            MsgBox "No cells in column A that contain ""Name"""
            Exit Sub
        End If
        Dim rngPrior As Range: Set rngPrior = rngName
        
        While Not rngName Is Nothing
            If rngName.Row < rngPrior.Row Then Exit Sub
            rngName.Offset(-1, 0).Copy rngName.Offset(1, 6).Resize(rngName.CurrentRegion.Rows.Count - 2)
            Set rngPrior = rngName
            Set rngName = Columns("A").Find(What:="Name", After:=rngName, LookAt:=xlWhole)
        Wend
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1