Search code examples
excelvbacellshapes

How to Copy the fill pattern of a cell to a shape?


I have been writing a code that is copying the color of the cell and pasting it to the relevant shape. I am able to copy the color. However, it seems more complicated to copy the fill pattern of the cell. The problem i have faced is that the pattern parameter of a cell is, for example, "xlLightHorizontal" and the same pattern for a shape is "msoPatternNarrowHorizontal". Both draw the same fill pattern but have different names.

How i copy the color and fill pattern of the cell:

modelText(3, 1) = Sheets("Orders").Cells(row, j + 1).Interior.Color
modelText(3, 2) = Sheets("Orders").Cells(row, j + 1).Interior.Pattern
modelText(3, 3) = Sheets("Orders").Cells(row, j + 1).Interior.PatternColor

How i assign the color and the fill pattern to the shape:

 With Selection.ShapeRange.Fill
        .ForeColor.RGB = modelText(3, 1)
        .BackColor.RGB = modelText(3, 3)
        .Patterned (msoPatternLightHorizontal)
 End With

Note that:

Cells(row, j + 1).Interior.Pattern returns xlLightHorizontal

Where

ShapeRange.Fill.Patterned does not accept xlLightHorizontal as an input parameter

Is there anyway that you know to copy the fill pattern of a cell to a shape?


Solution

  • I tried to find a solution. It looks like that the two set of enumeration are not matching. I hope you do have a very limited set of pattern you would like to use. In this case I would make a table of pairs (numbers to the same or at least similar pattern) of the enums to make the conversion. I have not better idea. Sorry.

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.core.msopatterntype?view=office-pia and https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.constants?view=excel-pia