Range Class failed

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have the following code, where I get an error 'Range Class failed' on the line marked 'ERROR ON THIS LINE in the code below.

Can anyone help?


Code:
Public Sub CreateBars(intKPINumber As Integer)

    Dim x As Integer,  y As Integer,  intRows As Integer, intCells As Integer, intCellsDetailed As Integer
    Dim rng As Range
    Dim strMonthValue As String
    Dim dblBarValue As Double
    Dim lngColour As Long    
    
    On Error GoTo ErrorHandler
    
    Select Case intKPINumber
        Case 5
            Sheets("KPI 5 - WBT").Select
            Set rng = Selection.Parent.UsedRange
            strMonthValue = Sheets("KPI 5 - WBT").Cells(rng.Rows.Count, 1).Value
            dblBarValue = Sheets("KPI 5 - WBT").Cells(rng.Rows.Count, 4).Value
            intCells = 225 - (100 * dblBarValue / 5)
            intCellsDetailed = 110 - (100 * dblBarValue / 5)
            
            If dblBarValue <= 3.625 Then
                lngColour = vbRed
            ElseIf dblBarValue > 3.625 And dblBarValue <= 3.875 Then
                lngColour = vbYellow
            ElseIf dblBarValue > 3.875 Then
                lngColour = vbGreen
            End If
            
            For y = 2 To 13
                If Sheets("DASHBOARD 2011-12").Cells(124, y).Value = strMonthValue Then
                    Sheets("DASHBOARD 2011-12").Select
                    Range(Cells(intCells, y), Cells(225, y)).Select
                    With Selection
                        .HorizontalAlignment = xlCenter
                        If lngColour = vbRed Then
                            .VerticalAlignment = xlBottom
                        ElseIf lngColour = vbYellow Then
                            .VerticalAlignment = xlCenter
                        ElseIf lngColour = vbGreen Then
                            .VerticalAlignment = xlTop
                        End If
                        .WrapText = False
                        .Orientation = 90
                        .AddIndent = False
                        .IndentLevel = 0
                        .ShrinkToFit = False
                        .ReadingOrder = xlContext
                        .MergeCells = True
                    End With
    
                    ActiveCell.FormulaR1C1 = dblBarValue
                    Selection.NumberFormat = "0.00"
                    
                    Sheets("DETAILED - KPI 5").Select
'ERROR ON THIS LINE Range(Cells(intCellsDetailed, y), Cells(110, y)).Select
                                                                   
                    With Selection
                        .HorizontalAlignment = xlCenter
                        If lngColour = vbRed Then
                            .VerticalAlignment = xlBottom
                        ElseIf lngColour = vbYellow Then
                            .VerticalAlignment = xlCenter
                        ElseIf lngColour = vbGreen Then
                            .VerticalAlignment = xlTop
                        End If
                        .WrapText = False
                        .Orientation = 90
                        .AddIndent = False
                        .IndentLevel = 0
                        .ShrinkToFit = False
                        .ReadingOrder = xlContext
                        .MergeCells = True
                    End With
    
                    With Selection.Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .PatternTintAndShade = 0
                        If lngColour = vbYellow Then
                            .Color = 65535
                            .TintAndShade = 0
                        ElseIf lngColour = vbRed Then
                            .Color = 255
                            .TintAndShade = 0
                        ElseIf lngColour = vbGreen Then
                            .Color = 6750054
                            .TintAndShade = 0
                        End If
                    End With

                    ActiveCell.FormulaR1C1 = dblBarValue
                    Selection.NumberFormat = "0.00"
                    Exit For
                End If
            Next y
    End Select
    
ErrorHandler:
    If Err.Number = 1004 Then
        MsgBox Err.Description
    End If
    
End Sub
 
Aziz

Why not remove the error message now?

If there are any other errors before the one you are looking out for then they won't get reported.

If you insist on keeping it remove the If statement.
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I have removed the error handler as it was always temporary..
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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