VBA not working - Copy Sheet data from 1 sheet to other sheet in same workbook

airoboml

New Member
Joined
Aug 5, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi Team ,

Basically i need to copy the rows and cols which are not fixed, so it can increase daily from one sheet to another sheet in the same workbook.

I am getting error in the end as "Run-time error '5' Invalid procedure call or argument, can you please help.


Below is the vba code:

Sub consolidate()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim wsAll As Worksheet
Set wsAll = wb.Worksheets("All")
Dim ws1 As Worksheet
Set ws1 = wb.Worksheets("S1")
Dim lastRowAll As Long
Dim lastRow1 As Long
Dim lastCol1 As Long


lastRowAll = wsAll.Cells(wsAll.Rows.Count, "A").End(xlUp).Row + 1
lastRow1 = ws1.Range("A1").End(xlDown).Row
lastCol1 = ws1.Range("A1").End(xlToRight).Column
ws1.Activate

Cells(lastRow1, lastCol1).Copy

wsAll.Activate
ActiveSheet.Cells("A" & lastRowAll).Select
Application.CutCopyMode = False


MsgBox ("Job Complete!!")
End Sub


Thanks,
SK
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Is there a copy of your Workbook that you can share?
 
Upvote 0
Hi Team ,

Basically i need to copy the rows and cols which are not fixed, so it can increase daily from one sheet to another sheet in the same workbook.

I am getting error in the end as "Run-time error '5' Invalid procedure call or argument, can you please help.


Below is the vba code:

Sub consolidate()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim wsAll As Worksheet
Set wsAll = wb.Worksheets("All")
Dim ws1 As Worksheet
Set ws1 = wb.Worksheets("S1")
Dim lastRowAll As Long
Dim lastRow1 As Long
Dim lastCol1 As Long


lastRowAll = wsAll.Cells(wsAll.Rows.Count, "A").End(xlUp).Row + 1
lastRow1 = ws1.Range("A1").End(xlDown).Row
lastCol1 = ws1.Range("A1").End(xlToRight).Column
ws1.Activate

Cells(lastRow1, lastCol1).Copy

wsAll.Activate
ActiveSheet.Cells("A" & lastRowAll).Select
Application.CutCopyMode = False


MsgBox ("Job Complete!!")
End Sub


Thanks,
SK
Excel attached in the link, unable to find option to upload excel
 
Upvote 0

Attachments

  • Source Range and Sheet.PNG
    Source Range and Sheet.PNG
    5.1 KB · Views: 7
  • Destination Sheet.PNG
    Destination Sheet.PNG
    4 KB · Views: 7
Upvote 0
Please post code between code tags (vba button on posting toolbar) to maintain indentation and readability.
Try ActiveSheet.Range("A" & lastRowAll).Select
It is usually not a good idea to activate and select sheets or ranges because they can change as the code progresses or you make mistakes in thinking which is the active sheet. Also it is seldom necessary and tends to slow code down over large ranges or loops. Especially true if you've set the sheets or ranges as objects (which you have) - you might as well refer to the object variables. F'rinstance, this should be enough:
ws1.Cells(lastRow1, lastCol1).Copy

EDIT - I will have to review all of this later. I had my post holding in limbo for about 3 hours because it would not commit when I clicked on the submit button.
 
Upvote 0
This line
VBA Code:
ActiveSheet.Cells("A" & lastRowAll).Select
should be Range not Cells
Rich (BB code):
ActiveSheet.Range("A" & lastRowAll).Select

You also aren't pasting anything in the code (you're only selecting the cell)

You do also realise that the line below is only copying s single cell?
VBA Code:
Cells(lastRow1, lastCol1).Copy

You probably mean something more like
VBA Code:
Sub consolidate()
    Dim wb As Workbook
    Set wb = ThisWorkbook
    Dim wsAll As Worksheet
    Set wsAll = wb.Worksheets("All")
    Dim ws1 As Worksheet
    Set ws1 = wb.Worksheets("S1")
    Dim lastRowAll As Long
    Dim lastRow1 As Long
    Dim lastCol1 As Long


    lastRowAll = wsAll.Cells(wsAll.Rows.Count, "A").End(xlUp).Row + 1
    lastRow1 = ws1.Range("A1").End(xlDown).Row
    
    lastCol1 = ws1.Range("A1").End(xlToRight).Column


    Range(ws1.Cells(1, "A"), ws1.Cells(lastRow1, lastCol1)).Copy wsAll.Range("A" & lastRowAll)
   

    MsgBox ("Job Complete!!")
End Sub
 
Last edited:
Upvote 1
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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