Consulting

Results 1 to 2 of 2

Thread: Object Required Error when Selecting Range

  1. #1
    VBAX Regular
    Joined
    Feb 2018
    Location
    Portland
    Posts
    38
    Location

    Object Required Error when Selecting Range

    Hello,

    The below code works just fine but is including data in column B which I do not want

    Sub ColorMaximumValue()
        Dim rng As Range, cell As Range, Maximum As Double
        Cells.Interior.ColorIndex = 0
        Set rng = Range("A1").CurrentRegion
        'Set rng = Range("A1", Range("A1").End(xlDown)).Select
        Maximum = Application.WorksheetFunction.Max(rng)
        For Each cell In rng
            If cell.Value = Maximum Then
            cell.Interior.ColorIndex = 22
            End If
        Next cell
    End Sub
    RangeError.jpg

    I adjusted the code to be:
    Sub ColorMaximumValue()
        Dim rng As Range, cell As Range, Maximum As Double
        Cells.Interior.ColorIndex = 0
        'Set rng = Range("A1").CurrentRegion
        Set rng = Range("A1", Range("A1").End(xlDown)).Select
        Maximum = Application.WorksheetFunction.Max(rng)
        For Each cell In rng
            If cell.Value = Maximum Then
            cell.Interior.ColorIndex = 22
            End If
        Next cell
    End Sub
    I get "Object Required" Error. I am not sure why as I tested Range("A1", Range("A1").End(xlDown)).Select in the immediate window and it worked just fine. Any ideas? Thanks

  2. #2
    VBAX Regular
    Joined
    Feb 2018
    Location
    Portland
    Posts
    38
    Location
    I figured it out. I just had to take the "Select" keyword off of my code like this:
    Set rng = Range("A1", Range("A1").End(xlDown))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •