+ Reply to Thread
Results 1 to 7 of 7

Copy/Paste Adjacent Cells based on ActiveCell

Hybrid View

brgr4u Copy/Paste Adjacent Cells... 04-24-2013, 02:12 PM
HaHoBe Re: Copy/Paste Adjacent Cells... 04-24-2013, 02:40 PM
brgr4u Re: Copy/Paste Adjacent Cells... 04-24-2013, 02:46 PM
HaHoBe Re: Copy/Paste Adjacent Cells... 04-24-2013, 03:00 PM
brgr4u Re: Copy/Paste Adjacent Cells... 04-24-2013, 03:10 PM
HaHoBe Re: Copy/Paste Adjacent Cells... 04-24-2013, 03:20 PM
brgr4u Re: Copy/Paste Adjacent Cells... 04-24-2013, 03:41 PM
  1. #1
    Registered User
    Join Date
    02-20-2013
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Excel 2007
    Posts
    28

    Copy/Paste Adjacent Cells based on ActiveCell

    Hello,

    I am looking for a piece of code that will search through the data in column E and if the value in column E is #N/A, then it will copy the adjacent data in columns A-D into a new worksheet.

    I have tried using Offset to select the 4 cells to the left of the activecell but I cannot select all 4 at the same time.

    Any help is greatly appreciated!

    Thank you!

    Sam
    Last edited by brgr4u; 04-24-2013 at 03:41 PM.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Copy/Paste Adjacent Cells based on ActiveCell

    Hi, Sam,

    maybe this may give you a hint:
    Sub EF917764()
    Dim lngLast As Long
    Dim lngCounter As Long
    Dim lngCopy As Long
    
    lngLast = Cells(Rows.Count, "E").End(xlUp).Row
    lngCopy = 3
    
    For lngCounter = 1 To lngLast
      If IsError(Cells(lngCounter, "E")) Then
        Sheets("new").Cells(lngCopy, "A").Resize(1, 4).Value = Cells(lngCounter, "A").Resize(1, 4).Value
        lngCopy = lngCopy + 1
      End If
    Next lngCounter
    End Sub
    Cioa,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    02-20-2013
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Copy/Paste Adjacent Cells based on ActiveCell

    I think that may get me close, can you tell me if what I came up with is workable?

    on the line that contains "If Activecell.Value = "N/A" Then" it kicks out a mismatch error

    Dim i As Integer
    
        For i = 2 To Range("C" & Rows.Count).End(xlUp).Row
            Range("e" & i).Select
            With ActiveSheet
                If ActiveCell.Value = "#N/A" Then
                ActiveCell.Offset(0, -4).Range("A1:D1").Select
                Selection.Copy Destination:=Sheets("Lookup").Range("B").End(xlDown).Row
                Else: End If
            End With
        Next i
    Last edited by brgr4u; 04-24-2013 at 02:48 PM.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Copy/Paste Adjacent Cells based on ActiveCell

    Hi, brgr4u,

    what does not available mean for Excel? An error occurred - maybe that´s why I used IsError.

    And there is hardly one good reason to select each and every cell to work with ActiveCell (except a lot of unnecessary cursor movement and the macro recorder displaying code like that with Selection).

    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    02-20-2013
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Copy/Paste Adjacent Cells based on ActiveCell

    That makes a lot of sense, but is it possible to put a copy/paste function in the same For-Next Loop? I would like to copy the data in columns A-D adjacent to the activecell to the bottom of Column B in another sheet.

    The original sheet is called "Apr Pivot" and the sheet data will be pasted into is called "Lookup"

    Thank you for all your help already!!

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Copy/Paste Adjacent Cells based on ActiveCell

    Hi, brgr4u,

    do you have any formulas that you want to copy? I generally try to put values into the backup sheet as the data may show different results from what they showed on the original sheet.

    Sub EF917764_2()
    Dim lngLast As Long
    Dim lngCounter As Long
    Dim wsData As Worksheet
    Dim wsCopy As Worksheet
    
    Set wsData = Sheets("Apr Pivot")
    Set wsCopy = Sheets("Lookup")
    
    With wsData
      lngLast = .Cells(Rows.Count, "E").End(xlUp).Row
    End With
    
    For lngCounter = 1 To lngLast
      If IsError(wsData.Cells(lngCounter, "E")) Then
        With wsCopy
          .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Resize(1, 4).Value = wsData.Cells(lngCounter, "A").Resize(1, 4).Value
        End With
      End If
    Next lngCounter
    
    Set wsCopy = Nothing
    Set wsData = Nothing
    End Sub
    Ciao,
    Holger
    Last edited by HaHoBe; 04-24-2013 at 03:43 PM. Reason: typos - why do I care to look at the posting in preview? :(

  7. #7
    Registered User
    Join Date
    02-20-2013
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Copy/Paste Adjacent Cells based on ActiveCell

    Yes!!!!! Thank you!!!

+ 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