I have the following code, but for some reason the msgbox krow does not seem to work. There is a match but it just shows error. I don't understand why. Help is appreciated, thanks!
Sub addsheet()
Dim lrow As Variant, krow As Variant
Dim i As Long, lastcol As Long, lastrow As Long, lastrowcomp As Long
Dim sheetname As String, sheetname2 As String
Sheets("Main Sheet").Activate
lastcol = Cells(2, Columns.Count).End(xlToLeft).Column
lrow = Application.Match(Sheets("Main Sheet").Range("F6").Value, Sheets("Main Sheet").Range(Cells(2, 14), Cells(2, lastcol)), 0)
If IsError(lrow) Then
MsgBox "Please Select a value under change"
ElseIf lrow > 0 Then
lastrow = Sheets("Main Sheet").Cells(Rows.Count, lrow + 13).End(xlUp).Row
lastrowcomp = Sheets("Comparison Check").Range("A" & Rows.Count).End(xlUp).Row
For i = 3 To lastrow
krow = Application.Match(Sheets("Main Sheet").Cells(i, lrow).Value, Sheets("Comparison Check").Range("A3:A" & lastrowcomp), 0)
MsgBox krow
'If IsError(krow) Then
'sheetname = Sheets("Main Sheet").Cells(i, lrow + 13).Value
'Worksheets.Add(After:=Worksheets(1)).Name = sheetname
'ThisWorkbook.Sheets("Sheet1").UsedRange.Copy
'ThisWorkbook.Sheets(sheetname).Select
'ThisWorkbook.Sheets(sheetname).Range("A1").Select
'ThisWorkbook.Sheets(sheetname).Paste
'ThisWorkbook.Sheets(sheetname).Cells.Interior.ColorIndex = 2
'Else
If krow > 0 Then
sheetname2 = Sheets("Comparison Check").Cells(krow, 1).Value
Sheets(sheetname2).Activate
End If
Next i
End If
End Sub
Basically, I am trying to create a new sheet with the name if there is no match and go to the existing sheet if the match exists. but when there is a match it somehow keeps creating a new sheet. Hence, why I added the msgbox krow to confirm the issue was with the matching.
Next iand oneEnd If.