SOLVED

VBA: Simple Copy and Paste using Cells

%3CLINGO-SUB%20id%3D%22lingo-sub-2080325%22%20slang%3D%22en-US%22%3EVBA%3A%20Simple%20Copy%20and%20Paste%20using%20Cells%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2080325%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20simple%20question%20for%20someone%20well%20versed%20in%20VBA.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20sample%20workbook%20titled%20%22CopyPaste.xlsm%22%20with%20%22Table1%22%20on%20%22Sheet1.%22%20The%20workbook%20is%20enclosed%20if%20you%20are%20curious.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22CopyPaste.jpg%22%20style%3D%22width%3A%20998px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F247856iC1ADEC398220DCA6%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22CopyPaste.jpg%22%20alt%3D%22Copy%20and%20Paste%20from%20a%20Table%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3ECopy%20and%20Paste%20from%20a%20Table%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EHere%20is%20what%20I%20want%20to%20do%20using%20VBA%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3COL%3E%3CLI%3EDetermine%20the%20row%20for%20the%20active%20cell.%3C%2FLI%3E%3CLI%3ECopy%20that%20row%20from%20columns%20A%20to%20H.%3C%2FLI%3E%3CLI%3EPaste%20the%20value%20from%20the%20copied%20row%20to%20A20.%3C%2FLI%3E%3CLI%3ECancel%20copy.%3C%2FLI%3E%3C%2FOL%3E%3CP%3EI%20also%20want%20to%20use%20%22cells%22%20in%20my%20VBA%20code.%20I%20have%20one%20small%20routine%20that%20works%2C%20and%20one%20that%20doesn't.%20I%20am%20curious%20as%20to%20what%20the%20problem%20is%20with%20the%20one%20that%20fails.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20HardCodeRowA()%0A%0A'%5C%20This%20rountine%20works.%0A%0ADim%20lRowValue%20As%20Long%0ADim%20rnKeyRange%20As%20Range%0A%0A%20%20%20%20lRowValue%20%3D%20ActiveCell.Row%0A%0A%20%20%20%20ActiveSheet.Range(Cells(lRowValue%2C%201)%2C%20Cells(lRowValue%2C%208)).Copy%0A%0A%20%20%20%20Cells(20%2C%201).PasteSpecial%20Paste%3A%3DxlPasteValues%0A%0A%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20HardCodeRowB()%0A%0A'%5C%20This%20rountine%20fails.%0A%0ADim%20lRowValue%20As%20Long%0ADim%20rnKeyRange%20As%20Range%0A%0A%20%20%20%20Set%20rnKeyRange%20%3D%20Sheets(%22Sheet1%22).Range(%22Table1%22)%0A%0A%20%20%20%20lRowValue%20%3D%20ActiveCell.Row%0A%0A%20%20%20%20rnKeyRange(Cells(lRowValue%2C%201)%2C%20Cells(lRowValue%2C%208)).Copy%0A%20%20%20%20'%5C%20Although%20I%20get%20no%20errors%20when%20I%20single%20step%20through%2C%20the%20routine_%0A%20%20%20%20'%5C%20does%20not%20seem%20to%20copy.%0A%0A%20%20%20%20Cells(20%2C%201).PasteSpecial%20Paste%3A%3DxlPasteValues%0A%0A%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20someone%20please%20comment%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%2C%20if%20there%20is%20a%20more%20elegant%20solution%20using%20Cells%2C%20please%20share%20that%20solution%20with%20me%20too.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20help.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2080325%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2080473%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%3A%20Simple%20Copy%20and%20Paste%20using%20Cells%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2080473%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F549380%22%20target%3D%22_blank%22%3E%40KStecyk%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20rnKeyRange(Cells(lRowValue%2C%201)%2C%20Cells(lRowValue%2C%208))%2C%20Cells(lRowValue)%2C%201)%20acts%20as%20the%20row%20number%20and%20Cells(lRowValue%2C%20%3CLI-EMOJI%20id%3D%22lia_smiling-face-with-sunglasses%22%20title%3D%22%3Asmiling_face_with_sunglasses%3A%22%3E%3C%2FLI-EMOJI%3E%20as%20the%20column%20number.%20Since%20the%20numbers%20in%20your%20table%20are%20quite%20high%2C%20rnKeyRange(Cells(lRowValue%2C%201)%2C%20Cells(lRowValue%2C%208))%20refers%20to%20a%20cell%20far%20outside%20the%20table.%3C%2FP%3E%0A%3CP%3EThat%20is%20obviously%20not%20what%20you%20want.%3C%2FP%3E%0A%3CP%3EYou%20could%20use%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ErnKeyRange.Range(Cells(lRowValue%20-%201%2C%201)%2C%20Cells(lRowValue%20-%201%2C%208)).Copy%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20used%20lRowValue%20-%201%20since%20rnKeyRange%20is%20the%20data%20range%3B%20it%20starts%20in%20row%202.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20an%20alternative%20version%20of%20the%20macro%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20HardCodeRowC()%0A%20%20%20%20Dim%20lRowValue%20As%20Long%0A%20%20%20%20lRowValue%20%3D%20ActiveCell.Row%0A%20%20%20%20Cells(20%2C%201).Resize(1%2C%208).Value%20%3D%20Cells(lRowValue%2C%201).Resize(1%2C%208).Value%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2080498%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%3A%20Simple%20Copy%20and%20Paste%20using%20Cells%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2080498%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F549380%22%20target%3D%22_blank%22%3E%40KStecyk%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETry%20this%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20macro2()%0A%20%20%20%20%20%0A%20%20%20%20%20If%20ActiveSheet.Name%20%26lt%3B%26gt%3B%20Sheet1.Name%20Then%0A%20%20%20%20%20%20%20%20%20%20Exit%20Sub%0A%20%20%20%20%20End%20If%0A%20%20%20%20%20%0A%20%20%20%20%20With%20Sheet1.ListObjects(%22Table1%22)%0A%20%20%20%20%20%20%20%20%20%20If%20Not%20Intersect(ActiveCell%2C%20.DataBodyRange)%20Is%20Nothing%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20With%20Intersect(ActiveCell.EntireRow%2C%20.DataBodyRange)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Sheet1.Range(%22A20%22).Resize(1%2C%20.Columns.Count).Value%20%3D%20.Value%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20End%20With%0A%20%20%20%20%20%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20line%20of%20HardCodeB%20does%20not%20work%20because%20it%20is%20expecting%20a%20reference%20in%20the%20format%20rnKeyRange(Row%2C%20Col)%20and%20you%20are%20giving%20it%20range%20references.%20For%20example%2C%20rnKeyRange(1%2C1)%20is%20a%20reference%20to%20cell%20%22A2%22%20(the%20top%20left%20cell%20of%20the%20rnKeyRange%20%22A2%3AH16%22).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERange%20references%20are%20relative%20to%20your%20starting%20range.%20Since%20your%20lRowValue%20is%20not%20relative%20to%20the%20Range%20rnKeyRange%2C%20but%20is%20relative%20to%20the%20worksheet%2C%20you%20should%20be%20able%20to%20use%3C%2FP%3E%3CP%3ESheet1.Range(Sheet1.Cells(lRowValue%2C%201)%2C%20Sheet1.Cells(lRowValue%2C%208)).Copy%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eor%3C%2FP%3E%3CP%3EWith%20rnKeyRange.Worksheet%3CBR%20%2F%3E.Range(.Cells(lRowValue%2C%201)%2C%20.Cells(lRowValue%2C%208)).Copy%3CBR%20%2F%3EEnd%20With%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20is%20a%20generally%20good%20practice%20to%20qualify%20your%20Cells%20reference%20for%20situations%20in%20which%20your%20macro%20may%20not%20be%20running%20on%20the%20active%20sheet.%20For%20example%2C%20Sheet1.Range(Cells(1%2C1)%2C%20Cells(1%2C10))%20-%20the%20Range%20object%20refers%20to%20Sheet1%2C%20but%20the%20Cells%20objects%20still%20refer%20to%20the%20active%20sheet.%20Not%20really%20necessary%20in%20this%20case%20since%20I'm%20testing%20to%20ensure%20the%20activesheet%20is%20Sheet1%20before%20proceeding%2C%20but%20I%20do%20it%20anyway.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2084618%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%3A%20Simple%20Copy%20and%20Paste%20using%20Cells%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2084618%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CBLOCKQUOTE%3E%3CP%3EIn%20rnKeyRange(Cells(lRowValue%2C%201)%2C%20Cells(lRowValue%2C%208))%2C%20Cells(lRowValue)%2C%201)%20acts%20as%20the%20row%20number%20and%20Cells(lRowValue%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E8)as%20the%20column%20number.%20Since%20the%20numbers%20in%20your%20table%20are%20quite%20high%2C%20rnKeyRange(Cells(lRowValue%2C%201)%2C%20Cells(lRowValue%2C%208))%20refers%20to%20a%20cell%20far%20outside%20the%20table.%3C%2FP%3E%3CP%3EThat%20is%20obviously%20not%20what%20you%20want.%3C%2FP%3E%3C%2FBLOCKQUOTE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20believe%20that%20I%20provided%20a%20range%20reference%20and%20it%20was%20expecting%20a%20row%20and%20column.%20Please%20see%20JMB17's%20response.%20If%20I%20choose%20F7%2C%20then%20lRowValue%20is%207.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CBLOCKQUOTE%3E%3CP%3EYou%20could%20use%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ErnKeyRange.Range(Cells(lRowValue%20-%201%2C%201)%2C%20Cells(lRowValue%20-%201%2C%208)).Copy%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20used%20lRowValue%20-%201%20since%20rnKeyRange%20is%20the%20data%20range%3B%20it%20starts%20in%20row%202.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20an%20alternative%20version%20of%20the%20macro%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FBLOCKQUOTE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20HardCodeRowC()%0A%20%20%20%20Dim%20lRowValue%20As%20Long%0A%20%20%20%20lRowValue%20%3D%20ActiveCell.Row%0A%20%20%20%20Cells(20%2C%201).Resize(1%2C%208).Value%20%3D%20Cells(lRowValue%2C%201).Resize(1%2C%208).Value%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CHR%20%2F%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBoth%20of%20those%20two%20solutions%20work%20well.%20And%20you%20answered%20my%20concern%20of%20how%20to%20modify%20my%20code%20to%20make%20it%20work.%20I%20understand%20why%20you%20subtracted%201%20from%20lRowValue.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20help.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

