I have made a macros having multiple subs. In one of the subs I am getting the give error. Before this sub, the code is running fine but in this sub as the active sheet switches to Sheet 2, I am getting the error in the specified line.
Sub Matchr()
counter = 0
Dim k As Integer
Sheets("Sheet2").Select
k = Sheet2.UsedRange.Rows.Count + 5 '<-- This line is throwing the error.
Set S1 = Worksheets("Sheet1")
Set S2 = Worksheets("Sheet2")
For i = 7 To S2.UsedRange.Rows.Count
For J = 7 To S1.UsedRange.Rows.Count
If Sheet2.Cells(i, "J") = Sheet1.Cells(J, "J") Then
counter = 1
End If
Next J
If counter = 0 Then
Sheet2.Cells(k, "I") = Sheet2.Cells(i, "I")
Sheet2.Cells(k, "J") = Sheet2.Cells(i, "J")
Sheet2.Cells(k, "K") = Sheet2.Cells(i, "K")
k = k + 1
End If
counter = 0
Next i
Range("I45:I58").Select
Selection.NumberFormat = "[$-409]d-mmm-yy;@"
Range("J63").Select
End Sub
Note that the given code is working fine when I am running it making a button for the macro. I am getting the error while running in Personal XLSB file.