-1

I have the following VBA code to compare the content (string) between two cells. If it is the same, I must to copy certain cells and paste them in another sheet. However, this code is not working. Please may you advise on how to adjust it?

Dim p As Integer
Dim i As Integer

For i = 12 To RealLastRow
If Worksheets("Pal_clave").Range("V" & i).Value = Worksheets("Pal_clave").Range("V" & i - 1).Value Then

Worksheets("Pal_clave").Range("D" & i).Copy Worksheets("Diagrama").Range("B" & p + 10)

Worksheets("Pal_clave").Range("K" & i).Copy Worksheets("Diagrama").Range("E" & p + 10)

Worksheets("Pal_clave").Range("T" & i).Copy Worksheets("Diagrama").Range("H" & p + 10)

Worksheets("Pal_clave").Range("V" & i).Copy Worksheets("Diagrama").Range("K" & p + 10)

Worksheets("Pal_clave").Range("AB" & i).Copy Worksheets("Diagrama").Range("N" & p + 10)

Worksheets("Pal_clave").Range("AJ" & i).Copy Worksheets("Diagrama").Range("B" & p + 20)

Worksheets("Pal_clave").Range("Y" & i).Copy Worksheets("Diagrama").Range("K" & p + 20)

p = p + 20

End If

Next i
Vityata
  • 42,633
  • 8
  • 55
  • 100

2 Answers2

1

It may be that some of your Subroutine is missing, but you do not define RealLastRow. You can shorten your code by setting your long worksheet names as variables and avoiding copy/paste. It is best not to use Select and Activate as used in the previous answer.

Sub LoopFor()

    'Use Long in case there are greater than 32767 rows
    Dim p As Long
    Dim i As Long
    Dim RealLastRow As Long
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet

    'Makes your code shorter
    Set ws1 = ThisWorkbook.Sheets("Pal_clave")
    Set ws2 = ThisWorkbook.Sheets("Diagrama")

    'This will get you the last row, even if there are gaps in the data
    RealLastRow = ws1.Cells(Rows.Count, 1).End(xlUp).Row

    'You may want to test if there are more than 12 rows
    For i = 12 To RealLastRow
        If ws1.Range("V" & i).Value = ws1.Range("V" & i - 1).Value Then

            'No need to copy/paste
            ws2.Range("B" & p + 10) = ws1.Range("D" & i)
            ws2.Range("E" & p + 10) = ws1.Range("K" & i)
            ws2.Range("H" & p + 10) = ws1.Range("T" & i)
            ws2.Range("K" & p + 10) = ws1.Range("V" & i)
            ws2.Range("N" & p + 10) = ws1.Range("AB" & i)
            ws2.Range("B" & p + 20) = ws1.Range("AJ" & i)
            ws2.Range("K" & p + 20) = ws1.Range("Y" & i)

            p = p + 20

        End If

    Next i

End Sub
Darrell H
  • 1,876
  • 1
  • 9
  • 14
0

As per my understanding, you have input sheet1 like this and you want compare the cells in A and B columns. If the string matches, copy the particular cell values from sheet1 and paste them into sheet2.

You have to use a For loop and if condition to achieve this.

Try this below code.

Sub CompareAndCopy()

Dim NumberOfValues, i, j As Integer
Dim value1, value2 As String
j = 2

Sheet1.Activate
NumberOfValues = Sheets("Sheet1").Range("A1").End(xlDown).Row

For i = 1 To NumberOfValues

value1 = Range("A" & i).Value
value2 = Range("B" & i).Value

'Comparing the cell values in A and B column
'If value matches then copy and paste them into sheet2 from sheet1

If value1 = value2 Then   
Worksheets("Sheet1").Range("D" & i & ":H" & i).Copy Destination:=Worksheets("Sheet2").Range("A" & j)
Sheet1.Activate
j = j + 1
End If

Next

End Sub
Nandan A
  • 2,702
  • 1
  • 12
  • 23