Previous in Forum: Recovered Data Not Working   Next in Forum: Killing Gmail Folders
Close
Close
Close
4 comments
Rate Comments: Nested
Power-User

Join Date: Feb 2011
Location: Shimurali, Westbengal,India
Posts: 119

VBA (Excel 2007) Shape Handling and Color Change

08/13/2013 5:35 AM

This is regarding shape handling in Excel 2007 using VBA code.

I want to change color of shapes if two shapes are touching each other. Please help me for the code in VBA

Register to Reply
User-tagged by 2 users
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Good Answers:

These comments received enough positive votes to make them "good answers".
2
Active Contributor

Join Date: Jul 2012
Location: South Africa
Posts: 13
Good Answers: 3
#1

Re: VBA (Excel 2007) Shape Handling and Color Change

08/14/2013 12:05 AM

Very rough but should be able to move you in the right direction?

There might be better ways to continuously check the position of the shapes - I'm not aware of any "OnShapePositionChange" interrupts so I've just used a timer - this will check once every second as long as the page is active…

Private Sub Worksheet_Activate()
Application.OnTime Now + TimeValue("00:00:01"), "CheckPosition"
End Sub

Private Sub Worksheet_Deactivate()
Application.OnTime EarliestTime:=Now + TimeValue("00:00:01"), Procedure:="CheckPosition", Schedule:=False
End Sub

Sub CheckPosition()
Dim shp1x As Integer, shp1y As Integer, shp2x As Integer, shp2y As Integer

shp1x = ActiveSheet.Shapes("Rectangle 1").Left
shp1y = ActiveSheet.Shapes("Rectangle 1").Top
shp2x = ActiveSheet.Shapes("Rectangle 2").Left
shp2y = ActiveSheet.Shapes("Rectangle 2").Top
If (shp1x <= (shp2x + ActiveSheet.Shapes("Rectangle 2").Width)) And _
((shp1x + ActiveSheet.Shapes("Rectangle 1").Width) >= shp2x) And _
(shp1y <= (shp2y + ActiveSheet.Shapes("Rectangle 2").Height)) And _
((shp1y + ActiveSheet.Shapes("Rectangle 1").Height) >= shp2y) Then
ActiveSheet.Shapes("Rectangle 1").Line.ForeColor.RGB = RGB(255, 0, 0)
ActiveSheet.Shapes("Rectangle 1").Fill.ForeColor.RGB = RGB(255, 0, 0)
ActiveSheet.Shapes("Rectangle 2").Line.ForeColor.RGB = RGB(255, 0, 0)
ActiveSheet.Shapes("Rectangle 2").Fill.ForeColor.RGB = RGB(255, 0, 0)
Else
ActiveSheet.Shapes("Rectangle 1").Line.ForeColor.RGB = RGB(0, 255, 0)
ActiveSheet.Shapes("Rectangle 1").Fill.ForeColor.RGB = RGB(0, 255, 0)
ActiveSheet.Shapes("Rectangle 2").Line.ForeColor.RGB = RGB(0, 255, 0)
ActiveSheet.Shapes("Rectangle 2").Fill.ForeColor.RGB = RGB(0, 255, 0)
End If

Application.OnTime Now + TimeValue("00:00:01"), "CheckPosition"
End Sub

__________________
Living is more than mere existence and breath
Register to Reply Good Answer (Score 2)
Power-User

Join Date: Feb 2011
Location: Shimurali, Westbengal,India
Posts: 119
#2
In reply to #1

Re: VBA (Excel 2007) Shape Handling and Color Change

08/14/2013 2:49 AM

Thank you.

It works fine. In my work sheet nearly 10 types of shapes of about 50 to 80 numbers are present. So, without selecting the shapes how it is possible?

Register to Reply
Active Contributor

Join Date: Jul 2012
Location: South Africa
Posts: 13
Good Answers: 3
#3
In reply to #2

Re: VBA (Excel 2007) Shape Handling and Color Change

08/14/2013 3:09 AM

Change the script to point to the shapes via shapes.Count

The following will check all the shapes:

Dim scount As Integer

For scount = 1 To ActiveSheet.shapes.Count
shp1x = ActiveSheet.shapes(scount).Left

<etc.>
Next scount

I would also use some "do While" loops if you want to check against all the shapes.

__________________
Living is more than mere existence and breath
Register to Reply
Active Contributor

Join Date: Jul 2012
Location: South Africa
Posts: 13
Good Answers: 3
#4
In reply to #2

Re: VBA (Excel 2007) Shape Handling and Color Change

08/15/2013 10:11 PM

Found some time to play:

Still very rough but you'll get the idea.

Changed it to only change the line color since some shapes does not have the Fill property - you'll have to add some kind of check if you wish to change the fill color for the shapes that does have Fill.

Sub CheckPosition()
Dim shp1x As Integer, shp1y As Integer, shp1w As Integer, shp1h As Integer
Dim shp2x As Integer, shp2y As Integer, shp2w As Integer, shp2h As Integer
Dim scount As Integer, tcount As Integer, touch As Boolean

'Reset all shapes color before checking
For scount = 1 To ActiveSheet.shapes.Count
ActiveSheet.shapes(scount).Line.ForeColor.RGB = RGB(0, 255, 0)
Next scount

For scount = 1 To ActiveSheet.shapes.Count
shp1x = ActiveSheet.shapes(scount).Left
shp1y = ActiveSheet.shapes(scount).Top
shp1w = ActiveSheet.shapes(scount).Width
shp1h = ActiveSheet.shapes(scount).Height
For tcount = 1 To ActiveSheet.shapes.Count 'test against all other shapes
If tcount <> scount Then 'do not test against self
shp2x = ActiveSheet.shapes(tcount).Left
shp2y = ActiveSheet.shapes(tcount).Top
shp2w = ActiveSheet.shapes(tcount).Width
shp2h = ActiveSheet.shapes(tcount).Height
If (shp1x <= (shp2x + shp2w)) And ((shp1x + shp1w) >= shp2x) And _
(shp1y <= (shp2y + shp2h)) And ((shp1y + shp1h) >= shp2y) Then
ActiveSheet.shapes(tcount).Line.ForeColor.RGB = RGB(255, 0, 0)
touch = True
End If
End If
Next tcount
If touch Then 'this shape was touching one or more others
ActiveSheet.shapes(scount).Line.ForeColor.RGB = RGB(255, 0, 0)
Else
ActiveSheet.shapes(scount).Line.ForeColor.RGB = RGB(0, 255, 0)
End If
touch = False
Next scount


Application.OnTime Now + TimeValue("00:00:03"), "CheckPosition"
End Sub

__________________
Living is more than mere existence and breath
Register to Reply
Register to Reply 4 comments

Good Answers:

These comments received enough positive votes to make them "good answers".

Previous in Forum: Recovered Data Not Working   Next in Forum: Killing Gmail Folders

Advertisement