Consulting

Results 1 to 5 of 5

Thread: VBA Excel: Ranges

  1. #1

    VBA Excel: Ranges

    Hello,
    is it possible to wirte the three lines in one line without listing all the offsets, but using one range to describe the three offsets?
    Basically I am looking for a way to offset a range of cells like cells(-2, -2), cells(-1, -2)cells(-0, -2) from a base cell.
    Malak

    Sub RangeOffset()
    Dim strBaseCell As String
    strBaseCell = "D10"
    Cells.Interior.ColorIndex = xlColorIndexNone
    Range(strBaseCell).Offset(-2, -2).Cells.Interior.ColorIndex = 41
    Range(strBaseCell).Offset(-1, -2).Cells.Interior.ColorIndex = 41
    Range(strBaseCell).Offset(0, -2).Cells.Interior.ColorIndex = 41
    End Sub

  2. #2
    Perhaps something like this.
    Sub RangeOffset()        
            Dim strBaseCell As String
            strBaseCell = "D10"
            Cells.Interior.ColorIndex = xlColorIndexNone
            Range(strBaseCell).Offset(-2, -2).Resize(3).Interior.ColorIndex = 41
        End Sub

  3. #3
    Or use the "With ..... End With"
    With Range("D10").Offset(-2, -2).Resize(3)
        .Interior.ColorIndex = xlColorIndexNone
        .Interior.ColorIndex = 41
    End With

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Building on the previous two posts...
    Option Explicit
    
    Const myBlue as Variant = 41
    Const myRed As Variant = 3
    
    Sub SamT_VBAX_TestBlue()  
      Cells.Interior.ColorIndex = xlColorIndexNone 
      ColorMe Range("D10"), myBlue
    End Sub
    
    Sub SamT_VBAX_TestRed()  
      Cells.Interior.ColorIndex = xlColorIndexNone 
      ColorMe Range("D10"), myRed
    End Sub
    
    
    Public Function ColorMe(Rng2Clr As Range, MYColor as Variant)
       Rng2Clr.Offset(-2, -2).Resize(3).Interior.ColorIndex = MYColor
    End Function
    Last edited by SamT; 03-29-2020 at 10:05 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Thank you everyone for your help!

Posting Permissions

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