I have a simple question for someone well versed in VBA.

 

I have a sample workbook titled "CopyPaste.xlsm" with "Table1" on "Sheet1." The workbook is enclosed if you are curious.

 

Copy and Paste from a TableCopy and Paste from a Table

 

Here is what I want to do using VBA:

 

  1. Determine the row for the active cell.
  2. Copy that row from columns A to H.
  3. Paste the value from the copied row to A20.
  4. Cancel copy.

I also want to use "cells" in my VBA code. I have one small routine that works, and one that doesn't. I am curious as to what the problem is with the one that fails.

 

 

Sub HardCodeRowA()

'\ This rountine works.

Dim lRowValue As Long
Dim rnKeyRange As Range

    lRowValue = ActiveCell.Row

    ActiveSheet.Range(Cells(lRowValue, 1), Cells(lRowValue, 8)).Copy

    Cells(20, 1).PasteSpecial Paste:=xlPasteValues

    Application.CutCopyMode = False

End Sub

 

 

 

Sub HardCodeRowB()

'\ This rountine fails.

Dim lRowValue As Long
Dim rnKeyRange As Range

    Set rnKeyRange = Sheets("Sheet1").Range("Table1")

    lRowValue = ActiveCell.Row

    rnKeyRange(Cells(lRowValue, 1), Cells(lRowValue, 8)).Copy
    '\ Although I get no errors when I single step through, the routine_
    '\ does not seem to copy.

    Cells(20, 1).PasteSpecial Paste:=xlPasteValues

    Application.CutCopyMode = False

