I can't get copy from one worksheet to another to work

MarcMoolenaar

New Member
Joined
Jul 1, 2024
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
I want to copy five ranges of cells from the active worksheet to sheet of another workbook. Somehow the selected data is not copied and the target ranges remain empty. This is what I did so far:

VBA Code:
Sub KopieerSchutbladInfo()
    Dim sourceWorkbook As Workbook
    Dim sourceWorksheet As Worksheet
    Dim sourceRange As Range
    Dim targetWorkbook As Workbook
    Dim targetWorksheet As Worksheet
    Dim targetRange As Range
    Dim sourceWorkbookPath As String
    Dim targetWorkbookPath As String
    Dim Targetline As Integer

    ' Define the path to the source workbook (that's the actual, already opened workbook)
    Set sourceWorkbook = ThisWorkbook
    Set sourceWorksheet = sourceWorkbook.ActiveSheet

    ' Define the path to the target workbook (this one is closed at this moment in time)
    targetWorkbookPath = "F:\Sales\Schutblad data.xlsx"
    Set targetWorkbook = Workbooks.Open(targetWorkbookPath)
    Set targetWorksheet = targetWorkbook.Sheets("Gegevens")
    Targetline = targetWorksheet.Range("A2").Value

    'Kopieer de gegevens van het schutblad naar het databestand

        ' Copy order data
        Set sourceRange = sourceWorksheet.Range("B4")
        Set targetRange = targetWorksheet.Range("A" & Targetline)
        sourceRange.Copy
        targetRange.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True

    ' Save and close the target workbook
    targetWorkbook.Save
    targetWorkbook.Close

End Sub


it seems simple enough ...
 
Last edited by a moderator:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
If you add the Msgbox what does Targetline return and is it what you expected?

VBA Code:
        Set targetRange = targetWorksheet.Range("A" & Targetline)
        MsgBox Targetline

I also take it that the code resides in sourceWorkbook and not your Personal workbook or any other workbook?
 
Upvote 0
Solution
The TargetLine numbe shown is correct. The code is stored in my Personal workbook, I guess that might cause the problem. Any clue on how to solve this?
 
Upvote 0
I change the "Set sourceWorkbook = ThisWorkbook" to Set sourceWorkbook = ActiveWorkbook" and it's working like a charm! Thanks for the winning hint!
 
Upvote 0
Happy it helped and welcome to the forum (and yes if you have code in the personal workbook then the workbook the code is working on should be the ActiveWorkbook [or a specified workbook], ThisWorkbook refers to the workbook the the code resides in)
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,341
Members
452,638
Latest member
Oluwabukunmi

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