1

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
7
  • FirstRowArrayCol1 etc are these ranges? Commented Mar 23, 2018 at 10:41
  • 2
    @QHarr .. was just typing that :) Commented Mar 23, 2018 at 10:42
  • They are storing row numbers as arrays specifically Commented Mar 23, 2018 at 10:42
  • So they are variants? Commented Mar 23, 2018 at 10:46
  • 2
    I think you have your answer below but as a side note: I wouldn't use array in this instance as your are working directly with the range anyway. So would suggest to use the Range Commented Mar 23, 2018 at 10:51

2 Answers 2

3

The Worksheet.Range Property accepts 2 cells like

Set rngDest = wksDest.Range(Cells(i, firstPos), Cells(i, firstPos))

or one address

Set rngDest = wksDest.Range(Cells(i, firstPos).Address)

but not 1 cells

Set rngDest = wksDest.Range(Cells(i, firstPos)) 'not valid

because therefore you can use Cells directly

Set rngDest = wksDest.Cells(i, firstPos)
Sign up to request clarification or add additional context in comments.

Comments

1

Change Set rngDest = wksDest.Range(Cells(i, firstPos)) to:

Set rngDest = wksDest.Cells(i, firstPos)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.