Copy/paste macro

Barrakooda

Board Regular
Joined
Feb 3, 2012
Messages
75
Hi all

Bit stumped & seeking assistance, vba isnt my thing.

I have a work book with several sheets in it. I am trying to copy the data from M2 on each sheet & paste it into Column C but in the next blank cell.

At the end of running the macro i would like to refresh a pivot table = PivotTable1

Help would be appreciated

So far i have

Code:
Sub RefreshGraphs()
'
' RefreshGraphs Macro
'


'
    Sheets("PIVOT").Select
    Range("D10").Select
    ActiveSheet.PivotTables("PivotTable3").PivotCache.Refresh
    Sheets("MTD Trending").Select
End Sub

&

Code:
Worksheets("TRUCKS").Range("M2").Copy _
        Destination:=Worksheets("TRUCKS").Cells(Worksheets("TRUCKS").Rows.Count, "C").End(xlUp).Offset(1, 0)


Worksheets("EX2600").Range("M2").Copy _
        Destination:=Worksheets("EX2600").Cells(Worksheets("EX2600").Rows.Count, "C").End(xlUp).Offset(1, 0)


Worksheets("WA500").Range("M2").Copy _
        Destination:=Worksheets("WA500").Cells(Worksheets("WA500").Rows.Count, "C").End(xlUp).Offset(1, 0)
        
Worksheets("WA600").Range("M2").Copy _
        Destination:=Worksheets("WA600").Cells(Worksheets("WA600").Rows.Count, "C").End(xlUp).Offset(1, 0)


Worksheets("WA600LC").Range("M2").Copy _
        Destination:=Worksheets("WA600LC").Cells(Worksheets("WA600LC").Rows.Count, "C").End(xlUp).Offset(1, 0)


Worksheets("992K").Range("M2").Copy _
        Destination:=Worksheets("992K").Cells(Worksheets("992K").Rows.Count, "C").End(xlUp).Offset(1, 0)


Worksheets("16M").Range("M2").Copy _
        Destination:=Worksheets("16MK").Cells(Worksheets("16M").Rows.Count, "C").End(xlUp).Offset(1, 0)


Worksheets("D10T").Range("M2").Copy _
        Destination:=Worksheets("D10T").Cells(Worksheets("D10T").Rows.Count, "C").End(xlUp).Offset(1, 0)
 
Can you post the sheet or a screendump (with blurred data) with the result of the macro and how it should behave?
The table doesn't get expanded, right?
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi Roodey

unfortunately cant link to pic or know how to upload from work PC

However what it is doing is putting the value into C33 instead of C2 (C1 has a header). By putting it into C33 it has added another row to the table.
 
Last edited:
Upvote 0
Okay, because firstrow and lastrow both give back 33 I assume cell C2 is not empty.
Do you want it to be overwritten then? Is it always C2?
 
Upvote 0
I cannot help you with seeing the sheet and what your expectations are.

The code below reads the data in your table, table named Table1.

Code:
Sub Temp()

Dim tbl As ListObjectSet tbl = ActiveSheet.ListObjects("Table1")
MsgBox tbl.Range.Rows.Count
MsgBox tbl.HeaderRowRange.Rows.Count
MsgBox tbl.DataBodyRange.Rows.Count
Set tbl = Nothing

End Sub
 
Last edited:
Upvote 0
Get Compile error: Syntax error on this line

Code:
Dim tbl As ListObjectSet tbl = ActiveSheet.ListObjects("Table1")

have confirmed that i was on sheet with Table1 on it
 
Upvote 0
Hi Barrakooda

The two lines seem to have run together.

Try:

Code:
Dim tbl As ListObject 
Set tbl = ActiveSheet.ListObjects("Table1")

Cheers

pvr928
 
Upvote 0
Hi pvr928

getting Run-time error '9' subscript out of range

Code:
Sub Temp1()


Dim tbl As ListObject
[COLOR=#ff0000]Set tbl = ActiveSheet.ListObjects("Table1")[/COLOR]
MsgBox tbl.Range.Rows.Count
MsgBox tbl.HeaderRowRange.Rows.Count
MsgBox tbl.DataBodyRange.Rows.Count
Set tbl = Nothing


End Sub
 
Last edited:
Upvote 0
Hi Barrakooda

The error will be occurring because there is not a Table called "Table1" on ActiveSheet.

The fix is to ensure this is the case.

I don't like referring to ActiveSheet, as there is a risk that the ActiveSheet is not the one the code needs to refer to.

I prefer the syntax:

Code:
Set tbl = Sheets("Sheet1").ListObjects("Table1")

Where "Table1" is indeed located on the tab called "Sheet1" [or whatever the name of the tab is in your file].

Cheers

pvr928
 
Upvote 0

Forum statistics

Threads
1,215,107
Messages
6,123,126
Members
449,097
Latest member
mlckr

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