excelvba

VBA Copying Issue


I have written a code to copy data from different workbooks into one specific workbook with multiple sheets. This code has been working for a couple of weeks now. Starting from today it is not working anymore:

Sub CopyDatafromWB(strsourceWB As String, strsourceSheet As String, strsourceRange As String, strtargetSheet As String, strtargetRange As String)
    '
    Dim sourceWB As Workbook
    Dim targetWB As Workbook
    Dim sourceSheet As Worksheet
    Dim targetSheet As Worksheet
    Dim previousScreenUpdating As Boolean
    Dim previousDisplayAlerts As Boolean
    
    ' Store the current settings
    previousScreenUpdating = Application.ScreenUpdating
    previousDisplayAlerts = Application.DisplayAlerts
    
    ' Disable screen updating and alerts
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    ' Open the source workbook
    Set sourceWB = Workbooks.Open(strsourceWB, UpdateLinks:=False)
    
    ' Set the target workbook
    Set targetWB = ThisWorkbook
    
    ' Set the source and target sheets
    Set sourceSheet = sourceWB.Sheets(strsourceSheet)
    Set targetSheet = targetWB.Sheets(strtargetSheet)
    MsgBox targetSheet.Name
    
    ' Copy and paste the values
    sourceSheet.Range(strsourceRange).Copy
    targetSheet.Range(strtargetRange).PasteSpecial Paste:=xlPasteValues
    'wb.Sheets("CO Aufträge HF FF").Range("A4:N20").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    
    
    ' Close the source workbook without saving changes
    sourceWB.Close SaveChanges:=False
    
    ' Restore the previous settings
    Application.ScreenUpdating = previousScreenUpdating
    Application.DisplayAlerts = previousDisplayAlerts
    
End Sub

Sub CopyData()
    ' Declare variables
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim strsourceWB As String
    Dim strsourceSheet As String
    Dim strsourceRange As String
    Dim strtargetSheet As String
    Dim strtargetRange As String
    Dim strPath As String
    Dim i As Long
    
    ' Set references to the workbook and "Mapping" sheet
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Mapping")
    
    ' Get the path of the workbook
    strPath = wb.Path & "/"
    
    Application.ScreenUpdating = False
    ' Loop through rows in the "Mapping" sheet
    On Error Resume Next
    For i = 3 To 3
        Err.Clear
        ' Assign the values from the "Mapping" sheet to variables
        strtargetSheet = ws.Cells(i, 2)
        strtargetRange = ws.Cells(i, 3)
        strsourceWB = strPath & ws.Cells(i, 4) & "/" & ws.Cells(i, 5)
        strsourceSheet = ws.Cells(i, 6)
        strsourceRange = ws.Cells(i, 7)
        
        ' Call the subprocedure "CopyDatafromWB" to copy data from source to target
        Call CopyDatafromWB(strsourceWB, strsourceSheet, strsourceRange, strtargetSheet, strtargetRange)
        
        If Err.Number = 0 Then
        ws.Cells(i, 8) = "Success"
        Else
        ws.Cells(i, 8) = "Fail"
        End If
          
    Next i
    ws.Activate
    Application.ScreenUpdating = True
    
    MsgBox "All data has been copied successfully!"
End Sub

After the first instance of the loop the data is not only copied to the targetsheet. But is also copied to the sheet called "Mapping".

I have checked my mapping table. Within this table "Mapping" is not listed as targetrange/targetsheet. I have also tried to process the code with breakpoints to identify the error. But once i set breakpoints for the following lines, the code is getting executed without any issues.

    sourceSheet.Range(strsourceRange).Copy
    targetSheet.Range(strtargetRange).PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False

Unfortunately I am not able to tell where this error comes from: My mapping table has the following structure:

[Example of Table]

enter image description here


Solution

  • A few general comments on debugging:

    On to your specific question.

    When copying/moving/pasting data, storing it in an array (Variant) has a few benefits vs working directly with a Range. The biggest reason people do this is performance benefits, but it's also got debugging applications because you can see more of your data in the Locals window. Here's a rewritten version of your CopyDatafromWB using an array:

    Sub CopyDatafromWB(strsourceWB As String, strsourceSheet As String, strsourceRange As String, strtargetSheet As String, strtargetRange As String)
    
        ' Open the source workbook
        Dim sourceWB As Workbook, sourceSheet As Worksheet
        Set sourceWB = Workbooks.Open(strsourceWB, UpdateLinks:=False)
        Set sourceSheet = sourceWB.Sheets(strsourceSheet)
        
        ' Store data in an array
        Dim sourceArray() As Variant
        sourceArray = sourceSheet.Range(strsourceRange).Value
        
        ' Write the array to the new sheet
        Dim targetSheet As Worksheet
        Set targetSheet = ThisWorkbook.Sheets(strtargetSheet)
        targetSheet.Range(strtargetRange).Value = sourceArray
        
        ' Close the source workbook without saving changes
        sourceWB.Close SaveChanges:=False
        
    End Sub
    

    It's hard to say exactly what is causing your issue. But you did draw attention to the .Copy and .PasteSpecial portions of your code which this will replace.