Finance

Charts

Statistics

Macros

Search

Selecting a Background Pattern for a Cell Range in Excel VBA

After selecting a range of cells—either programmatically or by the user—you can prompt the user to choose a background pattern or fill color for that range.

Example:

Sub SelectBackgroundPattern()
    Dim success As Boolean
    ' Select the cell range A1:C3 on the worksheet "Sheet1"
    ThisWorkbook.Worksheets("Sheet1").Range("A1:C3").Select
    ' Show the built-in Excel "Patterns" dialog for fill formatting
    success = Application.Dialogs(xlDialogPatterns).Show
    ' If the user cancels without selecting a pattern, show a message
    If Not success Then MsgBox "No pattern was selected"
End Sub

 

Explanation:

  • The constant xlDialogPatterns opens Excel’s built-in dialog box for cell fill patterns and colors, found under the Fill tab in the Format Cells dialog.
  • If the user clicks OK, the selected cells are formatted with the chosen background pattern or fill color.
  • If the user cancels the dialog, the variable success is False and a message box informs the user that no pattern was applied.

 

0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx