How to automatically paste the data from one sheet to another using VBA

CALEB23

New Member
Joined
Oct 1, 2024
Messages
4
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
  2. Web
Hello All.

I created a macro wherein the data captured from SAP and imported via power query will automatically pasted on another sheet of the same worksheet, what happened is that even though the query is updated once I ran the macro it captured the previous data generated from power query. What will be the correct script to capture the correct data?

I appreciate any help you can provide.

1728288946296.png

PnL sheet - exported data from SAP and link to the worksheet using power query
PnL Bal - sheets wherein the data from PnL sheet be pasted

Here is my sample script.

VBA Code:
#If VBA7 Then ' Excel 2010 or later
 
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
 
#Else ' Excel 2007 or earlier
 
    Public Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)

   
#End If

Sub PnL_Update()

    Sheets("PnL").Visible = True
    Sheets("PnL").Select
    ActiveWorkbook.RefreshAll

    DoEvents
    Sleep 10000

    Sheets("PnL").Select
    Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("PnL Bal").Select
    Dim lastrow As Long
    lastrow = Range("A" & Rows.Count).End(xlUp).Row + 1
    Range("A" & lastrow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Sleep 10000
   
    Sheets("PnL Bal").Activate
    Worksheets("PnL").Visible = False

End Sub
 
Last edited by a moderator:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Here is my sample script.
For the future, when doing so please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,224,803
Messages
6,181,055
Members
453,014
Latest member
Chris258

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