I use Excel 2016 Office 365 and I have been trying to fix why my code has a Run time error 9 - Subscript Out of Range Error on this line of code:
I'm having the code to compare the data in column A between 2 workbooks, wkorig (the origin workbook, which is in a folder on the desktop called MRFolder), and wkdest (the destination workbook), and wherever there is a match in values to highlight the row in the destination workbook. It worked once and just highlighted the first row, now I get this error.
I'm trying to learn more about VBA and I don't see why I get that error b/c the code works to open the destination workbook, and I am just asking it to look on that active sheet and highlight the row if there is a match.
I've changed this code to say
and
, but they cause the same Run Time 9 error but it highlights the
in the beginning.
Does someone know how to fix this type of error? Thanks.
Code:
ActiveSheet.Range(Cells(i, "A"), Cells(i, lastcoldest)).Interior.ColorIndex = RGB(255, 242, 204)
I'm having the code to compare the data in column A between 2 workbooks, wkorig (the origin workbook, which is in a folder on the desktop called MRFolder), and wkdest (the destination workbook), and wherever there is a match in values to highlight the row in the destination workbook. It worked once and just highlighted the first row, now I get this error.
I'm trying to learn more about VBA and I don't see why I get that error b/c the code works to open the destination workbook, and I am just asking it to look on that active sheet and highlight the row if there is a match.
I've changed this code to say
Code:
Sheets(3). Range(Cells, etc.)
Code:
ThisWorkbook.Sheets(3).Range(Cells, etc.)
Code:
lastrowdest = Sheets(3), etc.
Does someone know how to fix this type of error? Thanks.
Code:
Option Explicit
Sub highlightrow()
Dim wkdest As Workbook
Dim wkorig As String
Dim i As Long, j As Long, lastrowdest As Long, lastroworig As Long, lastcoldest As Long
Dim criteria As String
Dim pathstr As String
Set wkdest = ThisWorkbook
lastrowdest = Sheets(3).Range("A" & Rows.Count).End(xlUp).Row
lastcoldest = Sheets(3).Cells(2, Columns.Count).End(xlToLeft).Column
'gets filename from path
wkorig = Dir("C:\Users\PC-1\Desktop\MRFolder\AAA_data.xlsm")
pathstr = "C:\Users\PC-1\Desktop\MRFolder\"
Application.ScreenUpdating = False
'This gets criteria, node name, from wkdest workbook
For i = 2 To lastrowdest
criteria = wkdest.Sheets(3).Cells(i, "A").Value
'This is to just get row count for wkorig file, AAA_data.xlsm
If Len(wkorig) = 0 Then
Exit Sub
Else
Workbooks.Open Filename:=pathstr & "\" & wkorig
Sheets(1).Activate
lastroworig = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
End If
For j = 2 To lastroworig
If Sheets(1).Cells(i, "A").Value = criteria Then
'activate the destination workbook in order to highlight the row where the values match
wkdest.Sheets(3).Activate
ActiveSheet.Range(Cells(i, "A"), Cells(i, lastcoldest)).Interior.ColorIndex = RGB(255, 242, 204)
End If
Next j
Next i
Application.ScreenUpdating = True
End Sub