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
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