简体   繁体   中英

Error in VBA excel

my program is giving error

Sub my()

Dim j As Integer
Dim i As Integer
For i = 1 To 8
    For j = 1 To 4
        If Sheet1.Cells(i, 1) <> Sheet2.Cells(j, 1) Then
            Sheet1.Cells(i, 2) = Sheet2.Cells(j, 2)
        End If      
    Next j
Next i

End Sub

it gives an error object required can someone tell me what is wrong in it i am new in VBA

You can try like this:

Dim j As Integer
Dim i As Integer
Dim ws1 As Worksheet
Dim ws2 As Worksheet

Set ws1 = ThisWorkbook.Worksheets("Sheet1")
Set ws2 = ThisWorkbook.Worksheets("Sheet2")

For i = 1 To 8
    For j = 1 To 4
        If ws1.Cells(i, 1) <> ws2.Cells(j, 1) Then
            ws1.Cells(i, 2) = ws2.Cells(j, 2)
        End If
    Next j
Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM