I have just started working on VBA, and I'm stuck with this error while copying data between different sheets.
The requirement was to find specific block of data points and copy them on a separate sheet.
Variable like FirstRowArrayCol1 Contain the position of Data points, whereas i denotes the column numbeer.
The error is coming in this line
Set rngDest = wksDest.Range(Cells(i, firstPos))
Please check the below code. Greatly Appreciate any help, thanks.
Set wksSource = ActiveWorkbook.Sheets("Sheet1")
Set wksDest = ActiveWorkbook.Sheets("Sheet2")
For Each firstPos In FirstRowArrayCol1
If firstPos = 0 Then
Exit For
End If
For Each secondPos In SecondRowArrayCol1
If secondPos = 0 Then
Exit For
End If
Diff = Abs(firstPos - secondPos)
If Diff > 0 And Diff <= 5 Then
Debug.Print (column3 & firstPos & "," & column3 & secondPos & "," & column4 & firstPos & "," & column4 & secondPos)
'Copy Data
Set rngSource = wksSource.Range(Cells(i, firstPos), Cells(i + 1, secondPos))
rngSource.Range(Cells(i, firstPos), Cells(i + 1, secondPos)).Select
rngSource.Copy
'Paste Data Values
Set rngDest = wksDest.Range(Cells(i, firstPos))
rngDest.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
Next secondPos
For Each secondPos In SecondRowArrayCol2
If secondPos = 0 Then
Exit For
End If
Diff = Abs(firstPos - secondPos)
If Diff > 0 And Diff <= 5 Then
Debug.Print (column3 & firstPos & "," & column3 & secondPos & "," & column4 & firstPos & "," & column4 & secondPos)
'Copy Data
Set rngSource = wksSource.Range(Cells(i, firstPos), Cells(i + 1, secondPos))
rngSource.Range(Cells(i, firstPos), Cells(i + 1, secondPos)).Select
rngSource.Copy
'Paste Data Values
Set rngDest = wksDest.Range(Cells(i, firstPos))
rngDest.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
Next secondPos
Next firstPos
Range