End Sub

 

 

Can someone please comment?

 

Also, if there is a more elegant solution using Cells, please share that solution with me too.

 

Thank you for your help.

6 Replies

@KStecyk 

In rnKeyRange(Cells(lRowValue, 1), Cells(lRowValue, 8)), Cells(lRowValue), 1) acts as the row number and Cells(lRowValue, as the column number. Since the numbers in your table are quite high, rnKeyRange(Cells(lRowValue, 1), Cells(lRowValue, 8)) refers to a cell far outside the table.

That is obviously not what you want.

You could use

 

rnKeyRange.Range(Cells(lRowValue - 1, 1), Cells(lRowValue - 1, 8)).Copy

 

I used lRowValue - 1 since rnKeyRange is the data range; it starts in row 2.

 

Here is an alternative version of the macro:

 

Sub HardCodeRowC()
    Dim lRowValue As Long
    lRowValue = ActiveCell.Row
    Cells(20, 1).Resize(1, 8).Value = Cells(lRowValue, 1).Resize(1, 8).Value
End Sub
Best Response confirmed by KStecyk (Contributor)
Solution

@KStecyk 

 

Try this:

Sub macro2()
     
     If ActiveSheet.Name <> Sheet1.Name Then
          Exit Sub
     End If
     
     With Sheet1.ListObjects("Table1")
          If Not Intersect(ActiveCell, .DataBodyRange) Is Nothing Then
               With Intersect(ActiveCell.EntireRow, .DataBodyRange)
                    Sheet1.Range("A20").Resize(1, .Columns.Count).Value = .Value
               End With
          End If
     End With
     
End Sub

 

That line of HardCodeB does not work because it is expecting a reference in the format rnKeyRange(Row, Col) and you are giving it range references. For example, rnKeyRange(1,1) is a reference to cell "A2" (the top left cell of the rnKeyRange "A2:H16").

 

Range references are relative to your starting range. Since your lRowValue is not relative to the Range rnKeyRange, but is relative to the worksheet, you should be able to use

Sheet1.Range(Sheet1.Cells(lRowValue, 1), Sheet1.Cells(lRowValue, 8)).Copy

 

or

With rnKeyRange.Worksheet
.Range(.Cells(lRowValue, 1), .Cells(lRowValue, 8)).Copy
End With

 

It is a generally good practice to qualify your Cells reference for situations in which your macro may not be running on the active sheet. For example, Sheet1.Range(Cells(1,1), Cells(1,10)) - the Range object refers to Sheet1, but the Cells objects still refer to the active sheet. Not really necessary in this case since I'm testing to ensure the activesheet is Sheet1 before proceeding, but I do it anyway.

@Hans Vogelaar 

 

In rnKeyRange(Cells(lRowValue, 1), Cells(lRowValue, 8)), Cells(lRowValue), 1) acts as the row number and Cells(lRowValue, as the column number. Since the numbers in your table are quite high, rnKeyRange(Cells(lRowValue, 1), Cells(lRowValue, 8)) refers to a cell far outside the table.

That is obviously not what you want.

 

I believe that I provided a range reference and it was expecting a row and column. Please see JMB17's response. If I choose F7, then lRowValue is 7.

 

You could use

 

rnKeyRange.Range(Cells(lRowValue - 1, 1), Cells(lRowValue - 1, 8)).Copy

 

I used lRowValue - 1 since rnKeyRange is the data range; it starts in row 2.

 

Here is an alternative version of the macro:

 

 

Sub HardCodeRowC()
    Dim lRowValue As Long
    lRowValue = ActiveCell.Row
    Cells(20, 1).Resize(1, 8).Value = Cells(lRowValue, 1).Resize(1, 8).Value
End Sub

 


 

Both of those two solutions work well. And you answered my concern of how to modify my code to make it work. I understand why you subtracted 1 from lRowValue.

 

Thank you for your help.

@JMB17 

 

Thank you for taking the time and effort to provide a comprehensive answer.

I understand that I provided a range reference. It's interesting that I did not get an error message.

I modified your following line:

 

Sheet1.Range(Sheet1.Cells(lRowValue, 1), Sheet1.Cells(lRowValue, 8)).Copy

 

to the following:

 

Sheet1.Range(Cells(lRowValue, 1), Cells(lRowValue, 8)).Copy

 

I believe you referenced this in your final comments by saying that it is generally good practice to qualify your Cells reference. Cells could be referencing the activesheet, which might be different than Sheet1. As I was typing this response to you, I just appreciated the meaning of your comment. Okay, so I should go back to your original for reasons just discussed.

 

I like your macro2. I had to study it to understand it. I like your error checking and your use of Intersect, Resize, and Columns.Count. And I like the nested With statements.

 

I will adapt your macro2 to my situation.

 

I appreciate your help.

 

You're welcome.

In this particular case, you don't have to qualify the Cells reference because the macro first tests to ensure the activesheet is Sheet1 - so it is certain to be referencing the correct sheet and will work either way.

I pointed it out just so that you are aware of it. When I first started learning vba, it was something I overlooked at first on one of my first larger project and spent a fair amount of time trying to debug.

@JMB17 

 

Thank you for your additional comments. It's always helpful learning how to do think ahead.

 

Kevin