VBA code to fill shape transparent with any RGB colour cases

harjotawang

New Member
Joined
Nov 22, 2021
Messages
16
Office Version
  1. 2021
Platform
  1. Windows
this my code, i dont know how to make "case 0" become transparency

Sub Fillcolor()
For i = 1 To 10
API = Sheet1.Cells(i + 1, 7)
Select Case API

Case 0
a = 255
b = 255
b = 255
Case 1
a = 150
b = 100
Case 2
a = 255
b = 0
Case Else
a = 255
b = 0
End Select
Sheet1.Shapes.Range(Array("Freeform: Shape " & i)).Fill.ForeColor.RGB = RGB(a, b, 0)
Next i
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Fillcolor()
For i = 1 To 10
   API = Sheet1.Cells(i + 1, 7)
   With Sheet1.Shapes("Freeform: Shape " & i).Fill
      
      Select Case API
         Case 0
            .Visible = False
         Case 1
            .ForeColor.RGB = RGB(150, 100, 0)
         Case Else
            .ForeColor.RGB = RGB(255, 0, 0)
         End Select
   End With
Next i
End Sub
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Fillcolor()
For i = 1 To 10
   API = Sheet1.Cells(i + 1, 7)
   With Sheet1.Shapes("Freeform: Shape " & i).Fill
     
      Select Case API
         Case 0
            .Visible = False
         Case 1
            .ForeColor.RGB = RGB(150, 100, 0)
         Case Else
            .ForeColor.RGB = RGB(255, 0, 0)
         End Select
   End With
Next i
End Sub
so really big thanks to you, this code work, if you want to help me again please let me ask a question, how to make case "0", change/fill colour if its update value become 1 or 2 ?? i try to update the value, its still transparency.

Sub Fillcolor()
For i = 1 To 10
API = Sheet1.Cells(i + 1, 7)
With Sheet1.Shapes("Freeform: Shape " & i).Fill

Select Case API
Case 0
.Visible = False
Case 1
.ForeColor.RGB = RGB(150, 100, 0)
Case 2
.ForeColor.RGB = RGB(200, 50, 0)
Case Else
.ForeColor.RGB = RGB(255, 0, 0)
End Select
End With
Next i
End Sub
 
Upvote 0
If you change the value in the cell & run the code again, the fill colour should change.
 
Upvote 0
If you change the value in the cell & run the code again, the fill colour should change.
this screencapture from my problem, if the value "0" and change become "1"/"2", the shape doesnt change
 

Attachments

  • Screenshot 11-23-2021 20.22.46.png
    Screenshot 11-23-2021 20.22.46.png
    117.5 KB · Views: 19
  • Screenshot 11-23-2021 20.23.00.png
    Screenshot 11-23-2021 20.23.00.png
    117.7 KB · Views: 16
  • Screenshot 11-23-2021 20.24.01.png
    Screenshot 11-23-2021 20.24.01.png
    117.6 KB · Views: 16
  • Screenshot 11-23-2021 20.25.09.png
    Screenshot 11-23-2021 20.25.09.png
    124.6 KB · Views: 18
Upvote 0
Your shapes are grouped which is causing the problem, try ungrouping them.
 
Upvote 0
As long as none of the those shapes are grouped the code should work.
 
Upvote 0
As long as none of the those shapes are grouped the code should work.
please review my code, if any error. i stiil try but the shape can back colouring again after transpararency

Sub Fillcolor()
For i = 1 To 5
API = Sheet1.Cells(i + 1, 4)
With Sheet1.Shapes("gambar " & i).Fill

Select Case API
Case 0
.Visible = False
Case 1
.ForeColor.RGB = RGB(150, 100, 0)
Case 2
.ForeColor.RGB = RGB(200, 50, 0)
Case Else
.ForeColor.RGB = RGB(255, 0, 0)
End Select
End With
Next i

End Sub
 
Upvote 0
That code works for me. Do you get any error messages?
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,691
Members
449,117
Latest member
Aaagu

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