copy to last

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

copy to last

Post by adam »

Hi anyone,

I'm trying to copy the values in column c starting from range C6 until the last data row to cell V6 one by one and the call the macro But my code is not working.

What am I doing wrong in here?

Code: Select all

Sub Copy()
    Dim ws          As Worksheet
    Dim LastRow     As Long
    
    Set ws = Worksheets("contacts")
    
    LastRow = ws.Range("C" & Rows.Count).End(xlUp).Row
    ws.Range("V6").Value = ws.Range("C6:C" & LastRow).Value     
    Call ga
End Sub
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78463
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: copy to last

Post by HansV »

The problem is that ws.Range("V6") is a single cell and ws.Range("C6:C" & LastRow) probably contains multiple cells. You cannot assign a multi-cell range to a single cell.
Do you want to call ga for each cell? If so, you have to write a For ... Next loop.
If not: please explain more clearly what you want.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: copy to last

Post by adam »

I want to call the "ga" each time a cell in column C is copied to V6.

For example when the code is run,
cells of column C will get copied starting from row 6; one by one until the last data row. Each time the cell is copied, it will run the code "ga"
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78463
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: copy to last

Post by HansV »

As I mentioned, you'll have to use a loop:

Code: Select all

Sub Copy()
    Dim ws          As Worksheet
    Dim LastRow     As Long
    Dim c           As Range
    
    Set ws = Worksheets("contacts")
    
    LastRow = ws.Range("C" & Rows.Count).End(xlUp).Row
    For Each c In ws.Range("C6:C" & LastRow)
        ws.Range("V6").Value = c.Value
        Call ga
    Next c
End Sub
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: copy to last

Post by adam »

Here's How I've done it before you posted the code. and its working.

Code: Select all

Sub copy()
      Dim r As Long
    Dim ws          As Worksheet
    Dim LastRow     As Long
    
    Set ws = Worksheets("contacts")
    
    LastRow = ws.Range("C" & Rows.Count).End(xlUp).Row
    For r = 6 To LastRow
        ws.Range("V6").Value = ws.Range("C" & r).Value
        Call ga
    Next r
End Sub
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78463
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: copy to last

Post by HansV »

That works too.
Best wishes,
Hans

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: copy to last

Post by LisaGreen »

It may be a good idea not to call the sub "Copy" as well.

HTH
Lisa