Run-time error 9 Subscript out of range Compare program not working

vba317

Board Regular
Joined
Oct 7, 2015
Messages
58
I have tried researching Microsoft's help and googling different boards but I have not been able to resolve this pesky issue. What I am doing is copying two workbooks onto the same worksheet. After the copying is compete I compare the results of the two workbooks and if I get any cells that don't match I need to post those results on another worksheet. I was testing the code and it failed on the 24th worksheet, I don't know why. I would appreciate any help.

strSrcRange = A3:AE4
strCompRange = BA3:CE4
lngRow = 3
lngCol = 5
LBound(varSrc,1 ) = 1 UBound(varComp,1 = 2
LBound(varsrc,2) = 1, UBound(varComp,2) =31

I have highlighted the line that errors in Red

Both varSrc and varComp are out of range

Rich (BB code):
        'Original
        varSrc = wrkbk.Worksheets(lngShCnt).Range(strSrcRange)
        varComp = wrkbk.Worksheets(lngShCnt).Range(strCompRange)
Stop
        For lngRow = LBound(varSrc, 1) To UBound(varComp, 1)
            For lngCol = LBound(varSrc, 2) To UBound(varComp, 2)
'                'Define ranges of Source and Compare ranges
                If varSrc(lngRow, lngCol) <> varComp(lngRow, lngCol) Then
'                'Count the amount of errors
                lngErrCnt = 1 + lngErrCnt
'                Worksheets("Missing Tables").Columns("A:D").AutoFit
'                'Change Highlight cell in Source table with error as Red
                lngRow = lngRow + 2
                wrkShSrc.cells(lngRow, lngCol).Interior.ColorIndex = 3
'                'Change Highlight cell in Compare table with error as Red
                If lngLastColSrc <= 26 Then lngCompLastCol = lngCol + 26
                If lngLastColSrc > 26 Then lngCompLastCol = lngCol + 52
              wrkShComp.cells(lngRow, lngCompLastCol).Interior.ColorIndex = 3
             Application.ScreenUpdating = True
'                'Count the amount of errors
                lngErrCnt = 1 + lngErrCnt
                Worksheets("Missing Tables").Columns("A:C").AutoFit
'                'Change Highlight cell in Comparison table with error as Red
''                wrkShSrc.cells(lngRow, lngCol).Interior.ColorIndex = 3
'                'Change Highlight cell in Compare table with error as Red
''                lngCompLastCol = lngCol + 26
'              wrkShComp.cells(lngRow, lngCompLastCol).Interior.ColorIndex = 3
'                'Define Sheet Name
                 strSheet = wrkShSrc.Name
                    With wrkShOutput
'                        'Sheet Name with errors
'                        'Determine Last Row of the Missing Worksheet
                        If lngErrCnt = 1 Then lngLastRowOutput = wrkShOutput.Range("A" & Rows.Count).End(xlUp).Row + 1
                        .cells(lngLastRowOutput, 1).Value = strSheet
'                        'Display the sheet number
                        .cells(lngLastRowOutput, 2).Value = lngShCnt
'                        'Get the Current Count of errors
                        .cells(lngLastRowOutput, 3).Value = lngErrCnt
                        'AutoFit Columns
                       Sheets("Missing Tables").Range("A:E").EntireColumn.AutoFit
                       Sheets("Missing Tables").Columns("B").ColumnWidth = 14
                    End With
                    With wrkShOutput.cells(lngLastRowOutput, 1).Font
                        .Name = "Arial"
                        .Size = 10
                        .FontStyle = "Regular"
                    End With
                    With wrkShOutput.cells(lngLastRowOutput, 2).Font
                        .Name = "Arial"
                        .Size = 10
                        .FontStyle = "Regular"
                    End With
                     With wrkShOutput.cells(lngLastRowOutput, 3).Font
                        .Name = "Arial"
                        .Size = 10
                        .FontStyle = "Regular"
                    End With
                    wrkShOutput.Columns("A:C").cells.HorizontalAlignment = xlHAlignLeft
                Else
            End If
        Next lngCol
    Next lngRow
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
The highlighted code works fine for me when using the specified ranges.
 
Upvote 0
After more research I discovered that on failures I needed to reset the row and column counts back to where they were before I added rows and columns to the counts so the correct cell will highlight with red. BTW I thought I did use code tags.
Code:
 and the beginning and
at the end correct?
 
Upvote 0

Forum statistics

Threads
1,215,475
Messages
6,125,028
Members
449,205
Latest member
Eggy66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top