Rectangle Macro over multiple lines

LethalLeigh

New Member
Joined
Jan 31, 2022
Messages
2
Office Version
  1. 365
Platform
  1. MacOS
Hi all,
Fairly new to macro's so still getting the hang of them with VB.
So I have this code

Sub NIST()
'
' NIST Macro
'
'
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=10
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=9
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=9, Criteria1:= _
"<>"
Dim shp As Shape

For Each shp In ActiveSheet.Shapes

If shp.Type = msoShapeRectangle Then
If Application.Caller = shp.Name Then
shp.Fill.ForeColor.SchemeColor = 3
Else
shp.Fill.ForeColor.SchemeColor = 4
End If
End If
Next
End Sub

And many more like it, over 4 rows. Each row represents a filter group.
The goal was to select one from each row that would turn green when clicked, so in total there would be 4 green boxes from the 25.
The code here was good when each row had seperate shapes, as i could differentiate them. Now I've been asked to do this when they are all rectangles.
So each time a new box is selected, the last green box disapears.
I have used rows 3,5,7,9 for the series of rectangles, and the macros work for filtering. But could you help me with the colouring?.

Thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Welcome to the forum.

I'm not sure if this is what you are looking for, but this code will act on the shapes in the "same" row only. I stripped out the autofiltering stuff for my testing, and you can add it back in where needed. But for the coloring, the shapes I created are shown in the image, and the code is below. The code works by identifying the calling shape and then making all shapes in the same row (or at the same Top value) blue followed by making the calling shape green.

VBA Code:
Sub NIST()
    Dim callingShp As Shape
    Dim rowShp As Shape
    
    For Each callingShp In ActiveSheet.Shapes
        If callingShp.Type = msoShapeRectangle Then
            If Application.Caller = callingShp.Name Then
                Exit For
            End If
        End If
    Next
    If callingShp Is Nothing Then Exit Sub
    For Each rowShp In ActiveSheet.Shapes
        If Abs(rowShp.Top - callingShp.Top) < 0.5 Then
            rowShp.Fill.ForeColor.SchemeColor = 4
        End If
    Next
    callingShp.Fill.ForeColor.SchemeColor = 3
End Sub
 

Attachments

  • Screenshot 2022-02-01 090257.png
    Screenshot 2022-02-01 090257.png
    12 KB · Views: 4
Upvote 0
Welcome to the forum.

I'm not sure if this is what you are looking for, but this code will act on the shapes in the "same" row only. I stripped out the autofiltering stuff for my testing, and you can add it back in where needed. But for the coloring, the shapes I created are shown in the image, and the code is below. The code works by identifying the calling shape and then making all shapes in the same row (or at the same Top value) blue followed by making the calling shape green.

VBA Code:
Sub NIST()
    Dim callingShp As Shape
    Dim rowShp As Shape
   
    For Each callingShp In ActiveSheet.Shapes
        If callingShp.Type = msoShapeRectangle Then
            If Application.Caller = callingShp.Name Then
                Exit For
            End If
        End If
    Next
    If callingShp Is Nothing Then Exit Sub
    For Each rowShp In ActiveSheet.Shapes
        If Abs(rowShp.Top - callingShp.Top) < 0.5 Then
            rowShp.Fill.ForeColor.SchemeColor = 4
        End If
    Next
    callingShp.Fill.ForeColor.SchemeColor = 3
End Sub
Works great. Thanks
 
Upvote 0

Forum statistics

Threads
1,214,645
Messages
6,120,711
Members
448,984
Latest member
foxpro

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