VBA- Adding source and destination cells together rather than overwriting

Renx10

New Member
Joined
Jul 20, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have muddled together this piece of VBA that copies a cell range from 1 sheet, then pastes it into another multiple times depending on the number in a different cell. the pastes run concurrently 1 column apart
The issue I have is that the paste is overwriting the data in destination cell rather than adding together.
can anyone see where its going wrong?

Sub CopyAndVlookupWithErrors()
On Error Resume Next

Dim wsSource As Worksheet
Dim wsDestination As Worksheet
Dim lookupRange As Range
Dim pasteColumn As Long
Dim i As Integer

' Set the source and destination worksheets
Set wsSource = ThisWorkbook.Sheets("search2")
Set wsDestination = ThisWorkbook.Sheets("Search2results")

' Copy the range from source to destination
wsSource.Range("D4:J29").Copy

' Set the lookup range and perform VLOOKUP
Set lookupRange = ThisWorkbook.Sheets("Sheet13").Range("A:B")
pasteColumn = Application.WorksheetFunction.Match(wsSource.Range("C1").Value, lookupRange.Columns(1), 0)

' Check for errors in the VLOOKUP result
If Err.Number = 0 Then
' Paste the copied range multiple times with 1 column apart
For i = 0 To ThisWorkbook.Sheets("search form").Range("G3").Value - 1
wsDestination.Cells(2, pasteColumn + i * 1).PasteSpecial Paste:=xlPasteValues
Next i
Else
MsgBox "VLOOKUP Error: Value not found."
End If

On Error GoTo 0
End Sub

Thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Would you please tell us in words what you're attempting to do.
Trying to read all this code is hard to understand what you're attempting to do.
Please provide sheet names and copy what range and paste into what range.
 
Upvote 0
Sorry,
I am wanting to copy cells D4:J29 in "search2" and then paste them into "searchresults2" with the column being determined via a Vlookup
When the data is copied, G3 in "search form" determines how many times the copied data is pasted concurrently, 1 column apart. e.g if G3 = 5 it performs the paste 5 times.

As both the copied and destination contain numbers when the paste is performed I want the result to be a sum of the 2 numbers rather than just pasting over the current number, which is what the current VBA does
 
Upvote 0
1. You said: in "search2"
Does this mean a sheet named "search2"

2.You said: paste them into "searchresults2"
Does this mean a sheet named "searchresults2"
And other things are hard for me to understand
 
Upvote 0
This part of the code doesn't make sense to me:
VBA Code:
    ' Set the lookup range and perform VLOOKUP
    Set lookupRange = ThisWorkbook.Sheets("Sheet13").Range("A:B")
    pasteColumn = Application.WorksheetFunction.Match(wsSource.Range("C1").Value, lookupRange.Columns(1), 0)

It saying its a VLookup but although it sets the lookup range as 2 column A:B it is
a) Only doing a Match on column A so not returning a value from B
b) By using a Match it is only returning a row number in the sheet which for some reason is being used a column number for the output.

Accepting the above at face value and assuming you want to copy the output side by side see if these changes to your code do the trick:
Rich (BB code):
Sub CopyAndVlookupWithErrors()
    On Error Resume Next
    
    Dim wsSource As Worksheet
    Dim wsDestination As Worksheet
    Dim lookupRange As Range
    Dim pasteColumn As Long
    Dim i As Integer
    Dim NoOfColumns As Long
    
    ' Set the source and destination worksheets
    Set wsSource = ThisWorkbook.Sheets("search2")
    Set wsDestination = ThisWorkbook.Sheets("Search2results")
    
    ' Copy the range from source to destination
    With wsSource.Range("D4:J29")
        NoOfColumns = .Columns.Count
        .Copy
    End With
    
    ' Set the lookup range and perform VLOOKUP
    Set lookupRange = ThisWorkbook.Sheets("Sheet13").Range("A:B")
    pasteColumn = Application.WorksheetFunction.Match(wsSource.Range("C1").Value, lookupRange.Columns(1), 0)
    
    ' Check for errors in the VLOOKUP result
    If Err.Number = 0 Then
        ' Paste the copied range multiple times with 1 column apart
        For i = 0 To ThisWorkbook.Sheets("search form").Range("G3").Value - 1
            wsDestination.Cells(2, pasteColumn + i * NoOfColumns).PasteSpecial Paste:=xlPasteValues
        Next i
    Else
        MsgBox "VLOOKUP Error: Value not found."
    End If
    
    On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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