TheHighlander
New Member
- Joined
- Oct 4, 2024
- Messages
- 5
- Office Version
- 2016
- Platform
- Windows
Hello,
I am still fairly new to VBA programming but have been learning quickly. When looking for a way to add conditional formatting to a shape to make it change color, I came across "Private Sub Worksheet_Change(ByVal Target As Range)." While I was able to get this to work with a single shape and target, I do not know how to make this work with multiple targets.
The code I am trying to use as reference to alter my code is:
---------------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("E3") = "COPIED" Then
ActiveSheet.Shapes.Range(Array("Rectangle1")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(112, 173, 71)
Else
If Range("E3") = "COPY" Then
ActiveSheet.Shapes.Range(Array("Rectangle1")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(91, 155, 213)
End If
End If
ActiveSheet.Cells(2, 6).Select
End Sub
-----------------------------------------------------------------------------
(toggle BB code does not seem to be working for me)
I can get this to work, but only for 1 button.
What I want to happen:
When you click the "COPY" button, the button changes color and the words change to "COPIED." Clicking it again changes the color back and the word becomes "COPY" again.
I have all the code done except my method of changing color has a fault.
My problem is that I also have macros that sort the Name, Level, Kind, and Number columns. I have included the hidden column "E" (DATA column) in the sort so that the "COPIED" notation
moves with the other data. But I also need the button color to move with the data.
I have only 5 of these on my testing file, but will have 100 of these buttons on my actual file, so am looking for the easiest and most elegant solution to this.
I have included a screenshot as the buttons (shapes) that have macros attached to them do not seem to be showing up in the Mini Sheet picture.
This is my current code for the COPY buttons
----------------------------------------------------------------------------
Sub Copy2()
With ActiveSheet
If Range("E2").Value = "COPY" Or Range("E2") = Empty Then
Range("E2").Value = "COPIED"
ActiveSheet.Shapes("Rectangle 1").Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(112, 173, 71)
Else
Range("E2").Value = "COPY"
ActiveSheet.Shapes("Rectangle 1").Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(91, 155, 213)
End If
Cells(2, 11).Resize(, 4).Value = Cells(2, 1).Resize(, 4).Value
End With
End Sub
--------------------------------------------------------------------------------
I am still fairly new to VBA programming but have been learning quickly. When looking for a way to add conditional formatting to a shape to make it change color, I came across "Private Sub Worksheet_Change(ByVal Target As Range)." While I was able to get this to work with a single shape and target, I do not know how to make this work with multiple targets.
The code I am trying to use as reference to alter my code is:
---------------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("E3") = "COPIED" Then
ActiveSheet.Shapes.Range(Array("Rectangle1")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(112, 173, 71)
Else
If Range("E3") = "COPY" Then
ActiveSheet.Shapes.Range(Array("Rectangle1")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(91, 155, 213)
End If
End If
ActiveSheet.Cells(2, 6).Select
End Sub
-----------------------------------------------------------------------------
(toggle BB code does not seem to be working for me)
I can get this to work, but only for 1 button.
What I want to happen:
When you click the "COPY" button, the button changes color and the words change to "COPIED." Clicking it again changes the color back and the word becomes "COPY" again.
I have all the code done except my method of changing color has a fault.
My problem is that I also have macros that sort the Name, Level, Kind, and Number columns. I have included the hidden column "E" (DATA column) in the sort so that the "COPIED" notation
moves with the other data. But I also need the button color to move with the data.
I have only 5 of these on my testing file, but will have 100 of these buttons on my actual file, so am looking for the easiest and most elegant solution to this.
I have included a screenshot as the buttons (shapes) that have macros attached to them do not seem to be showing up in the Mini Sheet picture.
This is my current code for the COPY buttons
----------------------------------------------------------------------------
Sub Copy2()
With ActiveSheet
If Range("E2").Value = "COPY" Or Range("E2") = Empty Then
Range("E2").Value = "COPIED"
ActiveSheet.Shapes("Rectangle 1").Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(112, 173, 71)
Else
Range("E2").Value = "COPY"
ActiveSheet.Shapes("Rectangle 1").Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(91, 155, 213)
End If
Cells(2, 11).Resize(, 4).Value = Cells(2, 1).Resize(, 4).Value
End With
End Sub
--------------------------------------------------------------------------------
Copy Button2.xlsm | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | M | N | |||
1 | Name | Level | Kind | Number | DATA | DATA COPIED | ||||||||||
2 | Bill | level 7 | red | 150,000 | COPY | Mary | level 10 | yellow | 310000 | |||||||
3 | Bob | level 9 | yellow | 250,000 | COPY | |||||||||||
4 | Cat | level 8 | red | 340,000 | COPY | |||||||||||
5 | Kathy | level 10 | blue | 283,000 | COPY | |||||||||||
6 | Mary | level 10 | yellow | 310,000 | COPY | |||||||||||
7 | Willy | level 4 | yellow | 300,000 | ||||||||||||
8 | ||||||||||||||||
9 | ||||||||||||||||
10 | Name A | # | ||||||||||||||
11 | Type A | |||||||||||||||
12 | Level A | |||||||||||||||
13 | Num A | |||||||||||||||
14 | ||||||||||||||||
15 | SUB | |||||||||||||||
16 | Willy | level 4 | blue | 300000 | ||||||||||||
17 | ||||||||||||||||
18 | ||||||||||||||||
19 | ||||||||||||||||
20 | ||||||||||||||||
21 | ||||||||||||||||
22 | ||||||||||||||||
23 | ||||||||||||||||
24 | ||||||||||||||||
25 | ||||||||||||||||
ALL |