Color fill cells surrounding shapes

julhs

Active Member
Joined
Dec 3, 2018
Messages
471
Office Version
  1. 2010
Platform
  1. Windows
I want to color fill a range of cells dependent on position of 2 shapes.
This code works,
VBA Code:
Public Sub Color_Shapes_Position()
Dim rng As Range
Dim rng1 As Range
Dim sTopLeft As Variant
Dim sBottomRight As Variant

      sTopLeft = ActiveSheet.Shapes("Rounded Rectangle 180").TopLeftCell.Address
      sBottomRight = ActiveSheet.Shapes("Rounded Rectangle 185").BottomRightCell.Address

  Set rng1 = Range(Range(sTopLeft), Range(sBottomRight))
     rng1.Select

 With Selection.Interior
   .Pattern = xlSolid
   .PatternColorIndex = xlAutomatic
   .ThemeColor = xlThemeColorAccent6
   .TintAndShade = 0.799981688894314
   .PatternTintAndShade = 0
 End With
End Sub

BUT I want to color a slightly large range; ie
VBA Code:
Set rng1 = Range(Range(sTopLeft -1), Range(sBottomRight +1))
Everything I have tried just so far is ending up with a Type mismatch 13 error
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
1690031757858.png
 
Upvote 0
Try replacing your Set rng1= etc etc with
VBA Code:
Set Rng1 = Range(Range(sTopLeft).Offset(-1, -1), Range(sBottomRight).Offset(1, 1))
 
Upvote 0
You can find the rows and columns, then subtract or add to them

Note: I see you have added another post, you can use what I have supplied to set the new ranges for your other colors.
Excel Formula:
Sub Button1_Click()
    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim shp1 As Shape, shp2 As Shape
    Dim r1 As Long, c1 As Long
    Dim r2 As Long, c2 As Long
    Dim rng1 As Range
    With ws
        Set shp1 = .Shapes("Rectangle 1")
        Set shp2 = .Shapes("Rectangle 2")

        With shp1
            r1 = .TopLeftCell.Row: c1 = .TopLeftCell.Column
        End With
        With shp2
            r2 = .BottomRightCell.Row: c2 = .BottomRightCell.Column
        End With

        Set rng1 = .Range(.Cells(r1 - 1, c1 - 1), .Cells(r2 + 1, c2 + 1))


        rng1.Interior.Color = rgbAliceBlue

    End With
End Sub
 
Upvote 0
Hi Antony

Thanks for that.
It does add the extra fill color, top and bottom BUT it also adds it left and right as well.

While the extra on the left & right is not ideal I can live with it if there is not a way to prevent it?
 
Upvote 0
Rich (BB code):
    Set Rng1 = Range(Range(sTopLeft).Offset(-1, -1), Range(sBottomRight).Offset(1, 1))

The -1 in Red create the extra column on the left, the 1 in Blue the one on the right; replace both by 0 to color the area you prefer. That would also prevent errors in case the first shape is in column A
 
Upvote 0
Thanks Dave.
It is giving me the same result as Antony’s ( extra fill left and right)
 
Upvote 0
Thanks Antony.
Thats what I was after.
Many thanks
Julhs
 
Upvote 0
Thanks Dave.
It is giving me the same result as Antony’s ( extra fill left and right)
Did you read the post? The code adds or subtracts to the row number or column number, change the numbers being added or subtracted
 
Upvote 0
Dave
I posted #7 BEFORE I tweaked yours to suit me.
Having done so it does work for me as wanted
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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