How to add gradients to cells using the Gradient property in an Excel macro

Advantages of the Gradient property in an Excel macro

La Gradient property is a useful tool for formatting cells in spreadsheets by applying gradient colors. Therefore, using it correctly will allow you to do the following:

  • Customize an Excel sheet, easily differentiating selected tables and cells within range and domain.
  • Give conditional formats to Excel sheets, using a macro.

configure the gradient, you can also have a subsection known as a ColorStop object. Contains a collection of all the stop or end points of a color in the gradient. We emphasize that, through this object, color can be applied within a cell or a range. You can also change the appearance color of a Microsoft Excel sheet.




Therefore, the default gradients can be used or if you decide to create them from scratch to your liking. We will then explain the use of the Gradient property to add gradient colors inside cells using macros.

How to add gradients with predefined objects with ColorStop

There are various methods and ways to add gradients to cells using the Gradient property. However, to demonstrate the process in the simplest way, we will use the Microsoft Visual Basic macro language known as Microsoft VBA.

Next, we will explain how you can change the first and second color of a gradient using VBA for Excel. You have to keep in mind that the following codes explained step by step change alone the color of the gradient. So it is assumed that a gradient already exists in the cell in question, so:

  • First of all you should know that when you create a gradient using VBA, 2 ColorStop objects emerge from the primary gradient collection. Where one of the color stop objects will have position 1 and the other will have position 2.

How to add gradients to cells using the Gradient property in an Excel macro




  • Now to fully use the properties of the gradient in VBA, it is recommended to change the default positions to 0 and 1. In this way, you can add an intermediate position as follows:

Sub main ()
Dim objColorStop As ColorStop
Dim lngColor1 As Long
Dim lngColor0 As Long

'creates the gradient in cell A1
Range (โ€œA1โ€). Interior.Pattern = xlPatternLinearGradient
'changes its orientation
Range (โ€œA1โ€). Interior.Gradient.Degree = 90
'get the color code for the first object
colorstop lngColor0 = Range (โ€œA1โ€). Interior.Gradient.ColorStops (1).Color
'get the color code for the second object
colorstop lngColor1 = Range (โ€œA1โ€). Interior.Gradient.ColorStops (2).Color
'delete the previous colostop objects
Range (โ€œA1โ€). Interior.Gradient.ColorStops.Clear
'create a colorstop object with position 0
Set objColorStop = Range (โ€œA1โ€). Interior.Gradient.ColorStops.Add (0)
'changes its color to the first color
objColorStop.Color = lngColor0
'create a colorstop object with position 1
Set objColorStop = Range (โ€œA1โ€). Interior.Gradient.ColorStops.Add (1)
'changes its color to the second color
objColorStop.Color = lngColor1
End Sub

  • This code creates a gradient in cell A1 and changes the default positions to 0 and 1.

How to add gradients to cells using the Gradient property in an Excel macro

How to add shades with multiple colors with ColorStop

To create a gradient with different colors using macros in a Excel sheet, you need to follow the following code.


'creates the gradient in cell A1
Range (โ€œA1โ€). Interior.Pattern = xlPatternLinearGradient
'changes its orientation
Range (โ€œA1โ€). Interior.Gradient.Degree = 90
'delete previous colostop objects
Range (โ€œA1โ€). Interior.Gradient.ColorStops.Clear
'Create the color stops for the gradient in cell A1
Imposta objColorStop = Range (โ€œA1โ€). Interior.Gradient.ColorStops.Add (0)
objColorStop.Color = vbYellow
Imposta objColorStop = Range (โ€œA1 โ€œ).Interior.Gradient.ColorStops.Add (0.33)
objColorStop.Color = vbRed
Imposta objColorStop = Range (โ€ A1 โ€œ). Interior.Gradient.ColorStops.Add (0.66)
objColorStop.Color = vbGreen
Set objColorStop = Range ("A1" ") .Interior.Gradient.ColorStops. Add (1)
objColorStop.Color = vbBlue
End Sub



We hope that with these simple instructions and tips you will learn how to add gradients to cells using the Gradient property in an Excel macro. If you also want to know how WordPress posts can be displayed, don't waste time, enter here.

add a comment of How to add gradients to cells using the Gradient property in an Excel macro
Comment sent successfully! We will review it in the next few hours.